Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc problems: No.0989,0991 #4130

Merged
merged 1 commit into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 44 additions & 82 deletions solution/0900-0999/0989.Add to Array-Form of Integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:模拟

我们可以从数组的最后一位开始,将数组的每一位与 $k$ 相加,然后将 $k$ 除以 $10$,并将余数作为当前位的值,将商作为进位。一直循环,直到数组遍历完并且 $k = 0$。最后将答案数组反转即可。

时间复杂度 $O(n)$,其中 $n$ 表示 $\textit{num}$ 的长度。忽略答案数组的空间消耗,空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -80,13 +84,12 @@ tags:
```python
class Solution:
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
i, carry = len(num) - 1, 0
ans = []
while i >= 0 or k or carry:
carry += (0 if i < 0 else num[i]) + (k % 10)
carry, v = divmod(carry, 10)
ans.append(v)
k //= 10
i = len(num) - 1
while i >= 0 or k:
k += 0 if i < 0 else num[i]
k, x = divmod(k, 10)
ans.append(x)
i -= 1
return ans[::-1]
```
Expand All @@ -96,14 +99,13 @@ class Solution:
```java
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
int i = num.length - 1, carry = 0;
LinkedList<Integer> ans = new LinkedList<>();
while (i >= 0 || k > 0 || carry > 0) {
carry += (i < 0 ? 0 : num[i--]) + k % 10;
ans.addFirst(carry % 10);
carry /= 10;
List<Integer> ans = new ArrayList<>();
for (int i = num.length - 1; i >= 0 || k > 0; --i) {
k += (i >= 0 ? num[i] : 0);
ans.add(k % 10);
k /= 10;
}
Collections.reverse(ans);
return ans;
}
}
Expand All @@ -115,15 +117,13 @@ class Solution {
class Solution {
public:
vector<int> addToArrayForm(vector<int>& num, int k) {
int i = num.size() - 1, carry = 0;
vector<int> ans;
for (; i >= 0 || k || carry; --i) {
carry += (i < 0 ? 0 : num[i]) + k % 10;
ans.push_back(carry % 10);
carry /= 10;
for (int i = num.size() - 1; i >= 0 || k > 0; --i) {
k += (i >= 0 ? num[i] : 0);
ans.push_back(k % 10);
k /= 10;
}
reverse(ans.begin(), ans.end());
ranges::reverse(ans);
return ans;
}
};
Expand All @@ -132,92 +132,54 @@ public:
#### Go

```go
func addToArrayForm(num []int, k int) []int {
i, carry := len(num)-1, 0
ans := []int{}
for ; i >= 0 || k > 0 || carry > 0; i-- {
func addToArrayForm(num []int, k int) (ans []int) {
for i := len(num) - 1; i >= 0 || k > 0; i-- {
if i >= 0 {
carry += num[i]
k += num[i]
}
carry += k % 10
ans = append(ans, carry%10)
carry /= 10
ans = append(ans, k%10)
k /= 10
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return ans
slices.Reverse(ans)
return
}
```

#### TypeScript

```ts
function addToArrayForm(num: number[], k: number): number[] {
let arr2 = [...String(k)].map(Number);
let ans = [];
let sum = 0;
while (num.length || arr2.length || sum) {
let a = num.pop() || 0,
b = arr2.pop() || 0;
sum += a + b;
ans.unshift(sum % 10);
sum = Math.floor(sum / 10);
const ans: number[] = [];
for (let i = num.length - 1; i >= 0 || k > 0; --i) {
k += i >= 0 ? num[i] : 0;
ans.push(k % 10);
k = Math.floor(k / 10);
}
return ans;
return ans.reverse();
}
```

#### Rust

```rust
impl Solution {
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
let n = num.len();
let mut res = vec![];
let mut i = 0;
let mut sum = 0;
while i < n || sum != 0 || k != 0 {
sum += num.get(n - i - 1).unwrap_or(&0);
sum += k % 10;
res.push(sum % 10);

i += 1;
pub fn add_to_array_form(num: Vec<i32>, k: i32) -> Vec<i32> {
let mut ans = Vec::new();
let mut k = k;
let mut i = num.len() as i32 - 1;

while i >= 0 || k > 0 {
if i >= 0 {
k += num[i as usize];
}
ans.push(k % 10);
k /= 10;
sum /= 10;
i -= 1;
}
res.reverse();
res
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### 方法二

<!-- tabs:start -->

#### TypeScript

```ts
function addToArrayForm(num: number[], k: number): number[] {
const n = num.length;
const res = [];
let sum = 0;
for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
sum += num[n - i - 1] ?? 0;
sum += k % 10;
res.push(sum % 10);
k = Math.floor(k / 10);
sum = Math.floor(sum / 10);
ans.reverse();
ans
}
return res.reverse();
}
```

Expand Down
126 changes: 44 additions & 82 deletions solution/0900-0999/0989.Add to Array-Form of Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Simulation

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.

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)$.

<!-- tabs:start -->

Expand All @@ -75,13 +79,12 @@ tags:
```python
class Solution:
def addToArrayForm(self, num: List[int], k: int) -> List[int]:
i, carry = len(num) - 1, 0
ans = []
while i >= 0 or k or carry:
carry += (0 if i < 0 else num[i]) + (k % 10)
carry, v = divmod(carry, 10)
ans.append(v)
k //= 10
i = len(num) - 1
while i >= 0 or k:
k += 0 if i < 0 else num[i]
k, x = divmod(k, 10)
ans.append(x)
i -= 1
return ans[::-1]
```
Expand All @@ -91,14 +94,13 @@ class Solution:
```java
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
int i = num.length - 1, carry = 0;
LinkedList<Integer> ans = new LinkedList<>();
while (i >= 0 || k > 0 || carry > 0) {
carry += (i < 0 ? 0 : num[i--]) + k % 10;
ans.addFirst(carry % 10);
carry /= 10;
List<Integer> ans = new ArrayList<>();
for (int i = num.length - 1; i >= 0 || k > 0; --i) {
k += (i >= 0 ? num[i] : 0);
ans.add(k % 10);
k /= 10;
}
Collections.reverse(ans);
return ans;
}
}
Expand All @@ -110,15 +112,13 @@ class Solution {
class Solution {
public:
vector<int> addToArrayForm(vector<int>& num, int k) {
int i = num.size() - 1, carry = 0;
vector<int> ans;
for (; i >= 0 || k || carry; --i) {
carry += (i < 0 ? 0 : num[i]) + k % 10;
ans.push_back(carry % 10);
carry /= 10;
for (int i = num.size() - 1; i >= 0 || k > 0; --i) {
k += (i >= 0 ? num[i] : 0);
ans.push_back(k % 10);
k /= 10;
}
reverse(ans.begin(), ans.end());
ranges::reverse(ans);
return ans;
}
};
Expand All @@ -127,92 +127,54 @@ public:
#### Go

```go
func addToArrayForm(num []int, k int) []int {
i, carry := len(num)-1, 0
ans := []int{}
for ; i >= 0 || k > 0 || carry > 0; i-- {
func addToArrayForm(num []int, k int) (ans []int) {
for i := len(num) - 1; i >= 0 || k > 0; i-- {
if i >= 0 {
carry += num[i]
k += num[i]
}
carry += k % 10
ans = append(ans, carry%10)
carry /= 10
ans = append(ans, k%10)
k /= 10
}
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
ans[i], ans[j] = ans[j], ans[i]
}
return ans
slices.Reverse(ans)
return
}
```

#### TypeScript

```ts
function addToArrayForm(num: number[], k: number): number[] {
let arr2 = [...String(k)].map(Number);
let ans = [];
let sum = 0;
while (num.length || arr2.length || sum) {
let a = num.pop() || 0,
b = arr2.pop() || 0;
sum += a + b;
ans.unshift(sum % 10);
sum = Math.floor(sum / 10);
const ans: number[] = [];
for (let i = num.length - 1; i >= 0 || k > 0; --i) {
k += i >= 0 ? num[i] : 0;
ans.push(k % 10);
k = Math.floor(k / 10);
}
return ans;
return ans.reverse();
}
```

#### Rust

```rust
impl Solution {
pub fn add_to_array_form(num: Vec<i32>, mut k: i32) -> Vec<i32> {
let n = num.len();
let mut res = vec![];
let mut i = 0;
let mut sum = 0;
while i < n || sum != 0 || k != 0 {
sum += num.get(n - i - 1).unwrap_or(&0);
sum += k % 10;
res.push(sum % 10);

i += 1;
pub fn add_to_array_form(num: Vec<i32>, k: i32) -> Vec<i32> {
let mut ans = Vec::new();
let mut k = k;
let mut i = num.len() as i32 - 1;

while i >= 0 || k > 0 {
if i >= 0 {
k += num[i as usize];
}
ans.push(k % 10);
k /= 10;
sum /= 10;
i -= 1;
}
res.reverse();
res
}
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### TypeScript

```ts
function addToArrayForm(num: number[], k: number): number[] {
const n = num.length;
const res = [];
let sum = 0;
for (let i = 0; i < n || sum !== 0 || k !== 0; i++) {
sum += num[n - i - 1] ?? 0;
sum += k % 10;
res.push(sum % 10);
k = Math.floor(k / 10);
sum = Math.floor(sum / 10);
ans.reverse();
ans
}
return res.reverse();
}
```

Expand Down
Loading
Loading