Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit aa1a162

Browse files
feat: add solutions to lc problems: No.0989,0991 (#4130)
1 parent 5d987ae commit aa1a162

File tree

12 files changed

+183
-236
lines changed

12 files changed

+183
-236
lines changed

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

Lines changed: 44 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ tags:
7171

7272
<!-- solution:start -->
7373

74-
### 方法一
74+
### 方法一:模拟
75+
76+
我们可以从数组的最后一位开始,将数组的每一位与 $k$ 相加,然后将 $k$ 除以 10ドル,ドル并将余数作为当前位的值,将商作为进位。一直循环,直到数组遍历完并且 $k = 0$。最后将答案数组反转即可。
77+
78+
时间复杂度 $O(n),ドル其中 $n$ 表示 $\textit{num}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
7579

7680
<!-- tabs:start -->
7781

@@ -80,13 +84,12 @@ tags:
8084
```python
8185
class Solution:
8286
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
83-
i, carry = len(num) - 1, 0
8487
ans = []
85-
while i >=0or k or carry:
86-
carry += (0if i <0else num[i]) + (k %10)
87-
carry, v =divmod(carry, 10)
88-
ans.append(v)
89-
k //=10
88+
i =len(num) -1
89+
while i >=0or k:
90+
k +=0if i <0else num[i]
91+
k, x =divmod(k, 10)
92+
ans.append(x)
9093
i -= 1
9194
return ans[::-1]
9295
```
@@ -96,14 +99,13 @@ class Solution:
9699
```java
97100
class Solution {
98101
public List<Integer> addToArrayForm(int[] num, int k) {
99-
int i = num.length - 1, carry = 0;
100-
LinkedList<Integer> ans = new LinkedList<>();
101-
while (i >= 0 || k > 0 || carry > 0) {
102-
carry += (i < 0 ? 0 : num[i--]) + k % 10;
103-
ans.addFirst(carry % 10);
104-
carry /= 10;
102+
List<Integer> ans = new ArrayList<>();
103+
for (int i = num.length - 1; i >= 0 || k > 0; --i) {
104+
k += (i >= 0 ? num[i] : 0);
105+
ans.add(k % 10);
105106
k /= 10;
106107
}
108+
Collections.reverse(ans);
107109
return ans;
108110
}
109111
}
@@ -115,15 +117,13 @@ class Solution {
115117
class Solution {
116118
public:
117119
vector<int> addToArrayForm(vector<int>& num, int k) {
118-
int i = num.size() - 1, carry = 0;
119120
vector<int> ans;
120-
for (; i >= 0 || k || carry; --i) {
121-
carry += (i < 0 ? 0 : num[i]) + k % 10;
122-
ans.push_back(carry % 10);
123-
carry /= 10;
121+
for (int i = num.size() - 1; i >= 0 || k > 0; --i) {
122+
k += (i >= 0 ? num[i] : 0);
123+
ans.push_back(k % 10);
124124
k /= 10;
125125
}
126-
reverse(ans.begin(), ans.end());
126+
ranges::reverse(ans);
127127
return ans;
128128
}
129129
};
@@ -132,92 +132,54 @@ public:
132132
#### Go
133133
134134
```go
135-
func addToArrayForm(num []int, k int) []int {
136-
i, carry := len(num)-1, 0
137-
ans := []int{}
138-
for ; i >= 0 || k > 0 || carry > 0; i-- {
135+
func addToArrayForm(num []int, k int) (ans []int) {
136+
for i := len(num) - 1; i >= 0 || k > 0; i-- {
139137
if i >= 0 {
140-
carry += num[i]
138+
k += num[i]
141139
}
142-
carry += k % 10
143-
ans = append(ans, carry%10)
144-
carry /= 10
140+
ans = append(ans, k%10)
145141
k /= 10
146142
}
147-
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
148-
ans[i], ans[j] = ans[j], ans[i]
149-
}
150-
return ans
143+
slices.Reverse(ans)
144+
return
151145
}
152146
```
153147

154148
#### TypeScript
155149

156150
```ts
157151
function addToArrayForm(num: number[], k: number): number[] {
158-
let arr2 = [...String(k)].map(Number);
159-
let ans = [];
160-
let sum = 0;
161-
while (num.length || arr2.length || sum) {
162-
let a = num.pop() || 0,
163-
b = arr2.pop() || 0;
164-
sum += a + b;
165-
ans.unshift(sum % 10);
166-
sum = Math.floor(sum / 10);
152+
const ans: number[] = [];
153+
for (let i = num.length - 1; i >= 0 || k > 0; --i) {
154+
k += i >= 0 ? num[i] : 0;
155+
ans.push(k % 10);
156+
k = Math.floor(k / 10);
167157
}
168-
return ans;
158+
return ans.reverse();
169159
}
170160
```
171161

172162
#### Rust
173163

174164
```rust
175165
impl Solution {
176-
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
177-
let n = num.len();
178-
let mut res = vec![];
179-
let mut i = 0;
180-
let mut sum = 0;
181-
while i < n || sum != 0 || k != 0 {
182-
sum += num.get(n - i - 1).unwrap_or(&0);
183-
sum += k % 10;
184-
res.push(sum % 10);
185-
186-
i += 1;
166+
pub fn add_to_array_form(num: Vec<i32>, k: i32) -> Vec<i32> {
167+
let mut ans = Vec::new();
168+
let mut k = k;
169+
let mut i = num.len() as i32 - 1;
170+
171+
while i >= 0 || k > 0 {
172+
if i >= 0 {
173+
k += num[i as usize];
174+
}
175+
ans.push(k % 10);
187176
k /= 10;
188-
sum/=10;
177+
i-=1;
189178
}
190-
res.reverse();
191-
res
192-
}
193-
}
194-
```
195-
196-
<!-- tabs:end -->
197-
198-
<!-- solution:end -->
199179

200-
<!-- solution:start -->
201-
202-
### 方法二
203-
204-
<!-- tabs:start -->
205-
206-
#### TypeScript
207-
208-
```ts
209-
function addToArrayForm(num: number[], k: number): number[] {
210-
const n = num.length;
211-
const res = [];
212-
let sum = 0;
213-
for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
214-
sum += num[n - i - 1] ?? 0;
215-
sum += k % 10;
216-
res.push(sum % 10);
217-
k = Math.floor(k / 10);
218-
sum = Math.floor(sum / 10);
180+
ans.reverse();
181+
ans
219182
}
220-
return res.reverse();
221183
}
222184
```
223185

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

Lines changed: 44 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ tags:
6666

6767
<!-- solution:start -->
6868

69-
### Solution 1
69+
### Solution 1: Simulation
70+
71+
We can start from the last digit of the array and add each digit of the array to $k$. Then, divide $k$ by 10ドル,ドル and use the remainder as the current digit's value, with the quotient as the carry. Continue this process until the array is fully traversed and $k = 0$. Finally, reverse the answer array.
72+
73+
The time complexity is $O(n),ドル where $n$ is the length of $\textit{num}$. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
7074

7175
<!-- tabs:start -->
7276

@@ -75,13 +79,12 @@ tags:
7579
```python
7680
class Solution:
7781
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
78-
i, carry = len(num) - 1, 0
7982
ans = []
80-
while i >=0or k or carry:
81-
carry += (0if i <0else num[i]) + (k %10)
82-
carry, v =divmod(carry, 10)
83-
ans.append(v)
84-
k //=10
83+
i =len(num) -1
84+
while i >=0or k:
85+
k +=0if i <0else num[i]
86+
k, x =divmod(k, 10)
87+
ans.append(x)
8588
i -= 1
8689
return ans[::-1]
8790
```
@@ -91,14 +94,13 @@ class Solution:
9194
```java
9295
class Solution {
9396
public List<Integer> addToArrayForm(int[] num, int k) {
94-
int i = num.length - 1, carry = 0;
95-
LinkedList<Integer> ans = new LinkedList<>();
96-
while (i >= 0 || k > 0 || carry > 0) {
97-
carry += (i < 0 ? 0 : num[i--]) + k % 10;
98-
ans.addFirst(carry % 10);
99-
carry /= 10;
97+
List<Integer> ans = new ArrayList<>();
98+
for (int i = num.length - 1; i >= 0 || k > 0; --i) {
99+
k += (i >= 0 ? num[i] : 0);
100+
ans.add(k % 10);
100101
k /= 10;
101102
}
103+
Collections.reverse(ans);
102104
return ans;
103105
}
104106
}
@@ -110,15 +112,13 @@ class Solution {
110112
class Solution {
111113
public:
112114
vector<int> addToArrayForm(vector<int>& num, int k) {
113-
int i = num.size() - 1, carry = 0;
114115
vector<int> ans;
115-
for (; i >= 0 || k || carry; --i) {
116-
carry += (i < 0 ? 0 : num[i]) + k % 10;
117-
ans.push_back(carry % 10);
118-
carry /= 10;
116+
for (int i = num.size() - 1; i >= 0 || k > 0; --i) {
117+
k += (i >= 0 ? num[i] : 0);
118+
ans.push_back(k % 10);
119119
k /= 10;
120120
}
121-
reverse(ans.begin(), ans.end());
121+
ranges::reverse(ans);
122122
return ans;
123123
}
124124
};
@@ -127,92 +127,54 @@ public:
127127
#### Go
128128
129129
```go
130-
func addToArrayForm(num []int, k int) []int {
131-
i, carry := len(num)-1, 0
132-
ans := []int{}
133-
for ; i >= 0 || k > 0 || carry > 0; i-- {
130+
func addToArrayForm(num []int, k int) (ans []int) {
131+
for i := len(num) - 1; i >= 0 || k > 0; i-- {
134132
if i >= 0 {
135-
carry += num[i]
133+
k += num[i]
136134
}
137-
carry += k % 10
138-
ans = append(ans, carry%10)
139-
carry /= 10
135+
ans = append(ans, k%10)
140136
k /= 10
141137
}
142-
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
143-
ans[i], ans[j] = ans[j], ans[i]
144-
}
145-
return ans
138+
slices.Reverse(ans)
139+
return
146140
}
147141
```
148142

149143
#### TypeScript
150144

151145
```ts
152146
function addToArrayForm(num: number[], k: number): number[] {
153-
let arr2 = [...String(k)].map(Number);
154-
let ans = [];
155-
let sum = 0;
156-
while (num.length || arr2.length || sum) {
157-
let a = num.pop() || 0,
158-
b = arr2.pop() || 0;
159-
sum += a + b;
160-
ans.unshift(sum % 10);
161-
sum = Math.floor(sum / 10);
147+
const ans: number[] = [];
148+
for (let i = num.length - 1; i >= 0 || k > 0; --i) {
149+
k += i >= 0 ? num[i] : 0;
150+
ans.push(k % 10);
151+
k = Math.floor(k / 10);
162152
}
163-
return ans;
153+
return ans.reverse();
164154
}
165155
```
166156

167157
#### Rust
168158

169159
```rust
170160
impl Solution {
171-
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
172-
let n = num.len();
173-
let mut res = vec![];
174-
let mut i = 0;
175-
let mut sum = 0;
176-
while i < n || sum != 0 || k != 0 {
177-
sum += num.get(n - i - 1).unwrap_or(&0);
178-
sum += k % 10;
179-
res.push(sum % 10);
180-
181-
i += 1;
161+
pub fn add_to_array_form(num: Vec<i32>, k: i32) -> Vec<i32> {
162+
let mut ans = Vec::new();
163+
let mut k = k;
164+
let mut i = num.len() as i32 - 1;
165+
166+
while i >= 0 || k > 0 {
167+
if i >= 0 {
168+
k += num[i as usize];
169+
}
170+
ans.push(k % 10);
182171
k /= 10;
183-
sum/=10;
172+
i-=1;
184173
}
185-
res.reverse();
186-
res
187-
}
188-
}
189-
```
190-
191-
<!-- tabs:end -->
192-
193-
<!-- solution:end -->
194174

195-
<!-- solution:start -->
196-
197-
### Solution 2
198-
199-
<!-- tabs:start -->
200-
201-
#### TypeScript
202-
203-
```ts
204-
function addToArrayForm(num: number[], k: number): number[] {
205-
const n = num.length;
206-
const res = [];
207-
let sum = 0;
208-
for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
209-
sum += num[n - i - 1] ?? 0;
210-
sum += k % 10;
211-
res.push(sum % 10);
212-
k = Math.floor(k / 10);
213-
sum = Math.floor(sum / 10);
175+
ans.reverse();
176+
ans
214177
}
215-
return res.reverse();
216178
}
217179
```
218180

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /