We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 1284b88 commit 62e7901Copy full SHA for 62e7901
LeetcodeProblems/Algorithms/plusone.java
@@ -1,17 +1,11 @@
1
-class Solution {
2
- public int[] plusOne(int[] digits) {
3
-
4
- for(int i = digits.length-1; i>=0; i--){
5
- if (digits[i]<9){
6
- digits[i]++;
7
- return digits;
8
- }
9
- digits[i]=0;
10
11
12
- digits = new int[digits.length+1];
13
- digits[0] = 1;
14
15
+var plusOne = function(digits) {
+ result=[]; carry=1; n=digits.length-1;
+ for(i=n; i>=0; i--){
+ sum=(carry+digits[i])%10;
+ carry=Math.floor((carry+digits[i])/10);
+ result[n]=sum;
+ n--;
16
}
17
-}
+ if(carry!==0) {result.unshift(carry);}
+ return result;
+};
0 commit comments