My acceped Java solution


  • 0
    F
    public class Solution {
        public int[] plusOne(int[] digits) {
          if(digits.length == 0){
    			return digits;
    		}
    		
    		int arraySize = digits.length - 1;
    		int carry = 1;
    		
    	if((digits[0] == 9) && (digits[arraySize] == 9)){
    		int[] result = new int[digits.length + 1];
    		result[0] = 1;	
    		return result;
    	}
    	
    for (int i = arraySize; i >= 0; i--) {
    	if(digits[i] == 9){
    		digits[i]= 0;
    	}
    	else if(digits[i] != 9){
    		digits[i] = digits[i] + carry;
    		return digits;
    	}
    }
    	return digits;
    	
    }
    
    }

  • 0
    C

    If the number is not full filled with 9 but only has 9 at the start and the end?
    Like 900009.
    It doesn't need an extra digit. (Or I misunderstand something here...? 2:00 a.m. So sleepy)
    Test examples haven't cover this possible case maybe.


  • 1
    Y

    The middle part is not right, it couldn't deal with situation like {9, 0, 9}:

    if((digits[0] == 9) && (digits[arraySize] == 9)){
        int[] result = new int[digits.length + 1];
        result[0] = 1;  
        return result;
    }
    

    You just need to change a little bit:

    boolean everydigit = true;
    for (int j = 0; j <= leng; j++) {
    	if(digits[j]!=9)
    	    everydigit = false;
    }
    if (everydigit == true) {
    	int[] result = new int[digits.length + 1];
        result[0] = 1;  
        return result;
    }
    

    Hope this could give you some idea :D


Log in to reply
 

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