Leetcode 66 plus one Solution in Java | Hindi Coding Community

0

 


You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer. The digits are ordered from most significant to least significant in left-to-right order. The large integer does not contain any leading 0's.

Increment the large integer by one and return the resulting array of digits.



class Solution {
public int[] plusOne(int[] digits) {
int length = digits.length;
List<Integer> list = new ArrayList<>();
int carry = 1;
for(int i=length-1;i>=0;i--){
int sum = digits[i]+carry;
carry = sum > 9 ? 1 : 0;
digits[i] = sum % 10;
}
if(carry!=1){
return digits;
}
int[] newArray = new int[length+1];
newArray[0]=carry;
System.arraycopy(digits, 0, newArray, 1, length);
return newArray;
}
}



Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !