1
0
Fork 0
mirror of https://gitlab.com/mfocko/LeetCode.git synced 2024-09-16 16:36:56 +02:00

java: add «1945. Sum of Digits of String After Convert»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-03 21:11:10 +02:00
parent 43d48f5fcc
commit 4d20c963ba
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View 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;
}
}