[0ms]Solution in Java


  • 3
    T
     public int[] plusOne(int[] digits) {
    	 for (int i = digits.length-1; i >= 0; i--) {
    		if (0 == i && digits[i] == 9) {
    			int[] digits2 = new int[digits.length + 1];
    			digits2[0] = 1;
    			return digits2;
    		}
    		if (digits[i] == 9) {
    			digits[i] = 0;
    			continue;
    		}
    		digits[i] ++;
    		break;
    	}
    	 return digits;
     }

  • 0
    S

    Obviously it is a excellent solution of this problem. But I have a question about the extended version of this question. What if the condition in the question " the array of digits " changed to "the array of numbers"? that is , input [ 3, 4, 6,159] ,we get [ 3,5,2,0]


Log in to reply
 

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