-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLeftRotateString.java
52 lines (48 loc) · 1.33 KB
/
LeftRotateString.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
package org.offer.case42;
/**
* 面试题 42
* 题目二:左旋转字符串
* Created by tanc on 2017/7/5.
*/
public class LeftRotateString {
/**
* 方法一:直接剪切字符串成两部分,再重新拼接
*/
public static String methodOne(String src, int num) {
int len = src.length();
num = num % len;
if (num == 0) {
return src;
}
String a = src.substring(0, num);
String b = src.substring(num, len);
return b + a;
}
/**
* 方法二:使用书上的方法,先分别翻转前后两部分,再翻转全部,一共翻转三次,就可以得到结果
*/
public static String methodTwo(String src, int num) {
int len = src.length();
num = num % len;
if (num == 0) {
return src;
}
char[] chars = src.toCharArray();
reverse(chars, 0, num - 1);
reverse(chars, num, chars.length - 1);
reverse(chars, 0, chars.length - 1);
return new String(chars);
}
/**
* 翻转部分字符
*/
private static void reverse(char[] data, int start, int end) {
while (start < end) {
char temp = data[start];
data[start] = data[end];
data[end] = temp;
start++;
end--;
}
}
}