java: add «1945. Sum of Digits of String After Convert»
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
43d48f5fcc
commit
4d20c963ba
1 changed files with 23 additions and 0 deletions
23
java/sum-of-digits-of-string-after-convert.java
Normal file
23
java/sum-of-digits-of-string-after-convert.java
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
class Solution {
|
||||||
|
public int getLucky(String s, int k) {
|
||||||
|
// First iteration
|
||||||
|
int lucky = 0;
|
||||||
|
for (var c : s.toCharArray()) {
|
||||||
|
var number = c - 'a' + 1;
|
||||||
|
while (number > 0) {
|
||||||
|
lucky += number % 10;
|
||||||
|
number /= 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 1; i < k; ++i) {
|
||||||
|
var nextLucky = 0;
|
||||||
|
for (; lucky > 0; lucky /= 10) {
|
||||||
|
nextLucky += lucky % 10;
|
||||||
|
}
|
||||||
|
lucky = nextLucky;
|
||||||
|
}
|
||||||
|
|
||||||
|
return lucky;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue