Skip to content

Commit 5c36e4f

Browse files
committed
1945. 字符串转化后的各位数字之和
1 parent 6c586c7 commit 5c36e4f

3 files changed

+55
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
|1047|[删除字符串中的所有相邻重复项](https://leetcode.cn/problems/remove-all-adjacent-duplicates-in-string/)|[JavaScript](./algorithms/remove-all-adjacent-duplicates-in-string.js)|Easy|
175175
|1780|[判断一个数字是否可以表示成三的幂的和](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/)|[JavaScript](./algorithms/check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
176176
|1832|[判断句子是否为全字母句](https://leetcode.cn/problems/check-if-the-sentence-is-pangram/)|[JavaScript](./algorithms/check-if-the-sentence-is-pangram.js)|Easy|
177+
|1945|[字符串转化后的各位数字之和](https://leetcode.cn/problems/sum-of-digits-of-string-after-convert/)|[JavaScript](./algorithms/sum-of-digits-of-string-after-convert.js)|Easy|
177178
|面试题 04.12|[面试题 04.12. 求和路径](https://leetcode.cn/problems/paths-with-sum-lcci/)|[JavaScript](./algorithms/paths-with-sum-lcci.js)|Medium|
178179
|面试题 02.07|[面试题 02.07. 链表相交](https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/)|[JavaScript](./algorithms/intersection-of-two-linked-lists-lcci.js)|Easy|
179180
|剑指 Offer 05. 替换空格|[剑指 Offer 05. 替换空格](https://leetcode.cn/problems/ti-huan-kong-ge-lcof/)|[JavaScript](./algorithms/ti-huan-kong-ge-lcof.js)|Easy|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* 1945. 字符串转化后的各位数字之和
3+
* @param {string} s
4+
* @param {number} k
5+
* @return {number}
6+
*/
7+
var getLucky = function(s, k) {
8+
let result = 0;
9+
let sum = '';
10+
11+
for (let i = 0; i < s.length; i++) {
12+
sum += s[i].charCodeAt() - 97 + 1;
13+
}
14+
while (k--) {
15+
result = 0;
16+
for (let i = 0; i < sum.length; i++) {
17+
result += Number(sum[i]);
18+
}
19+
sum = result.toString();
20+
}
21+
22+
return result;
23+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode.cn id=1945 lang=javascript
3+
*
4+
* [1945] 字符串转化后的各位数字之和
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @param {number} k
11+
* @return {number}
12+
*/
13+
var getLucky = function(s, k) {
14+
let result = 0;
15+
let sum = '';
16+
17+
for (let i = 0; i < s.length; i++) {
18+
sum += s[i].charCodeAt() - 97 + 1;
19+
}
20+
while (k--) {
21+
result = 0;
22+
for (let i = 0; i < sum.length; i++) {
23+
result += Number(sum[i]);
24+
}
25+
sum = result.toString();
26+
}
27+
28+
return result;
29+
};
30+
// @lc code=end
31+

0 commit comments

Comments
 (0)