Skip to content

Commit

Permalink
feat: add typescript solution to lc problem: No.0415.Add Strings (doo…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaocchen authored Oct 14, 2021
1 parent 927ea66 commit 06875ce
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
21 changes: 21 additions & 0 deletions solution/0400-0499/0415.Add Strings/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ class Solution {
}
```

### **TypeScript**

```ts
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
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);
}
return ans.join("");
};
```

### **...**

```
Expand Down
21 changes: 21 additions & 0 deletions solution/0400-0499/0415.Add Strings/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ class Solution {
}
```

### **TypeScript**

```ts
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
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);
}
return ans.join("");
};
```

### **...**

```
Expand Down
16 changes: 16 additions & 0 deletions solution/0400-0499/0415.Add Strings/Solution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
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);
}
return ans.join("");
};

0 comments on commit 06875ce

Please sign in to comment.