题目描述:
1945. 字符串转化后的各位数字之和 - 力扣(LeetCode) (leetcode-cn.com)
自测用例:
"iiii"
1
"leetcode"
2
"zbax"
2
"abcdefghijklmnopqrstuvwxyz"
1
"abcdefghijklmnopqrstuvwxyz"
2
"abcdefghijklmnopqrstuvwxyz"
3
"abcdefghijklmnopqrstuvwxyz"
4
"abcdefghijklmnopqrstuvwxyz"
5
"abcdefghijklmnopqrstuvwxyz"
10
"abcdefghijklmnopqrstuvwxyz"
9
"abcdefghijklmnopqrstuvwxyz"
8
Java代码:
class Solution {
public int bitSum(int n){
int ans=0;
for(;n!=0;n/=10)ans+=n%10;
return ans;
}
public int getLucky(String s, int k) {
int[] m={1,2,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,2,3,4,5,6,7,8};
int ans=0;
for(int e:s.toCharArray())ans+=m[e-'a'];
for(int i=1;i<k;i++)ans=bitSum(ans);
return ans;
}
}
?
|