Skip to content

Commit

Permalink
feat: update solutions to lc problems
Browse files Browse the repository at this point in the history
* No.0415.Add Strings
* No.0989.Add to Array-Form of Integer
  • Loading branch information
yanglbme committed May 3, 2022
1 parent 898db48 commit 832a5a3
Show file tree
Hide file tree
Showing 15 changed files with 230 additions and 139 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
- [找到需要补充粉笔的学生编号](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README.md) - 二分查找
- [可移除字符的最大数目](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README.md) - 二分查找
- [排序数组](/solution/0900-0999/0912.Sort%20an%20Array/README.md) - 快速排序、归并排序
- [字符串相加](/solution/0400-0499/0415.Add%20Strings/README.md) - 高精度加法
<!-- 排序算法、待补充 -->

### 2. 搜索
Expand Down
1 change: 1 addition & 0 deletions README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Complete solutions to [LeetCode](https://leetcode.com/problemset/all/), [LCOF](h
- [Find the Student that Will Replace the Chalk](/solution/1800-1899/1894.Find%20the%20Student%20that%20Will%20Replace%20the%20Chalk/README_EN.md) - Binary search
- [Maximum Number of Removable Characters](/solution/1800-1899/1898.Maximum%20Number%20of%20Removable%20Characters/README_EN.md) - Binary search
- [Sort an Array](/solution/0900-0999/0912.Sort%20an%20Array/README_EN.md) - Quick Sort, Merge Sort
- [Add Strings](/solution/0400-0499/0415.Add%20Strings/README_EN.md) - Addition of large numbers

### 2. Search

Expand Down
43 changes: 18 additions & 25 deletions solution/0400-0499/0415.Add Strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@
```python
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
i, j, carry = len(num1) - 1, len(num2) - 1, 0
ans = []
i, j = len(num1) - 1, len(num2) - 1
carry = 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
ans.append(str(carry % 10))
carry //= 10
carry, v = divmod(carry, 10)
ans.append(str(v))
i, j = i - 1, j - 1
return ''.join(ans[::-1])
```
Expand All @@ -76,11 +75,10 @@ class Solution:
```java
class Solution {
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1;
int carry = 0;
StringBuilder ans = new StringBuilder();
while (i >= 0 || j >= 0 || carry > 0) {
carry += (i < 0 ? 0 : num1.charAt(i--) - '0') + (j < 0 ? 0 : num2.charAt(j--) - '0');
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
for (; i >= 0 || j >= 0 || carry > 0; --i, --j) {
carry += (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
ans.append(carry % 10);
carry /= 10;
}
Expand All @@ -99,18 +97,14 @@ class Solution {
*/
var addStrings = function (num1, num2) {
let ans = [];
for (
let i = num1.length - 1, j = num2.length - 1, sum = 0;
i >= 0 || j >= 0 || sum > 0;
i--, j--
) {
const a = i >= 0 ? parseInt(num1.charAt(i), 10) : 0;
const b = j >= 0 ? parseInt(num2.charAt(j), 10) : 0;
sum += a + b;
ans.unshift(sum % 10);
sum = Math.floor(sum / 10);
let [i, j, carry] = [num1.length - 1, num2.length - 1, 0];
for (; i >= 0 || j >= 0 || carry; --i, --j) {
carry += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
carry += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
ans.push(carry % 10);
carry = Math.floor(carry / 10);
}
return ans.join('');
return ans.reverse().join('');
};
```

Expand All @@ -120,12 +114,11 @@ var addStrings = function (num1, num2) {
class Solution {
public:
string addStrings(string num1, string num2) {
int i = num1.size() - 1, j = num2.size() - 1;
int carry = 0;
string ans;
while (i >= 0 || j >= 0 || carry)
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
for (; i >= 0 || j >= 0 || carry; --i, --j)
{
carry += (i < 0 ? 0 : num1[i--] - '0') + (j < 0 ? 0 : num2[j--] - '0');
carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0');
ans += to_string(carry % 10);
carry /= 10;
}
Expand All @@ -139,9 +132,9 @@ public:
```go
func addStrings(num1 string, num2 string) string {
carry := 0
ans := ""
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
i, j, carry := len(num1)-1, len(num2)-1, 0
for ; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
if i >= 0 {
carry += int(num1[i] - '0')
}
Expand Down
43 changes: 18 additions & 25 deletions solution/0400-0499/0415.Add Strings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@
```python
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
i, j, carry = len(num1) - 1, len(num2) - 1, 0
ans = []
i, j = len(num1) - 1, len(num2) - 1
carry = 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
ans.append(str(carry % 10))
carry //= 10
carry, v = divmod(carry, 10)
ans.append(str(v))
i, j = i - 1, j - 1
return ''.join(ans[::-1])
```
Expand All @@ -64,11 +63,10 @@ class Solution:
```java
class Solution {
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1;
int carry = 0;
StringBuilder ans = new StringBuilder();
while (i >= 0 || j >= 0 || carry > 0) {
carry += (i < 0 ? 0 : num1.charAt(i--) - '0') + (j < 0 ? 0 : num2.charAt(j--) - '0');
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
for (; i >= 0 || j >= 0 || carry > 0; --i, --j) {
carry += (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
ans.append(carry % 10);
carry /= 10;
}
Expand All @@ -87,18 +85,14 @@ class Solution {
*/
var addStrings = function (num1, num2) {
let ans = [];
for (
let i = num1.length - 1, j = num2.length - 1, sum = 0;
i >= 0 || j >= 0 || sum > 0;
i--, j--
) {
const a = i >= 0 ? parseInt(num1.charAt(i), 10) : 0;
const b = j >= 0 ? parseInt(num2.charAt(j), 10) : 0;
sum += a + b;
ans.unshift(sum % 10);
sum = Math.floor(sum / 10);
let [i, j, carry] = [num1.length - 1, num2.length - 1, 0];
for (; i >= 0 || j >= 0 || carry; --i, --j) {
carry += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
carry += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
ans.push(carry % 10);
carry = Math.floor(carry / 10);
}
return ans.join('');
return ans.reverse().join('');
};
```

Expand All @@ -108,12 +102,11 @@ var addStrings = function (num1, num2) {
class Solution {
public:
string addStrings(string num1, string num2) {
int i = num1.size() - 1, j = num2.size() - 1;
int carry = 0;
string ans;
while (i >= 0 || j >= 0 || carry)
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
for (; i >= 0 || j >= 0 || carry; --i, --j)
{
carry += (i < 0 ? 0 : num1[i--] - '0') + (j < 0 ? 0 : num2[j--] - '0');
carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0');
ans += to_string(carry % 10);
carry /= 10;
}
Expand All @@ -127,9 +120,9 @@ public:
```go
func addStrings(num1 string, num2 string) string {
carry := 0
ans := ""
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
i, j, carry := len(num1)-1, len(num2)-1, 0
for ; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
if i >= 0 {
carry += int(num1[i] - '0')
}
Expand Down
7 changes: 3 additions & 4 deletions solution/0400-0499/0415.Add Strings/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class Solution {
public:
string addStrings(string num1, string num2) {
int i = num1.size() - 1, j = num2.size() - 1;
int carry = 0;
string ans;
while (i >= 0 || j >= 0 || carry)
int i = num1.size() - 1, j = num2.size() - 1, carry = 0;
for (; i >= 0 || j >= 0 || carry; --i, --j)
{
carry += (i < 0 ? 0 : num1[i--] - '0') + (j < 0 ? 0 : num2[j--] - '0');
carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0');
ans += to_string(carry % 10);
carry /= 10;
}
Expand Down
4 changes: 2 additions & 2 deletions solution/0400-0499/0415.Add Strings/Solution.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
func addStrings(num1 string, num2 string) string {
carry := 0
ans := ""
for i, j := len(num1)-1, len(num2)-1; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
i, j, carry := len(num1)-1, len(num2)-1, 0
for ; i >= 0 || j >= 0 || carry != 0; i, j = i-1, j-1 {
if i >= 0 {
carry += int(num1[i] - '0')
}
Expand Down
7 changes: 3 additions & 4 deletions solution/0400-0499/0415.Add Strings/Solution.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
class Solution {
public String addStrings(String num1, String num2) {
int i = num1.length() - 1, j = num2.length() - 1;
int carry = 0;
StringBuilder ans = new StringBuilder();
while (i >= 0 || j >= 0 || carry > 0) {
carry += (i < 0 ? 0 : num1.charAt(i--) - '0') + (j < 0 ? 0 : num2.charAt(j--) - '0');
int i = num1.length() - 1, j = num2.length() - 1, carry = 0;
for (; i >= 0 || j >= 0 || carry > 0; --i, --j) {
carry += (i < 0 ? 0 : num1.charAt(i) - '0') + (j < 0 ? 0 : num2.charAt(j) - '0');
ans.append(carry % 10);
carry /= 10;
}
Expand Down
18 changes: 7 additions & 11 deletions solution/0400-0499/0415.Add Strings/Solution.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,12 @@
*/
var addStrings = function (num1, num2) {
let ans = [];
for (
let i = num1.length - 1, j = num2.length - 1, sum = 0;
i >= 0 || j >= 0 || sum > 0;
i--, j--
) {
const a = i >= 0 ? parseInt(num1.charAt(i), 10) : 0;
const b = j >= 0 ? parseInt(num2.charAt(j), 10) : 0;
sum += a + b;
ans.unshift(sum % 10);
sum = Math.floor(sum / 10);
let [i, j, carry] = [num1.length - 1, num2.length - 1, 0];
for (; i >= 0 || j >= 0 || carry; --i, --j) {
carry += i < 0 ? 0 : parseInt(num1.charAt(i), 10);
carry += j < 0 ? 0 : parseInt(num2.charAt(j), 10);
ans.push(carry % 10);
carry = Math.floor(carry / 10);
}
return ans.join('');
return ans.reverse().join('');
};
7 changes: 3 additions & 4 deletions solution/0400-0499/0415.Add Strings/Solution.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
class Solution:
def addStrings(self, num1: str, num2: str) -> str:
i, j, carry = len(num1) - 1, len(num2) - 1, 0
ans = []
i, j = len(num1) - 1, len(num2) - 1
carry = 0
while i >= 0 or j >= 0 or carry:
carry += (0 if i < 0 else int(num1[i])) + (0 if j < 0 else int(num2[j]))
ans.append(str(carry % 10))
carry //= 10
carry, v = divmod(carry, 10)
ans.append(str(v))
i, j = i - 1, j - 1
return ''.join(ans[::-1])
84 changes: 62 additions & 22 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 @@ -58,7 +58,7 @@

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

数组从尾到头遍历,分别与 `K` 中的每一位相加,进位保存在 `carry` 中,不进位和则添加到结果列表中。最后逆序结果列表即可。
数组从尾到头遍历,分别与 `k` 中的每一位相加,进位保存在 `carry` 中,不进位和则添加到结果列表中。最后逆序结果列表即可。

<!-- tabs:start -->

Expand All @@ -68,16 +68,16 @@

```python
class Solution:
def addToArrayForm(self, A: List[int], K: int) -> List[int]:
n = len(A) - 1
carry, res = 0, []
while n >= 0 or K != 0 or carry != 0:
carry += (0 if n < 0 else A[n]) + (K % 10)
res.append(carry % 10)
K //= 10
carry //= 10
n -= 1
return res[::-1]
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 -= 1
return ans[::-1]
```

### **Java**
Expand All @@ -86,19 +86,16 @@ class Solution:

```java
class Solution {
public List<Integer> addToArrayForm(int[] A, int K) {
int n = A.length - 1;
List<Integer> res = new ArrayList<>();
int carry = 0;
while (n >= 0 || K != 0 || carry != 0) {
carry += (n < 0 ? 0 : A[n]) + (K % 10);
res.add(carry % 10);
K /= 10;
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;
--n;
k /= 10;
}
Collections.reverse(res);
return res;
return ans;
}
}
```
Expand Down Expand Up @@ -161,6 +158,49 @@ impl Solution {
}
```

### **C++**

```cpp
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;
k /= 10;
}
reverse(ans.begin(), ans.end());
return ans;
}
};
```
### **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-- {
if i >= 0 {
carry += num[i]
}
carry += k % 10
ans = append(ans, carry%10)
carry /= 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
}
```

### **...**

```
Expand Down
Loading

0 comments on commit 832a5a3

Please sign in to comment.