编程挑战:如何检查列表中的所有元素是否唯一 [英] Programming challenge: how to check if all elements in a list is unique

查看:75
本文介绍了编程挑战:如何检查列表中的所有元素是否唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helllo!我正在做一个c#编程挑战和即时通讯我是否理解并解决了这个问题吧?



问题是

给出一个电话号码列表,确定它是否在某种意义上是一致的没有数字是另一个的前缀。假设电话目录列出了这些数字:



紧急911

Alice 95 225 256

Bob 91 12 54 26

在这种情况下,无法给Bob打电话,因为只要您拨打了Bob的电话号码的前三位数字,中心就会将您的呼叫转到紧急线路。所以这个列表不一致。



第一行输入给出一个整数,1≤t≤40,即测试用例的数量。每个测试用例以单独一行的电话号码n开始,1≤n≤10000。然后跟随n行,每行有一个唯一的电话号码。电话号码是最多十位数的序列。



对于每个测试用例,如果列表一致则输出YES,否则输出NO。 br />


我这样解决了:

Helllo! im doing a c# programming challange and im woundering if i understood and solved the question right?

The question is:
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let’s say the phone catalogue listed these numbers:

Emergency 911
Alice 95 225 256
Bob 91 12 54 26
In this case, it’s not possible to call Bob, because the central would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.

The first line of input gives a single integer, 1≤t≤40, the number of test cases. Each test case starts with n, the number of phone numbers, on a separate line, 1≤n≤10000. Then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits.

For each test case, output "YES" if the list is consistent, or "NO" otherwise.

I solved it this way:

List<string> phoneList = new List<string>();
         string input;

         while ((input = Console.ReadLine()) != null)
         {
           phoneList.Add(input);
         }

         for (int i = 0; i < phoneList.Count; i++)
         {
             for (int j = 0; j < phoneList.Count; j++)
             {

                 if (phoneList[i].Substring(0,3).Equals(phoneList[j].Substring(0,3)) && i != j)
                 {
                     Console.WriteLine("NO");
                     return;
                 }
             }
         }

         Console.WriteLine("YES");







它是否正确解决?我尝试过使用Distinct()方法insteed但不知道如何使它与Substring()方法一起使用。我非常感谢所有的Anwsers!谢谢!




Is it solved Correctly? and i tried to use the Distinct() method insteed but didint know how to make it work with the Substring() method. I really appreciate all Anwsers! Thank you!

推荐答案

一个更简单的解决方案是对所有基于字符串的数字进行排序。

此时,任何包含另一个数字的数字将立即出现在列表中。

A simpler solution is to sort all the string-based numbers.
At that point, any number which contains another will appear in the list immediately after it.
123456
911
91125426
95225256

那么,它只是一个循环:

So then, it's just one loop:

if (thisString.StartsWith(previousString))
   {
   // It's a match.
   }


如果您能够假设所有紧急号码都是固定的位数,在大多数情况下; 3位数,那么问题更容易解决。如果是这样,那么它们可以被忽略。



否则你可以处理第二个唯一前缀号码列表:

代码之后:

If you are able to make an assumption that all emergency numbers will be a fixed number of digits, in most cases; 3 digits, then the problem is easier to solve. If so, then they can be ignored.

Otherwise you could process a second list of unique prefix numbers:
After your code:
while ((input = Console.ReadLine()) != null)
{
    phoneList.Add(input);
}







List<string> prefixes = new List<string>();
foreach(string number in phoneList)
{
    prefixes.Add(phoneList.SubString(0,2));
}</string></string>



然后检查该列表是否包含要验证的电话号码。


Then check that list contains the phone number being validated.


这篇关于编程挑战:如何检查列表中的所有元素是否唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆