Java Solution Using ArrayList


  • 0
    E
    public class Solution {
        public int[] plusOne(int[] digits) {
            ArrayList<Integer>arrlist=new ArrayList<Integer>();
            int carr=0;
            for(int i=digits.length-1;i>=0;i--){
                int sum=0;
                if(i==(digits.length-1)){
                    sum=digits[i]+1;
                }
                else{
                    sum=digits[i]+carr;
                    carr=0;
                }
                if(sum>=10){
                    carr=sum/10;
                    sum%=10;
                }
                arrlist.add(sum);
            }
            if(carr==1){
                arrlist.add(1);
            }
            //Collections.reverse(arrlist);
            int arr[]=new int[arrlist.size()];
            for(int i=0;i<arrlist.size();i++){
                arr[i]=arrlist.get(arrlist.size()-i-1);
            }
            return arr;
        }
    }
    

Log in to reply
 

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