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 06875ce

Browse files
feat: add typescript solution to lc problem: No.0415.Add Strings (doocs#580)
1 parent 927ea66 commit 06875ce

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

‎solution/0400-0499/0415.Add Strings/README.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,27 @@ class Solution {
6464
}
6565
```
6666

67+
### **TypeScript**
68+
69+
```ts
70+
/**
71+
* @param {string} num1
72+
* @param {string} num2
73+
* @return {string}
74+
*/
75+
var addStrings = function(num1, num2) {
76+
let ans = [];
77+
for (let i = num1.length - 1, j = num2.length - 1, sum = 0; i >= 0 || j >= 0 || sum > 0; i--, j--) {
78+
const a = i >= 0 ? parseInt(num1.charAt(i), 10) : 0;
79+
const b = j >= 0 ? parseInt(num2.charAt(j), 10) : 0;
80+
sum += (a + b);
81+
ans.unshift(sum % 10);
82+
sum = Math.floor(sum / 10);
83+
}
84+
return ans.join("");
85+
};
86+
```
87+
6788
### **...**
6889

6990
```

‎solution/0400-0499/0415.Add Strings/README_EN.md‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,27 @@ class Solution {
7979
}
8080
```
8181

82+
### **TypeScript**
83+
84+
```ts
85+
/**
86+
* @param {string} num1
87+
* @param {string} num2
88+
* @return {string}
89+
*/
90+
var addStrings = function(num1, num2) {
91+
let ans = [];
92+
for (let i = num1.length - 1, j = num2.length - 1, sum = 0; i >= 0 || j >= 0 || sum > 0; i--, j--) {
93+
const a = i >= 0 ? parseInt(num1.charAt(i), 10) : 0;
94+
const b = j >= 0 ? parseInt(num2.charAt(j), 10) : 0;
95+
sum += (a + b);
96+
ans.unshift(sum % 10);
97+
sum = Math.floor(sum / 10);
98+
}
99+
return ans.join("");
100+
};
101+
```
102+
82103
### **...**
83104

84105
```
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {string} num1
3+
* @param {string} num2
4+
* @return {string}
5+
*/
6+
var addStrings = function(num1, num2) {
7+
let ans = [];
8+
for (let i = num1.length - 1, j = num2.length - 1, sum = 0; i >= 0 || j >= 0 || sum > 0; i--, j--) {
9+
const a = i >= 0 ? parseInt(num1.charAt(i), 10) : 0;
10+
const b = j >= 0 ? parseInt(num2.charAt(j), 10) : 0;
11+
sum += (a + b);
12+
ans.unshift(sum % 10);
13+
sum = Math.floor(sum / 10);
14+
}
15+
return ans.join("");
16+
};

0 commit comments

Comments
(0)

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