File tree 5 files changed +84
-0
lines changed
algorithms/ReverseStringII
5 files changed +84
-0
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,7 @@ All solutions will be accepted!
12
12
| 804| [ Unique Morse Code Words] ( https://leetcode-cn.com/problems/unique-morse-code-words/description/ ) | [ java/py/js] ( ./algorithms/UniqueMorseCodeWords ) | Easy|
13
13
| 500| [ Keyboard Row] ( https://leetcode-cn.com/problems/keyboard-row/description/ ) | [ java/py/js] ( ./algorithms/KeyboardRow ) | Easy|
14
14
| 344| [ Reverse String] ( https://leetcode-cn.com/problems/reverse-string/ ) | [ java/py/js] ( ./algorithms/ReverseString ) | Easy|
15
+ | 541| [ Reverse String II] ( https://leetcode-cn.com/problems/reverse-string-ii/description/ ) | [ java/py/js] ( ./algorithms/ReverseStringII ) | Easy|
15
16
| 292| [ Nim Game] ( https://leetcode-cn.com/problems/nim-game/description/ ) | [ java/py/js] ( ./algorithms/NimGame ) | Easy|
16
17
| 557| [ Reverse Words In A String III] ( https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/description/ ) | [ java/py/js] ( ./algorithms/ReverseWordsInAStringIII ) | Easy|
17
18
| 104| [ Maximum Depth Of Binary Tree] ( https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/ ) | [ java/py/js] ( ./algorithms/MaximumDepthOfBinaryTree ) | Easy|
Original file line number Diff line number Diff line change
1
+ # Reverse String II
2
+ This problem is easy to solve
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public String reverseStr (String s , int k ) {
3
+ char [] res = s .toCharArray ();
4
+ int i = 0 ,
5
+ length = s .length ();
6
+
7
+ while (i < length ) {
8
+ if (i % (2 * k ) == 0 ) {
9
+ int start = i ,
10
+ end = i + k - 1 ;
11
+ if (end >= length ) {
12
+ end = length - 1 ;
13
+ }
14
+ while (start < end ) {
15
+ char temp = res [start ];
16
+ res [start ++] = res [end ];
17
+ res [end --] = temp ;
18
+ }
19
+ i = end + 1 ;
20
+ } else {
21
+ i ++;
22
+ }
23
+ }
24
+
25
+ return new String (res );
26
+ }
27
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @param {number } k
4
+ * @return {string }
5
+ */
6
+ var reverseStr = function ( s , k ) {
7
+ let res = s . split ( '' ) ,
8
+ i = 0
9
+
10
+ while ( i < s . length ) {
11
+ if ( i % ( 2 * k ) === 0 ) {
12
+ let start = i ,
13
+ end = i + k - 1
14
+ if ( end >= s . length ) {
15
+ end = s . length - 1
16
+ }
17
+ while ( start < end ) {
18
+ let temp = res [ start ]
19
+ res [ start ++ ] = res [ end ]
20
+ res [ end -- ] = temp
21
+ }
22
+ i = end + 1
23
+ } else {
24
+ i ++
25
+ }
26
+ }
27
+
28
+ return res . join ( '' )
29
+ } ;
Original file line number Diff line number Diff line change
1
+ class Solution (object ):
2
+ def reverseStr (self , s , k ):
3
+ """
4
+ :type s: str
5
+ :type k: int
6
+ :rtype: str
7
+ """
8
+ res = list (s )
9
+ i = 0
10
+ while i < len (s ):
11
+ if i % (2 * k ) == 0 :
12
+ start = i
13
+ end = i + k - 1
14
+ if end >= len (s ):
15
+ end = len (s ) - 1
16
+ while start < end :
17
+ res [start ], res [end ] = res [end ], res [start ]
18
+ start += 1
19
+ end -= 1
20
+
21
+ i = end + 1
22
+ else :
23
+ i += 1
24
+
25
+ return '' .join (res )
You can’t perform that action at this time.
0 commit comments