Is there any better way to solve this problem in Java?


  • 0
    B

    Here is my code and it works.But I think it is not good, do you have any better solution?Thanks.

    public class Solution {
    public int[] plusOne(int[] digits) {
        boolean flag = true;
        for(int i = 0;i<digits.length;i++){
            if(digits[i] != 9){
                flag = false;
                break;
            }
           
        }
        int temp = 1;
        if(flag){
            int[] newDigit = new int[digits.length+1];
            newDigit[0]= 0;
            
            for(int i=newDigit.length-1;i>0;i-- ){
                if(digits[i-1]+temp==10){
                    newDigit[i]=0;
                    temp = 1;
                }else{
                    newDigit[i]=digits[i-1]+temp;
                    temp = 0;
                }
            }
            if(temp == 1)
                newDigit[0] = 1;
            return newDigit;
        }else{
            for(int i= digits.length-1;i>=0;i--){
                if(digits[i]+temp == 10){
                    digits[i] = 0;
                    temp = 1;
                }else{
                    digits[i]=digits[i]+temp;
                    break;
                }
            }
            return digits;
        }
        
        
    }
    

    }


  • 7
    Z
    public class PlusOne {
    public int[] plusOne(int[] digits) {
        for(int i=digits.length-1;i>=0;i--){
            digits[i] =1+digits[i];
            if(digits[i]==10){
                digits[i]=0;
            }
            else{
    
                return digits;
            }
        }
    
        //don't forget over flow case
        int[] ans = new int[digits.length+1];
        ans[0]=1;
        for(int i=0;i<digits.length;i++){
            ans[i+1] = digits[i];
        }
        return ans;
    }
    

    }


  • 0
    S

    actually, the second for loop is not really necessary for over flow case.
    Since there is only one condition which is all digits are 9.


  • 0
    Q

    Why re-invent the wheel? BigInteger is made to handle this sort of stuff, so just use it:

    import java.math.BigInteger;
    public class Solution {
        public int[] plusOne(int[] digits) {
            String val = "";
            for (int d : digits) {
                val += d;
            }
            String result = new BigInteger(val).add(BigInteger.ONE).toString();
            int[] toReturn = new int[result.length()];
            for (int index = 0; index < result.length(); index++) {
                toReturn[index] = result.charAt(index) - '0';
            }
            return toReturn;
        }
    }

  • 0
    Y
     public int[] plusOne(int[] digits) {
        int n=digits.length;
        int carry=-1;
        for(int i=n-1;i>=0;i--){          //check the carry bit
            if(digits[i]!=9){
                carry=i;
                break;
            }
        }
        if(carry==-1){
            int[] result=new int[n+1];
            result[0]=1;
            for(int j=1;j<n+1;j++)
                result[j]=0;
            return result;
        }
        else{
            int[] result=new int[n];
            for(int i=n-1;i>carry;i--)
                result[i]=0;
            result[carry]=digits[carry]+1;
            for(int i=carry-1;i>=0;i--)
                result[i]=digits[i];
            return result;
        }
    } 
    

  • 0
    1
    This post is deleted!

  • 0
    S

    Thanks for your sharing. But here is English only.


  • 0
    J

    the second loop is still needed. it will execute only when digits is not returned in the first loop, which is the case of 9999


  • 0
    B

    The second for loop could be omitted, due to the dirty secret that integers in the new array will be initialized as 0 in Java.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.