Share my java solution


  • 0
    X
    public class Solution {
        public int[] plusOne(int[] digits) {
            boolean ifcontinue=true;
            
            for(int i=digits.length-1;i>=0&&ifcontinue;i--){
                digits[i]++;
                if(digits[i]%10==0){
                    ifcontinue=true;
                }
                else{
                    ifcontinue=false;
                }
                digits[i]=digits[i]%10;
            }
            if(ifcontinue){ 
                int[] result=new int[digits.length+1];
                result[0]=1;
                System.arraycopy(digits,0,result,1,digits.length);
                return result;
            }
            return digits;
        }
    }

  • 0
    B

    public int[] plusOne(int[] digits) {
    int i=digits.length;
    int count=0;
    for (int j=0;j<i;j++){
    if (digits[j]!=9)
    break;
    else count++;
    }
    if (count==i)
    {
    int[] result=new int[i+1];
    result[0]=1;
    return result;
    }
    count=0;
    digits[i-1]+=1;
    count=digits[i-1]/10;
    if (count==1)
    digits[i-1]=digits[i-1]%10;
    else return digits;
    for (int j=i-2;j>=0;j--){
    digits[j]+=count;
    count=digits[j]/10;
    if (count==1)
    digits[j]=digits[j]%10;
    else return digits;
    }
    return digits;

    compile error : reach end of file while parsing . Can you give some explaination?


  • 0
    X

    you may need } }.


Log in to reply
 

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