Accepted Java solution(184ms)


  • 0
    O
    public class Solution {
        public int[] plusOne(int[] digits) {
            List<Integer> list = new ArrayList<Integer>();
    		if (digits.length == 0) {
    			return new int[0];
    		}
    		
    		int x = 0;
    		for(int i = digits.length - 1; i >= 0; i--) {
    			x = i == digits.length - 1 ? digits[i] + 1 : digits[i] + x;
    			list.add((x%10 == 0 ? 0 : x%10));
    			x = x%10 == 0 && x != 0 ? 1 : (x - x%10)/10;
    		}
    		
    		if (x > 0) {
    			list.add(x);
    		}
    		int[] result = new int[list.size()];
    		for(int i = 0; i < list.size(); i++) {
    			result[i] = list.get(list.size() - 1 - i);
    		}
    		
            return result;
        }
    }

Log in to reply
 

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