File tree 6 files changed +131
-18
lines changed
solution/1900-1999/1945.Sum of Digits of String After Convert
6 files changed +131
-18
lines changed Original file line number Diff line number Diff line change 59
59
60
60
<!-- 这里可写通用的实现逻辑 -->
61
61
62
+ ** 方法一:模拟**
63
+
64
+ 根据题目描述进行模拟即可。
65
+
66
+ 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为字符串 $s$ 的长度。
67
+
62
68
<!-- tabs:start -->
63
69
64
70
### ** Python3**
68
74
``` python
69
75
class Solution :
70
76
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)
72
78
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)
76
80
s = str (t)
77
81
return int (s)
78
82
```
@@ -91,8 +95,8 @@ class Solution {
91
95
s = sb. toString();
92
96
while (k-- > 0 ) {
93
97
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' ;
96
100
}
97
101
s = String . valueOf(t);
98
102
}
@@ -101,6 +105,47 @@ class Solution {
101
105
}
102
106
```
103
107
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
+
104
149
### ** ...**
105
150
106
151
```
Original file line number Diff line number Diff line change @@ -67,11 +67,9 @@ Thus the resulting integer is 6.
67
67
``` python
68
68
class Solution :
69
69
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)
71
71
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)
75
73
s = str (t)
76
74
return int (s)
77
75
```
@@ -88,8 +86,8 @@ class Solution {
88
86
s = sb. toString();
89
87
while (k-- > 0 ) {
90
88
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' ;
93
91
}
94
92
s = String . valueOf(t);
95
93
}
@@ -98,6 +96,47 @@ class Solution {
98
96
}
99
97
```
100
98
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
+
101
140
### ** ...**
102
141
103
142
```
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -7,8 +7,8 @@ public int getLucky(String s, int k) {
7
7
s = sb .toString ();
8
8
while (k -- > 0 ) {
9
9
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' ;
12
12
}
13
13
s = String .valueOf (t );
14
14
}
Original file line number Diff line number Diff line change 1
1
class Solution :
2
2
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 )
4
4
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 )
8
6
s = str (t )
9
7
return int (s )
You can’t perform that action at this time.
0 commit comments