Skip to content

Commit fd79157

Browse files
committed
Refactored the code a bit
1 parent 933bc38 commit fd79157

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

src/main/java/com/leetcode/strings/ReverseStringII.java

+4-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/**
44
* Problem: https://leetcode.com/problems/reverse-string-ii/
5-
*
5+
*
66
* @author rampatra
77
* @since 2019-04-20
88
*/
@@ -13,7 +13,7 @@ public class ReverseStringII {
1313
* where,
1414
* n = no. of characters in string
1515
* <p>
16-
* Runtime: <a href="https://leetcode.com/submissions/detail/223714484/">0 ms</a>.
16+
* Runtime: <a href="https://leetcode.com/submissions/detail/223715011/">0 ms</a>.
1717
*
1818
* @param str
1919
* @param k
@@ -22,14 +22,8 @@ public class ReverseStringII {
2222
public static String reverseStr(String str, int k) {
2323
char[] chars = str.toCharArray();
2424
int len = str.length();
25-
int i = 0;
26-
while (i < len) {
27-
if (len - i + 1 >= 2 * k) {
28-
reverse(chars, i, i + k);
29-
} else {
30-
reverse(chars, i, Math.min(len, i + k));
31-
}
32-
i += 2 * k;
25+
for (int i = 0; i < len; i += 2 * k) {
26+
reverse(chars, i, Math.min(len, i + k));
3327
}
3428
return new String(chars);
3529
}

0 commit comments

Comments
 (0)