-
Notifications
You must be signed in to change notification settings - Fork 58
/
Day30.java
72 lines (59 loc) · 1.6 KB
/
Day30.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.offer;
/**
* 反转单词顺序 题目要求如 i am a student.
* 反转后 student. a am i
* @author kexun
*
*/
public class Day30 {
public static void main(String[] args) {
Day30 d = new Day30();
StringBuffer str = new StringBuffer("i am a student.");
d.reverse(str, 0, str.length()-1);
System.out.println(str);
d.singleReverse(str);
System.out.println(str);
StringBuffer sb = new StringBuffer("abcdefg");
d.leftReverse(sb, 5);
System.out.println(sb);
}
/**
* 通过两次反转,第一次反转整个string,结果为 .tneduts a ma i
* 然后对每个单词再做一次反转即可得到结果
* @param str
*/
public void reverse(StringBuffer sb, int start, int end) {
if (sb.length() == 0)
return;
// 第一次对整个string反转
while (start < end) {
String endStr = sb.substring(end, end+1);
String startStr = sb.substring(start, start+1);
sb.replace(end, end+1, startStr);
sb.replace(start, start+1, endStr);
start++;
end--;
}
}
// 第二次对单个词反转
public void singleReverse(StringBuffer sb) {
int start = 0;
for (int i = 0; i < sb.length(); i++) {
char ch = sb.charAt(i);
if (ch == ' ') {
reverse(sb, start, i-1);
start = i+1;
}
}
}
// 难度加大点,左旋转字符串 如: abcdef和2 结果 cdefab
public void leftReverse(StringBuffer sb, int index) {
if (sb==null || sb.length()==0 || index >= sb.length())
return;
int start = 0;
int end = sb.length()-1;
reverse(sb, start, end);
reverse(sb, start, end-index);
reverse(sb, end-index+1, end);
}
}