Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Lines changed: 4 additions & 10 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /