Skip to content

Commit 8b8019c

Browse files
add 1945
1 parent b99ecdf commit 8b8019c

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ _If you like this project, please leave me a star._ ★
88

99
| # | Title | Solutions | Video | Difficulty | Tag 1
1010
|-----|----------------|---------------|--------|-------------|-------------
11+
|1945|[Sum of Digits of String After Convert](https://leetcode.com/problems/sum-of-digits-of-string-after-convert/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1945.java) ||Easy||
1112
|1941|[Check if All Characters Have Equal Number of Occurrences](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1941.java) ||Easy||
1213
|1936|[Add Minimum Number of Rungs](https://leetcode.com/problems/add-minimum-number-of-rungs/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1936.java) ||Medium||
1314
|1935|[Maximum Number of Words You Can Type](https://leetcode.com/problems/maximum-number-of-words-you-can-type/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1935.java) ||Easy|String|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class _1945 {
7+
public static class Solution1 {
8+
public int getLucky(String s, int k) {
9+
List<Integer> list = new ArrayList<>();
10+
for (char c : s.toCharArray()) {
11+
list.add(c - 'a' + 1);
12+
}
13+
int sum = 0;
14+
for (int i : list) {
15+
if (i >= 10) {
16+
sum += i / 10;
17+
}
18+
sum += i % 10;
19+
}
20+
while (k-- > 1) {
21+
int newSum = 0;
22+
while (sum != 0) {
23+
newSum += sum % 10;
24+
sum /= 10;
25+
}
26+
sum = newSum;
27+
}
28+
return sum;
29+
}
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._1945;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class _1945Test {
10+
private static _1945.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _1945.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(8, solution1.getLucky("zbax", 2));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(36, solution1.getLucky("iiii", 1));
25+
}
26+
27+
@Test
28+
public void test3() {
29+
assertEquals(6, solution1.getLucky("leetcode", 2));
30+
}
31+
32+
@Test
33+
public void test4() {
34+
assertEquals(8, solution1.getLucky("zbax", 2));
35+
}
36+
37+
@Test
38+
public void test5() {
39+
assertEquals(8, solution1.getLucky("fleyctuuajsr", 5));
40+
}
41+
42+
}

0 commit comments

Comments
 (0)