-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathString1.java
More file actions
108 lines (102 loc) · 1.86 KB
/
String1.java
File metadata and controls
108 lines (102 loc) · 1.86 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package string;
public class String1 {
// 替换空格
public static char[] replaceSpace(char p[], char r[]) {
if (p == null || r == null || p.length == 0) {
return null;
}
int count = 0;
int oriLength = 0;
while (p[oriLength] != 0) {
if (p[oriLength] == ' ') {
count++;
}
oriLength++;
}
count = oriLength + (r.length - 1) * count - 1;
oriLength--;
while (oriLength != count && oriLength >= 0) {
if (p[oriLength] == ' ') {
for (int j = r.length - 1; j >= 0; j--) {
p[count] = r[j];
count--;
}
oriLength--;
} else {
p[count] = p[oriLength];
count--;
oriLength--;
}
}
return p;
}
// 打印全排列
public static void permutationStr(char p[], int start) {
if (p == null) {
return;
}
if (start == p.length) {
System.out.println(p);
return;
}
char c;
for (int i = start; i < p.length; i++) {
c = p[start];
p[start] = p[i];
p[i] = c;
permutationStr(p, start + 1);
c = p[start];
p[start] = p[i];
p[i] = c;
}
}
// 第一个不重复字符
public static char firstNotRepeat(char p[]) {
if (p == null) {
return 0;
}
int ch[] = new int[256];
for (int i = 0; i < p.length; i++) {
ch[p[i]]++;
}
for (int i = 0; i < p.length; i++) {
if (ch[p[i]] == 1) {
return p[i];
}
}
return 0;
}
// 翻转单词
public static void reverseSub(char p[], int start, int end) {
char c;
int i = start;
int j = end;
while (i < j) {
c = p[i];
p[i] = p[j];
p[j] = c;
i++;
j--;
}
}
// 翻转句子
public static void reverseSentence(char p[]) {
if (p == null) {
return;
}
int start = 0;
int end = 0;
reverseSub(p, 0, p.length - 1);
while (start < p.length) {
if (p[start] == ' ') {
start++;
end++;
} else if (end == p.length || p[end] == ' ') {
reverseSub(p, start, --end);
start = ++end;
} else {
end++;
}
}
}
}