Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
Example:
Given [1,2,3] which represents 123, return [1,2,4].
Given [9,9,9] which represents 999, return [1,0,0,0].
My code is not working for the input [9,8,7,6,5,4,3,2,1,0] the output comes as just [9]
Can anyone tell me why?
public class Solution {
/**
* @param digits a number represented as an array of digits
* @return the result
*/
public int[] plusOne(int[] digits) {
// Write your code here
float n = 0;
for(int i = 0; i < digits.length; i++) {
n = n*10 + digits[i];
}
n++;
String s = Float.toString(n);
s = s.substring(0, s.indexOf("."));
int l = s.length();
int result[] = new int[l];
for(int i = 0; i < l; i++) {
result[i] = Integer.parseInt(Character.toString(s.charAt(i)));
}
return result;
}
}