-
Notifications
You must be signed in to change notification settings - Fork 21
/
_058_Rotate_String.java
58 lines (46 loc) · 1.34 KB
/
_058_Rotate_String.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package offerV2;
/**
* @No v2-058,v1-042
* @problem 左旋转字符串
* @tag 数组、字符串
* @author liyazhou
* @date 2017-06-02
*
* <pre>
* 字符串的左悬殊操作是把字符串前面的若干个字符转移到字符串的尾部。
* 请定义一个函数实现字符串左旋转操作的功能。
* 比如输入字符串"abcdefg" 和数字 2,该函数将返回左旋转 2 位得到的结果"cdefgab"。
* </pre>
*/
public class _058_Rotate_String {
/**
* Note
*
* Thought
*
*
* Algorithm
* 1. 左半部分翻转
* 2. 有半部分翻转
* 3. 整体翻转
*/
private static class Solution {
public String leftRotateString(char[] chars, int k){
if (chars == null) return null;
reverse(chars, 0, k-1);
reverse(chars, k, chars.length-1);
reverse(chars, 0, chars.length-1);
return new String(chars);
}
private void reverse(char[] chars, int start, int end){
for (; start < end; start ++, end --){
swap(chars, start, end);
}
}
private void swap(char[] chs, int i, int j) {
char tmp = chs[i];
chs[i] = chs[j];
chs[j] = tmp;
}
}
}