打印所有唯一的数字

时间:2016-09-22 05:24:27

标签: c# algorithm data-structures

问题:打印所有只有唯一数字的号码。 输入:n = 15 输出:1 2 3 4 5 6 7 8 9 10 12 13 14 15

这里不包括11,因为它有1次,相同的方式123,456 ..也有效,但121 1344无效,因为有多个相同的数字。

我正在从1-n运行循环并检查每个号码。 我正在使用Hash-map来确定数字的唯一性。

上述问题是否有更好的解决方案。

8 个答案:

答案 0 :(得分:3)

我不确定,但是那样......

 List<int> numbers = new List<int>(){};
 numbers =numbers.Where(p=>validCheck(p)==true).ToList();

static bool validCheck(int n)
{
 return (n.ToString().Length==n.ToString().Disctinct().Count());
}

答案 1 :(得分:2)

您可以使用LINQ,将数字转换为字符串,并检查字符串的长度是否等于不同的charchters的数量。

for (int i = 1; i < n; i++){
  if (i.ToString().Length == i.ToString().Distinct().Count())
    Console.Out.Write(i + " ");
} 

答案 2 :(得分:1)

作为一个半有用的库函数,你可以用一个开头和你想要的数量来播种它。

UniqueDigits(0,15).ToList().ForEach(Console.WriteLine);   

然后

foreach (var digit in UniqueDigits(100,50))
{
    Console.WriteLine(digit);
}

<a href="#divIdName">Go here</a>
<div id="divIdName">Destination</div>

答案 3 :(得分:0)

这就是我如何消除具有重复字符的数字。

declare @Startdate datetime; @Startdate = convert(datetime, 'mydate')

答案 4 :(得分:0)

我的想法:

  1. 将循环从0运行到n
  2. 对于每批10个(例如从0到9,10到19,230到239 ..),选择与最后一个数字分开的数字。这些数字映射到倾向于被跳过的计数器。休息一切都要发出。例如:对于批次12x,选择1&amp; 2,现在我们知道我们必须跳过位置1和位置2的数字,其余的都是可以接受的,所以不需要对它们进行任何处理。
  3. 将上述数字按排序方式保存在arrayList中,并将指针保留在索引0处。让我们将其称为“ptr&#39;”。在运行该批处理时,检查每个批处理的count(从0移动到9)是否等于数组[ptr]。如果不是,请发出号码。否则,跳过它并做ptr ++。
  4. 当您执行第2步时,请检查是否有任何数字重复。如果是,请跳过整批10。
  5. 没有发生字符串操作,所以它应该带来效率

答案 5 :(得分:0)

字符串是IEnumerable - 因此您可以使用LINQ语句来解决您的问题:

Numbers.Where(N => N.ToString().Distinct().Count() == N.ToString().Length);

查询检查您的号码字符串中有多少个字符不同,并将此号码与总字符数相匹配。

这是整个代码打印出所有不同的数字,直到20:

List<int> Numbers = new List<int>();

for (int i = 1; i <= 20; i++)
{
    Numbers.Add(i);
}

IEnumerable<int> AcceptedNumbers = Numbers.Where(N => N.ToString().Distinct().Count() == N.ToString().Length);

foreach (int AcceptedNumber in AcceptedNumbers)
{
    Console.WriteLine(AcceptedNumber);
}

答案 6 :(得分:0)

另一个解决方案是使用整数除法和模数(没有数字到字符串转换)。您可以使用以下方法验证数字的唯一性(假设digits是具有10个元素的int数组)。

public static bool IsUnique(int num) {
    int[] digits = new int[10];
    num = Math.Abs(num);
    while (num > 0) {
        int r = num % 10;
        num /= 10;

        digits[r] ++;
        if (digits[r] > 1) {
            return false;
        }
    }
    return true;
}

工作示例http://ideone.com/9emEoz

答案 7 :(得分:0)

只有9 * 9! /(10 - n)!带有n位数的唯一数字数字。对于较大的n,您可能需要下一个词典算法来避免不必要的迭代。 (例如,只有544,320个7位唯一数字,但你的程序需要迭代近1000万个数字来生成它们!)

以下是我对下一个词典过程的尝试,其中包含一组n-unique-digit个数字(n > 1}):

(1) From left to right, start with the digits 10, then ascend from 2.
    For example, the first 4-digit number would be 1023.

(2) Increment the right-most digit that can be incremented to the next available 
    higher digit unused by digits to its left. Ascend to the right of the
    incremented digit with the rest of the available digits, starting with lowest.

Examples: 1023 -> 1024 (4 is unused by the digits left of 3)
             ^

          9786 -> 9801 (8 is unused be the digits left of 7)
           ^

          9658 -> 9670 (7 is unused by the digits left of 5)
            ^
相关问题