有没有一种简单的方法可以将int转换为每个数字的整数数组?

时间:2022-08-16 21:24:30

Say I have

说我有

var i = 987654321;

Is there an easy way to get an array of the digits, the equivalent of

有一种简单的方法来获得数字的数组,相当于

var is = new int[] { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

without .ToString()ing and iterating over the chars with int.Parse(x)?

没有.ToString()和使用int.Parse(x)迭代字符?

7 个解决方案

#1


public Stack<int> NumbersIn(int value)
{
    if (value == 0) return new Stack<int>();

    var numbers = NumbersIn(value / 10);

    numbers.Push(value % 10);

    return numbers;
}

var numbers = NumbersIn(987654321).ToArray();

Alternative without recursion:

替代没有递归:

public int[] NumbersIn(int value)
{
    var numbers = new Stack<int>();

    for(; value > 0; value /= 10)
        numbers.Push(value % 10);

    return numbers.ToArray();
}

#2


I know there are probably better answers than this, but here is another version:

我知道可能有更好的答案,但这是另一个版本:

You can use yield return to return the digits in ascending order (according to weight, or whatever it is called).

您可以使用yield return以升序返回数字(根据权重或其他任何方式)。

public static IEnumerable<int> Digits(this int number)
{
    do
    {
        yield return number % 10;
        number /= 10;
    } while (number > 0);
}

12345 => 5, 4, 3, 2, 1

12345 => 5,4,3,2,1

#3


Another alternative which don't uses recursion and uses a Stack that avoids reallocation on every insert (at least for the first 32 digits):

另一种不使用递归并使用Stack的替代方法,避免在每个插入时重新分配(至少对于前32位):

var list = new Stack<int>(32);
var remainder = 123456;
do
{
    list.Push(remainder % 10);
    remainder /= 10;
} while (remainder != 0);

return list.ToArray();

And yes, this method also works for 0 and negative numbers.

是的,这种方法也适用于0和负数。

Interestingly, give this algorithm a negative number -123456 and you will get {-1, -2, -3, -4, -5, -6}

有趣的是,给这个算法一个负数-123456,你会得到{-1,-2,-3,-4,-5,-6}

Update: switched from using List to Stack since this automatically gives the correct order.

更新:从使用列表切换到堆栈,因为这会自动给出正确的顺序。

#4


var x = new Stack<int>();
do
{
    x.Push(i % 10);
    i /= 10;
} while (i > 0);
return x.ToArray();

#5


In short: use loop which divide number modulo 10 (%) to get reminder (each digit) and put it into array.

简而言之:使用循环将数字模10(%)除以得到提醒(每个数字)并将其放入数组中。

#6


Strings and can fun (some of the other options would be faster... but this is pretty easy)

字符串和乐趣(一些其他选项会更快......但这很容易)

var @is = 987654321.ToString().Select(c => c - 48).ToArray();

#7


This does convert to string and iterate over the characters, but it does it sort of automatically and in a one-liner:

这确实转换为字符串并迭代字符,但它会自动排序并在单行中:

var i = 987654321;
var @is = i.ToString().Select(c => c - '0').ToArray();

#1


public Stack<int> NumbersIn(int value)
{
    if (value == 0) return new Stack<int>();

    var numbers = NumbersIn(value / 10);

    numbers.Push(value % 10);

    return numbers;
}

var numbers = NumbersIn(987654321).ToArray();

Alternative without recursion:

替代没有递归:

public int[] NumbersIn(int value)
{
    var numbers = new Stack<int>();

    for(; value > 0; value /= 10)
        numbers.Push(value % 10);

    return numbers.ToArray();
}

#2


I know there are probably better answers than this, but here is another version:

我知道可能有更好的答案,但这是另一个版本:

You can use yield return to return the digits in ascending order (according to weight, or whatever it is called).

您可以使用yield return以升序返回数字(根据权重或其他任何方式)。

public static IEnumerable<int> Digits(this int number)
{
    do
    {
        yield return number % 10;
        number /= 10;
    } while (number > 0);
}

12345 => 5, 4, 3, 2, 1

12345 => 5,4,3,2,1

#3


Another alternative which don't uses recursion and uses a Stack that avoids reallocation on every insert (at least for the first 32 digits):

另一种不使用递归并使用Stack的替代方法,避免在每个插入时重新分配(至少对于前32位):

var list = new Stack<int>(32);
var remainder = 123456;
do
{
    list.Push(remainder % 10);
    remainder /= 10;
} while (remainder != 0);

return list.ToArray();

And yes, this method also works for 0 and negative numbers.

是的,这种方法也适用于0和负数。

Interestingly, give this algorithm a negative number -123456 and you will get {-1, -2, -3, -4, -5, -6}

有趣的是,给这个算法一个负数-123456,你会得到{-1,-2,-3,-4,-5,-6}

Update: switched from using List to Stack since this automatically gives the correct order.

更新:从使用列表切换到堆栈,因为这会自动给出正确的顺序。

#4


var x = new Stack<int>();
do
{
    x.Push(i % 10);
    i /= 10;
} while (i > 0);
return x.ToArray();

#5


In short: use loop which divide number modulo 10 (%) to get reminder (each digit) and put it into array.

简而言之:使用循环将数字模10(%)除以得到提醒(每个数字)并将其放入数组中。

#6


Strings and can fun (some of the other options would be faster... but this is pretty easy)

字符串和乐趣(一些其他选项会更快......但这很容易)

var @is = 987654321.ToString().Select(c => c - 48).ToArray();

#7


This does convert to string and iterate over the characters, but it does it sort of automatically and in a one-liner:

这确实转换为字符串并迭代字符,但它会自动排序并在单行中:

var i = 987654321;
var @is = i.ToString().Select(c => c - '0').ToArray();