简体   繁体   English

java中for循环条件的一个问题

[英]A question about for-loop condition in java

I am working on the problem below: Given a non-empty array of digits representing a non-negative integer, plus one to the integer.我正在解决以下问题:给定一个表示非负整数的非空数字数组,加上整数。 Eg:[1,2,3] Output:[1,2,4] My approach is usring carry to calculate the singular digit from end to start.例如:[1,2,3] 输出:[1,2,4] 我的方法是使用进位来计算从结束到开始的单数。 I got 2 version of this problem.我得到了这个问题的 2 个版本。 First one does not work when the input is [1,4,4,9].However, the second one is wokring.当输入为 [1,4,4,9] 时,第一个不起作用。但是,第二个是工作。 In the second version, I change the for-loop condition.在第二个版本中,我更改了 for 循环条件。 I wonder why this happened?我想知道为什么会这样? Could anyone help me with this?有人可以帮我解决这个问题吗? Any help would be appreciated!任何帮助,将不胜感激! Thanks!谢谢! Version 1:版本 1:

   public static int[] plusOne(int[] digits) {
        int sum = 0;
        int len = digits.length;
        int carry = (digits[digits.length -1]+1)/10;
        digits[digits.length -1] = (digits[digits.length -1]+1)%10;

        for (int j = len-2;j >= 0;j--){

            carry = (digits[j]+carry)/10 ;
            digits[j] =(digits[j]+carry)%10 ;
        }

        if (carry==0) return digits;
        else{
            int[] res = new int[len+1];
            res[0] = 1;
            int i = 0;
            for (int j=1;j<res.length;j++){
                res[j] = digits [i++];
            }
            return res;
        }
    }

Version 2:版本 2:

public static int[] plusOne(int[] digits) {
        int sum = 0;
        int len = digits.length;
        int carry = (digits[digits.length -1]+1)/10;
        digits[digits.length -1] = (digits[digits.length -1]+1)%10;

        for (int j = len-2;j >= 0;j--){
        int temp = digits[j]+carry;
        carry = (temp)/10 ;
        digits[j] =(temp)%10 ;
        }

        if (carry==0) return digits;
        else{
            int[] res = new int[len+1];
            res[0] = 1;
            int i = 0;
            for (int j=1;j<res.length;j++){
                res[j] = digits [i++];
            }
            return res;
        }

Posting a longer answer after all...毕竟发布更长的答案......

The only difference in those two code snippets are the first for loops.这两个代码片段的唯一区别是第一个for循环。 Your first example doesn't work as you expected.您的第一个示例没有按预期工作。

for (int j = len-2;j >= 0;j--){
        carry = (digits[j]+carry)/10 ;
        digits[j] =(digits[j]+carry)%10 ;
}

Here the problem lies in the line carry = (digits[j]+carry)/10 ;这里的问题在于行进carry = (digits[j]+carry)/10 ;
You're changing the variable carry , but this value is being used in the next line.您正在更改变量carry ,但该值将在下一行中使用。

Let's say, carry was 1 and digits[j] is 5 .比方说, carry1digits[j]5 After carry = (digits[j]+carry)/10; carry = (digits[j]+carry)/10;carry = (digits[j]+carry)/10; the variable carry changed to 0 and the subsequent calculation of digits[j] results in 5 instead of 6 .变量carry更改为0并且随后的digits[j]计算结果为5而不是6

The second example works, because you're introducing an intermediate variable temp :第二个示例有效,因为您要引入一个中间变量temp

for (int j = len-2;j >= 0;j--){
    int temp = digits[j]+carry;
    carry = (temp)/10 ;
    digits[j] =(temp)%10 ;
}

temp is calculated once and then only read from, so carry and digits are not dependant on each other any more. temp计算一次,然后只能从中读取,因此carrydigits不再相互依赖。 In turn, you will get the result you expect.反过来,你会得到你期望的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM