My Simple Java Solution (Available for binary or other system)


  • 0
    O
    public class Solution {
        public int[] plusOne(int[] digits) {
            if(digits == null || digits.length == 0)
                return digits;
            int flag = 1;//triger
            int base = 10;
            for(int i=digits.length-1; i>=0; i--){
                if((digits[i]=digits[i]+flag) >= base){
                    flag = 1;
                    digits[i] %= base;
                }else{
                    flag = 0;
                }
            }
            if(flag == 1){//if overflow
                digits = new int[digits.length+1];
                digits[0] = 1;
            }
            return digits;
        }
    }
    

Log in to reply
 

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