Skip to content

Commit 82b3bb7

Browse files
committed
Refine 541_Reverse_String_II
1 parent efa47f2 commit 82b3bb7

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ I'm currently working on [Analytics-Zoo](https://github.com/intel-analytics/anal
164164
| 485 | [Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/485_Max_Consecutive_Ones.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/485_Max_Consecutive_Ones.java) | Add one when encounter 1, set to 0 when encounter 0, O(n) and O(1) |
165165
| 509 | [Fibonacci Number](https://leetcode.com/problems/fibonacci-number/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/509_Fibonacci_Number.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/509_Fibonacci_Number.java) | 1. Recursive, O(n) <br>2. DP with memo, O(n). Note that N<=30, which means that we can keep a memo from 0 to 30. |
166166
| 538 | [Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/538_Convert_BST_to_Greater_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/538_Convert_BST_to_Greater_Tree.java) | Right first DFS with a variable recording sum of node.val and right.val. 1. Recursive.<br>2. Stack 3. Reverse Morris In-order Traversal |
167+
| 541 | [Reverse String II](https://leetcode.com/problems/reverse-string-ii/solution/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/541_Reverse_String_II.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/541_Reverse_String_II.java) | Handle each 2k until reaching end, On(n) and O(n) |
167168
| 543 | [Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/description/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/543_Diameter_of_Binary_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/543_Diameter_of_Binary_Tree.java) | DFS with O(1) for max answer |
168169
| 547 | [Friend Circles](https://leetcode.com/problems/friend-circles/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/547_Friend_Circles.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/547_Friend_Circles.java) | 1. DFS, O(n^2) and O(1)<br>2. BFS, O(n^2) and O(1)<br>3. Union-find, O(n^2) and O(n)|
169170
| 557 | [Reverse Words in a String III](https://leetcode.com/problems/reverse-words-in-a-string-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/557_Reverse_Words_in_a_String_III.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/557_Reverse_Words_in_a_String_III.java) | String handle: Split with space than reverse word, O(n) and O(n). Better solution is that reverse can be O(1) space in array. |

java/541_Reverse_String_II.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public String reverseStr(String s, int k) {
3+
// https://leetcode.com/problems/reverse-string-ii/solution/
4+
char[] a = s.toCharArray();
5+
for (int start = 0; start < a.length; start += 2 * k) {
6+
int i = start, j = Math.min(start + k - 1, a.length - 1);
7+
// Reverse from i to j
8+
while (i < j) {
9+
char tmp = a[i];
10+
a[i++] = a[j];
11+
a[j--] = tmp;
12+
}
13+
}
14+
return new String(a);
15+
}
16+
}

python/541_Reverse_String_II.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Solution:
2-
def reverseStr(self, s, k):
2+
def reverseStr(self, s: str, k: int) -> str:
33
N = len(s)
44
ans = ""
55
position = 0
@@ -9,6 +9,12 @@ def reverseStr(self, s, k):
99
position += 2 * k
1010
return ans
1111

12+
# def reverseStr(self, s: str, k: int) -> str:
13+
# s = list(s)
14+
# for i in range(0, len(s), 2*k):
15+
# s[i:i+k] = reversed(s[i:i+k])
16+
# return "".join(s)
17+
1218

1319

1420
s1 = Solution()

0 commit comments

Comments
 (0)