solution that is really easy to understand


  • 0
    C
     public int[] plusOne(int[] digits) {
            int j = digits.length-1;
    	int[] result = new int[digits.length+1];
    	boolean[] notation = new boolean[digits.length];
    	while(j>=0) {
    		if(digits[j] == 9) {
    			notation[j] = true;
    			digits[j--] = 0;
    		}else {
    			digits[j]++;
    			break;
    		}
    	}	
    	if(notation[0] == false) {
    		return digits;
    	}else {
    		result[0] = 1;
    		for(int i = 1;i<result.length;i++) {
    			result[i] = digits[i-1];
    		}
    		return result;
    	}		
    }

Log in to reply
 

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