Skip to content

Commit d108c2e

Browse files
committed
feat: add solutions to lc problem: No.1945
No.1945.Sum of Digits of String After Convert
1 parent e95e300 commit d108c2e

File tree

6 files changed

+131
-18
lines changed

6 files changed

+131
-18
lines changed

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

+51-6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959

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

62+
**方法一:模拟**
63+
64+
根据题目描述进行模拟即可。
65+
66+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
67+
6268
<!-- tabs:start -->
6369

6470
### **Python3**
@@ -68,11 +74,9 @@
6874
```python
6975
class Solution:
7076
def getLucky(self, s: str, k: int) -> int:
71-
s = ''.join([str(ord(c) - ord('a') + 1) for c in s])
77+
s = ''.join(str(ord(c) - ord('a') + 1) for c in s)
7278
for _ in range(k):
73-
t = 0
74-
for c in s:
75-
t += ord(c) - ord('0')
79+
t = sum(int(c) for c in s)
7680
s = str(t)
7781
return int(s)
7882
```
@@ -91,8 +95,8 @@ class Solution {
9195
s = sb.toString();
9296
while (k-- > 0) {
9397
int t = 0;
94-
for (int i = 0; i < s.length(); ++i) {
95-
t += s.charAt(i) - '0';
98+
for (char c : s.toCharArray()) {
99+
t += c - '0';
96100
}
97101
s = String.valueOf(t);
98102
}
@@ -101,6 +105,47 @@ class Solution {
101105
}
102106
```
103107

108+
### **C++**
109+
110+
```cpp
111+
class Solution {
112+
public:
113+
int getLucky(string s, int k) {
114+
string t;
115+
for (char c : s) t += to_string(c - 'a' + 1);
116+
s = t;
117+
while (k--) {
118+
int t = 0;
119+
for (char c : s) t += c - '0';
120+
s = to_string(t);
121+
}
122+
return stoi(s);
123+
}
124+
};
125+
```
126+
127+
### **Go**
128+
129+
```go
130+
func getLucky(s string, k int) int {
131+
var t strings.Builder
132+
for _, c := range s {
133+
t.WriteString(strconv.Itoa(int(c - 'a' + 1)))
134+
}
135+
s = t.String()
136+
for k > 0 {
137+
k--
138+
t := 0
139+
for _, c := range s {
140+
t += int(c - '0')
141+
}
142+
s = strconv.Itoa(t)
143+
}
144+
ans, _ := strconv.Atoi(s)
145+
return ans
146+
}
147+
```
148+
104149
### **...**
105150

106151
```

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

+45-6
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ Thus the resulting integer is 6.
6767
```python
6868
class Solution:
6969
def getLucky(self, s: str, k: int) -> int:
70-
s = ''.join([str(ord(c) - ord('a') + 1) for c in s])
70+
s = ''.join(str(ord(c) - ord('a') + 1) for c in s)
7171
for _ in range(k):
72-
t = 0
73-
for c in s:
74-
t += ord(c) - ord('0')
72+
t = sum(int(c) for c in s)
7573
s = str(t)
7674
return int(s)
7775
```
@@ -88,8 +86,8 @@ class Solution {
8886
s = sb.toString();
8987
while (k-- > 0) {
9088
int t = 0;
91-
for (int i = 0; i < s.length(); ++i) {
92-
t += s.charAt(i) - '0';
89+
for (char c : s.toCharArray()) {
90+
t += c - '0';
9391
}
9492
s = String.valueOf(t);
9593
}
@@ -98,6 +96,47 @@ class Solution {
9896
}
9997
```
10098

99+
### **C++**
100+
101+
```cpp
102+
class Solution {
103+
public:
104+
int getLucky(string s, int k) {
105+
string t;
106+
for (char c : s) t += to_string(c - 'a' + 1);
107+
s = t;
108+
while (k--) {
109+
int t = 0;
110+
for (char c : s) t += c - '0';
111+
s = to_string(t);
112+
}
113+
return stoi(s);
114+
}
115+
};
116+
```
117+
118+
### **Go**
119+
120+
```go
121+
func getLucky(s string, k int) int {
122+
var t strings.Builder
123+
for _, c := range s {
124+
t.WriteString(strconv.Itoa(int(c - 'a' + 1)))
125+
}
126+
s = t.String()
127+
for k > 0 {
128+
k--
129+
t := 0
130+
for _, c := range s {
131+
t += int(c - '0')
132+
}
133+
s = strconv.Itoa(t)
134+
}
135+
ans, _ := strconv.Atoi(s)
136+
return ans
137+
}
138+
```
139+
101140
### **...**
102141

103142
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public:
3+
int getLucky(string s, int k) {
4+
string t;
5+
for (char c : s) t += to_string(c - 'a' + 1);
6+
s = t;
7+
while (k--) {
8+
int t = 0;
9+
for (char c : s) t += c - '0';
10+
s = to_string(t);
11+
}
12+
return stoi(s);
13+
}
14+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
func getLucky(s string, k int) int {
2+
var t strings.Builder
3+
for _, c := range s {
4+
t.WriteString(strconv.Itoa(int(c - 'a' + 1)))
5+
}
6+
s = t.String()
7+
for k > 0 {
8+
k--
9+
t := 0
10+
for _, c := range s {
11+
t += int(c - '0')
12+
}
13+
s = strconv.Itoa(t)
14+
}
15+
ans, _ := strconv.Atoi(s)
16+
return ans
17+
}

solution/1900-1999/1945.Sum of Digits of String After Convert/Solution.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ public int getLucky(String s, int k) {
77
s = sb.toString();
88
while (k-- > 0) {
99
int t = 0;
10-
for (int i = 0; i < s.length(); ++i) {
11-
t += s.charAt(i) - '0';
10+
for (char c : s.toCharArray()) {
11+
t += c - '0';
1212
}
1313
s = String.valueOf(t);
1414
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
class Solution:
22
def getLucky(self, s: str, k: int) -> int:
3-
s = ''.join([str(ord(c) - ord('a') + 1) for c in s])
3+
s = ''.join(str(ord(c) - ord('a') + 1) for c in s)
44
for _ in range(k):
5-
t = 0
6-
for c in s:
7-
t += ord(c) - ord('0')
5+
t = sum(int(c) for c in s)
86
s = str(t)
97
return int(s)

0 commit comments

Comments
 (0)