Fast and neat one pass java solution


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

Log in to reply
 

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