Skip to content

Commit 5c6697b

Browse files
committed
feat: update leetcode solutions: No.0989. Add to Array-Form of Integer
1 parent 76fcb6b commit 5c6697b

File tree

4 files changed

+75
-15
lines changed

4 files changed

+75
-15
lines changed

solution/0900-0999/0989.Add to Array-Form of Integer/README.md

+29-2
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,49 @@
5757

5858
<!-- 这里可写通用的实现逻辑 -->
5959

60+
数组从尾到头遍历,分别与 `K` 中的每一位相加,进位保存在 `carry` 中,不进位和则添加到结果列表中。最后逆序结果列表即可。
61+
6062
<!-- tabs:start -->
6163

6264
### **Python3**
6365

6466
<!-- 这里可写当前语言的特殊实现逻辑 -->
6567

6668
```python
67-
69+
class Solution:
70+
def addToArrayForm(self, A: List[int], K: int) -> List[int]:
71+
n = len(A) - 1
72+
carry, res = 0, []
73+
while n >= 0 or K != 0 or carry != 0:
74+
carry += (0 if n < 0 else A[n]) + (K % 10)
75+
res.append(carry % 10)
76+
K //= 10
77+
carry //= 10
78+
n -= 1
79+
return res[::-1]
6880
```
6981

7082
### **Java**
7183

7284
<!-- 这里可写当前语言的特殊实现逻辑 -->
7385

7486
```java
75-
87+
class Solution {
88+
public List<Integer> addToArrayForm(int[] A, int K) {
89+
int n = A.length - 1;
90+
List<Integer> res = new ArrayList<>();
91+
int carry = 0;
92+
while (n >= 0 || K != 0 || carry != 0) {
93+
carry += (n < 0 ? 0 : A[n]) + (K % 10);
94+
res.add(carry % 10);
95+
K /= 10;
96+
carry /= 10;
97+
--n;
98+
}
99+
Collections.reverse(res);
100+
return res;
101+
}
102+
}
76103
```
77104

78105
### **...**

solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,38 @@
9696
### **Python3**
9797

9898
```python
99-
99+
class Solution:
100+
def addToArrayForm(self, A: List[int], K: int) -> List[int]:
101+
n = len(A) - 1
102+
carry, res = 0, []
103+
while n >= 0 or K != 0 or carry != 0:
104+
carry += (0 if n < 0 else A[n]) + (K % 10)
105+
res.append(carry % 10)
106+
K //= 10
107+
carry //= 10
108+
n -= 1
109+
return res[::-1]
100110
```
101111

102112
### **Java**
103113

104114
```java
105-
115+
class Solution {
116+
public List<Integer> addToArrayForm(int[] A, int K) {
117+
int n = A.length - 1;
118+
List<Integer> res = new ArrayList<>();
119+
int carry = 0;
120+
while (n >= 0 || K != 0 || carry != 0) {
121+
carry += (n < 0 ? 0 : A[n]) + (K % 10);
122+
res.add(carry % 10);
123+
K /= 10;
124+
carry /= 10;
125+
--n;
126+
}
127+
Collections.reverse(res);
128+
return res;
129+
}
130+
}
106131
```
107132

108133
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
class Solution {
22
public List<Integer> addToArrayForm(int[] A, int K) {
3-
for (int i = A.length - 1; i >= 0 && K != 0; --i) {
4-
K += A[i];
5-
A[i] = K % 10;
6-
K /= 10;
7-
}
3+
int n = A.length - 1;
84
List<Integer> res = new ArrayList<>();
9-
while (K != 0) {
10-
res.add(K % 10);
5+
int carry = 0;
6+
while (n >= 0 || K != 0 || carry != 0) {
7+
carry += (n < 0 ? 0 : A[n]) + (K % 10);
8+
res.add(carry % 10);
119
K /= 10;
10+
carry /= 10;
11+
--n;
1212
}
1313
Collections.reverse(res);
14-
for (int a : A) {
15-
res.add(a);
16-
}
1714
return res;
1815
}
19-
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def addToArrayForm(self, A: List[int], K: int) -> List[int]:
3+
n = len(A) - 1
4+
carry, res = 0, []
5+
while n >= 0 or K != 0 or carry != 0:
6+
carry += (0 if n < 0 else A[n]) + (K % 10)
7+
res.append(carry % 10)
8+
K //= 10
9+
carry //= 10
10+
n -= 1
11+
return res[::-1]

0 commit comments

Comments
 (0)