Skip to content

Commit 425db88

Browse files
committed
feat: add solutions to lc problem: No.1945
No.1945.Sum of Digits of String After Convert
1 parent 72adec5 commit 425db88

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

solution/1900-1999/1945.Sum of Digits of String After Convert/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,46 @@ func getLucky(s string, k int) int {
146146
}
147147
```
148148

149+
### **TypeScript**
150+
151+
```ts
152+
function getLucky(s: string, k: number): number {
153+
let ans = '';
154+
for (const c of s) {
155+
ans += c.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
156+
}
157+
for (let i = 0; i < k; i++) {
158+
let t = 0;
159+
for (const v of ans) {
160+
t += Number(v);
161+
}
162+
ans = `${t}`;
163+
}
164+
return Number(ans);
165+
}
166+
```
167+
168+
### **Rust**
169+
170+
```rust
171+
impl Solution {
172+
pub fn get_lucky(s: String, k: i32) -> i32 {
173+
let mut ans = String::new();
174+
for c in s.as_bytes() {
175+
ans.push_str(&(c - b'a' + 1).to_string());
176+
}
177+
for _ in 0..k {
178+
let mut t = 0;
179+
for c in ans.as_bytes() {
180+
t += (c - b'0') as i32;
181+
}
182+
ans = t.to_string();
183+
}
184+
ans.parse().unwrap()
185+
}
186+
}
187+
```
188+
149189
### **...**
150190

151191
```

solution/1900-1999/1945.Sum of Digits of String After Convert/README_EN.md

+40
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,46 @@ func getLucky(s string, k int) int {
137137
}
138138
```
139139

140+
### **TypeScript**
141+
142+
```ts
143+
function getLucky(s: string, k: number): number {
144+
let ans = '';
145+
for (const c of s) {
146+
ans += c.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
147+
}
148+
for (let i = 0; i < k; i++) {
149+
let t = 0;
150+
for (const v of ans) {
151+
t += Number(v);
152+
}
153+
ans = `${t}`;
154+
}
155+
return Number(ans);
156+
}
157+
```
158+
159+
### **Rust**
160+
161+
```rust
162+
impl Solution {
163+
pub fn get_lucky(s: String, k: i32) -> i32 {
164+
let mut ans = String::new();
165+
for c in s.as_bytes() {
166+
ans.push_str(&(c - b'a' + 1).to_string());
167+
}
168+
for _ in 0..k {
169+
let mut t = 0;
170+
for c in ans.as_bytes() {
171+
t += (c - b'0') as i32;
172+
}
173+
ans = t.to_string();
174+
}
175+
ans.parse().unwrap()
176+
}
177+
}
178+
```
179+
140180
### **...**
141181

142182
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
impl Solution {
2+
pub fn get_lucky(s: String, k: i32) -> i32 {
3+
let mut ans = String::new();
4+
for c in s.as_bytes() {
5+
ans.push_str(&(c - b'a' + 1).to_string());
6+
}
7+
for _ in 0..k {
8+
let mut t = 0;
9+
for c in ans.as_bytes() {
10+
t += (c - b'0') as i32;
11+
}
12+
ans = t.to_string();
13+
}
14+
ans.parse().unwrap()
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function getLucky(s: string, k: number): number {
2+
let ans = '';
3+
for (const c of s) {
4+
ans += c.charCodeAt(0) - 'a'.charCodeAt(0) + 1;
5+
}
6+
for (let i = 0; i < k; i++) {
7+
let t = 0;
8+
for (const v of ans) {
9+
t += Number(v);
10+
}
11+
ans = `${t}`;
12+
}
13+
return Number(ans);
14+
}

0 commit comments

Comments
 (0)