Java solution, very simple, 0ms


  • 3
    C
        public int[] plusOne(int[] digits) {
    	int carry = 1;
    	for(int i = digits.length - 1; i >= 0; i --){
    		digits[i] += carry;
    		if(digits[i] < 10)
    			break;
    		else{
    			digits[i] = 0;
    			carry = 1;
    		}
    	}
    	if(0 == digits[0]){// has a carry
    		int[] result = new int[1 + digits.length];
    		for(int i = 0; i < digits.length; i ++){
    			result[i + 1] = digits[i];
    		}
    		result[0] = 1;
    		return result;
    	}
    	else{
    		return digits;
    	}
    }

Log in to reply
 

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