猿问

数组中的数字增量

我想递增 (+1) 具有 N 个值并表示整数的 int 数组的最后一位。每个值都是 0-9 之间的单个数字。


逻辑是这样的:如果要增加的数字是 9,它必须变成 0,下一个(从右到左)必须增加 1。如果你到达数组的第一个数字,它是 9,这将变成 10。示例:


[3,4,5,6] -> [3,4,5,7]  

[3,9,2,9] -> [3,9,3,0]  

[3,4,9,9] -> [3,5,0,0]  

[9,9,9,9] -> [10,0,0,0]  

我做了同样的练习,但只有 4 位数,所以逻辑很简单:


int[] incrementArrayDigits(int[] fourDigits) {


    if (fourDigits[3] != 9) {

        fourDigits[3]++;

    } else if (fourDigits[2] != 9) {

        fourDigits[3] = 0;

        fourDigits[2]++;

    } else if (fourDigits[1] != 9) {

        fourDigits[3] = 0;

        fourDigits[2] = 0;

        fourDigits[1]++;

    } else if (fourDigits[0] != 9) {

        fourDigits[3] = 0;

        fourDigits[2] = 0;

        fourDigits[1] = 0;

        fourDigits[0]++;

    }


    if (fourDigits[0] == 9 && fourDigits[1] == 9 && fourDigits[2] == 9 && 

        fourDigits[3] == 9) {

        fourDigits[1] = fourDigits[2] = fourDigits[3] = 0;

        fourDigits[0] = 10;

    }


    System.out.println(Arrays.toString(fourDigits));

    return fourDigits;

}

我试图解决 N 个数字取数组长度然后使用 for 循环的问题,但我无法达到预期的结果。


qq_花开花谢_0
浏览 129回答 4
4回答

RISEBY

解决这个问题的一种方法是使用递归。这个想法是跟踪您正在递增的数组的哪个元素。您从 index 处的元素开始array.length - 1,递增它,如果它达到 10,则将其设置为 0,然后对 index 处的元素执行相同的操作array.length - 2,依此类推。另请注意,由于您是在方法中更改数组,因此不必返回数组。private static void incrementArrayDigits(int[] array, int position) {&nbsp; &nbsp; if (position >= array.length || position < 0) {&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; &nbsp; array[position]++;&nbsp; &nbsp; if (array[position] == 10 && position != 0) {&nbsp; &nbsp; &nbsp; &nbsp; array[position] = 0;&nbsp; &nbsp; &nbsp; &nbsp; incrementArrayDigits(array, position - 1);&nbsp; &nbsp; }}// usage:int[] array = {9,9,9};incrementArrayDigits(array, array.length - 1);System.out.println(Arrays.toString(array));

慕妹3146593

这是我想出的解决方案。希望能帮助到你!public void incrementArrayDigits(int[] arr) {&nbsp; &nbsp; if(arr == null)&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; int currIndex = arr.length - 1;&nbsp; &nbsp; while(currIndex > -1){&nbsp; &nbsp; &nbsp; &nbsp; arr[currIndex]++;&nbsp; &nbsp; &nbsp; &nbsp; if(arr[currIndex] < 10)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; else if (currIndex < 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[currIndex--] = 0;&nbsp; &nbsp; }}

回首忆惘然

一种可能的解决方案是向后迭代数组并在需要时递增:private static int[] incrementArrayDigits(int[] fourDigits) {&nbsp; &nbsp; for (int i = fourDigits.length - 1; i >= 0; i--) {&nbsp; &nbsp; &nbsp; &nbsp; fourDigits[i]++; // increment&nbsp; &nbsp; &nbsp; &nbsp; if (i > 0) { // cut result to 0-9, if not the first value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fourDigits[i] %= 10;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (fourDigits[i] > 0) { // if no carry is passed break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return fourDigits;}

富国沪深

事实上,你真正想要达到的是“PLUS 1”。如果你的 N <= 10,直接使用 int。如果您的 N <=10,请使用 long。当然,如果你真的需要N非常大。尝试为数字实现一个类?
随时随地看视频慕课网APP

相关分类

Java
我要回答