简体   繁体   中英

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. I got 2 version of this problem. First one does not work when the input is [1,4,4,9].However, the second one is wokring. In the second version, I change the for-loop condition. I wonder why this happened? Could anyone help me with this? Any help would be appreciated! Thanks! Version 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:

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. 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 ;
You're changing the variable carry , but this value is being used in the next line.

Let's say, carry was 1 and digits[j] is 5 . After carry = (digits[j]+carry)/10; the variable carry changed to 0 and the subsequent calculation of digits[j] results in 5 instead of 6 .

The second example works, because you're introducing an intermediate variable 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. In turn, you will get the result you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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