8000 refactor 541 · puperfused/Leetcode@f89622d · GitHub
[go: up one dir, main page]

Skip to content 10000

Commit f89622d

Browse files
refactor 541
1 parent f2b016d commit f89622d

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed
Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 541. Reverse String II
5+
*
46
* Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string.
57
* If there are less than k characters left, reverse all of them.
68
* If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and left the other as original.
@@ -15,22 +17,23 @@
1517
*/
1618
public class _541 {
1719

18-
public String reverseStr(String s, int k) {
19-
StringBuilder stringBuilder = new StringBuilder();
20-
for (int i = 0; i < s.length(); i = i + 2 * k) {
21-
if (s.length() >= (i + k)) {
22-
stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse());
23-
} else {
24-
stringBuilder.append(new StringBuilder(s.substring(i)).reverse());
25-
break;
26-
}
27-
if ((i + 2 * k) <= s.length()) {
28-
stringBuilder.append(s.substring(i + k, i + 2 * k));
29-
} else {
30-
stringBuilder.append(s.substring(i + k));
20+
public static class Solution1 {
21+
public String reverseStr(String s, int k) {
22+
StringBuilder stringBuilder = new StringBuilder();
23+
for (int i = 0; i < s.length(); i = i + 2 * k) {
24+
if (s.length() >= (i + k)) {
25+
stringBuilder.append(new StringBuilder(s.substring(i, i + k)).reverse());
26+
} else {
27+
stringBuilder.append(new StringBuilder(s.substring(i)).reverse());
28+
break;
29+
}
30+
if ((i + 2 * k) <= s.length()) {
31+
stringBuilder.append(s.substring(i + k, i + 2 * k));
32+
} else {
33+
stringBuilder.append(s.substring(i + k));
34+
}
3135
}
36+
return stringBuilder.toString();
3237
}
33-
return stringBuilder.toString();
3438
}
35-
3639
}

src/test/java/com/fishercoder/_541Test.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
import static junit.framework.Assert.assertEquals;
99

1010
public class _541Test {
11-
private static _541 test;
11+
private static _541.Solution1 solution1;
1212
private static String expected;
1313
private static String actual;
1414
private static String s;
1515
private static int k;
1616

1717
@BeforeClass
1818
public static void setup() {
19-
test = new _541();
19+
solution1 = new _541.Solution1();
2020
}
2121

2222
@Before
@@ -28,7 +28,7 @@ public void test1() {
2828
s = "abcd";
2929
k = 5;
3030
expected = "dcba";
31-
actual = test.reverseStr(s, k);
31+
actual = solution1.reverseStr(s, k);
3232
assertEquals(expected, actual);
3333
}
3434

@@ -37,7 +37,7 @@ public void test2() {
3737
s = "abcdefg";
3838
k = 2;
3939
expected = "bacdfeg";
40-
actual = test.reverseStr(s, k);
40+
actual = solution1.reverseStr(s, k);
4141
assertEquals(expected, actual);
4242
}
4343

@@ -46,7 +46,7 @@ public void test3() {
4646
s = "abcd";
4747
k = 4;
4848
expected = "dcba";
49-
actual = test.reverseStr(s, k);
49+
actual = solution1.reverseStr(s, k);
5050
assertEquals(expected, actual);
5151
}
5252

@@ -55,7 +55,7 @@ public void test4() {
5555
s = "abcdefg";
5656
k = 3;
5757
expected = "cbadefg";
58-
actual = test.reverseStr(s, k);
58+
actual = solution1.reverseStr(s, k);
5959
assertEquals(expected, actual);
6060
}
6161

@@ -64,7 +64,7 @@ public void test5() {
6464
s = "abcd";
6565
k = 3;
6666
expected = "cbad";
67-
actual = test.reverseStr(s, k);
67+
actual = solution1.reverseStr(s, k);
6868
assertEquals(expected, actual);
6969
}
7070

@@ -74,7 +74,7 @@ public void test6() {
7474
System.out.println("s.length() = " + s.length());
7575
k = 39;
7676
expected = "fdcqkmxwholhytmhafpesaentdvxginrjlyqzyhehybknvdmfrfvtbsovjbdhevlfxpdaovjgunjqllgsqddebemjanqcqnfkjmi";
77-
actual = test.reverseStr(s, k);
77+
actual = solution1.reverseStr(s, k);
7878
assertEquals(expected, actual);
7979
}
8080

0 commit comments

Comments
 (0)
0