Skip to content

Commit 9370f57

Browse files
committed
feat: add typescript solution to lc problem: No.2243
No.2243.Calculate Digit Sum of a String
1 parent 13a7f8d commit 9370f57

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

solution/2200-2299/2243.Calculate Digit Sum of a String/README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@ s 变为 "0" + "0" + "0" = "000" ,其长度等于 k ,所以返回 "000" 。
8080
### **TypeScript**
8181

8282
```ts
83-
83+
function digitSum(s: string, k: number): string {
84+
let ans = [];
85+
while (s.length > k) {
86+
for (let i = 0; i < s.length; i += k) {
87+
let cur = s.slice(i, i + k);
88+
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
89+
}
90+
s = ans.join('');
91+
ans = [];
92+
}
93+
return s;
94+
};
8495
```
8596

8697
### **...**

solution/2200-2299/2243.Calculate Digit Sum of a String/README_EN.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,18 @@ s becomes &quot;0&quot; + &quot;0&quot; + &quot;0&quot; = &quot;000&quot;, whose
7171
### **TypeScript**
7272

7373
```ts
74-
74+
function digitSum(s: string, k: number): string {
75+
let ans = [];
76+
while (s.length > k) {
77+
for (let i = 0; i < s.length; i += k) {
78+
let cur = s.slice(i, i + k);
79+
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
80+
}
81+
s = ans.join('');
82+
ans = [];
83+
}
84+
return s;
85+
};
7586
```
7687

7788
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function digitSum(s: string, k: number): string {
2+
let ans = [];
3+
while (s.length > k) {
4+
for (let i = 0; i < s.length; i += k) {
5+
let cur = s.slice(i, i + k);
6+
ans.push(cur.split('').reduce((a, c) => a + parseInt(c), 0));
7+
}
8+
s = ans.join('');
9+
ans = [];
10+
}
11+
return s;
12+
};

0 commit comments

Comments
 (0)