Share My Solution Using While Loop


  • 0
    N
    public class Solution {
        public int[] plusOne(int[] digits) {
            if(digits.length == 0) return digits;
            int i = digits.length-1;
            while(++digits[i]>9){
            	digits[i] = 0;
            	i--;
            	if(i<0){
            		int result[] = new int[digits.length+1];
            		result[0] = 1;
            		return result;
            	}
            }
            return digits;
        }
    }
    
    

Log in to reply
 

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