leetcode 66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
Input:
Output:
Explanation: The array represents the integer 123.
Example 2:
Input:
Output:
Explanation: The array represents the integer 4321.
class Solution {
public int[] plusOne(int[] digits) {
int i = digits.length - 1;
if(digits< 9){
digits = digits + 1;
return digits;
}
while(digits == 9){
digits = 0;
if(i > 0){
i--;
}
}
if(i == 0 && digits == 0){
digits = digits + 1;
int[] result = new int;
int j;
for(j = 0; j< digits.length ; j++){
result = digits;
}
result = 0;
return result;
}
if(i == 0){
digits = digits + 1;
return digits;
}
digits = digits + 1;
return digits;
}
}
页:
[1]