-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que1528.java
66 lines (58 loc) · 1.59 KB
/
Que1528.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
/**
* @author: hyl
* @date: 2020/09/18
**/
public class Que1528 {
/**
* 直接暴力遍历即可
* 时间复杂度 : O(N)
* 空间复杂度 : O(N)
*
* @param s
* @param indices
* @return
*/
public String restoreString(String s, int[] indices) {
if (s == null || s.length() == 0 || indices == null || indices.length == 0) {
return "";
}
char[] resCharArr = new char[s.length()];
for (int i = 0; i < indices.length; i++) {
resCharArr[indices[i]] = s.charAt(i);
}
return new String(resCharArr);
}
/**
* 使用原地修改的方式达到空间复杂度O(1)
* 时间复杂度 : O(N)
* 空间复杂度 : O(1)
*
* @param s
* @param indices
* @return
*/
public String restoreString2(String s, int[] indices) {
if (s == null || s.length() == 0 || indices == null || indices.length == 0) {
return "";
}
int len =s.length();
char[] chars = s.toCharArray();
for (int i = 0; i < len; i++) {
if (indices[i] != i){
char ch = chars[i];
int idx = indices[i];
while (idx != i){
char temp = chars[idx];
chars[idx] = ch;
ch = temp;
int t = indices[idx];
indices[idx] = idx;
idx = t;
}
chars[i] = ch;
indices[i] = i;
}
}
return new String(chars);
}
}