-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetLuck.java
More file actions
27 lines (24 loc) · 781 Bytes
/
Copy pathGetLuck.java
File metadata and controls
27 lines (24 loc) · 781 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package LeetcodePractice;
public class GetLuck {
public int getLucky(String s, int k) {
StringBuilder num = new StringBuilder();
for (char c : s.toCharArray()) {
num.append(c - 'a' + 1);
}
int result = 0;
for (int i = 0; i < k; i++) {
result = 0;
for (char c : num.toString().toCharArray()) {
result += c - '0';
}
num = new StringBuilder(String.valueOf(result));
}
return result;
}
public static void main(String[] args) {
GetLuck sol = new GetLuck();
System.out.println(sol.getLucky("iiii", 1));
System.out.println(sol.getLucky("leetcode", 2));
System.out.println(sol.getLucky("zbax", 2));
}
}