-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que1313.java
54 lines (47 loc) · 1.31 KB
/
Que1313.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
import java.util.ArrayList;
import java.util.List;
/**
* @author: hyl
* @date: 2020/03/10
**/
public class Que1313 {
/**
* 暴力,直接填充数
* 时间复杂度 : O(N*N)
* 空间复杂度 : O(N)
*/
public int[] decompressRLElist(int[] nums) {
List<Integer> resList = new ArrayList();
for (int i = 0; i < nums.length-1; i+=2) {
for (int j = 0; j < nums[i] ; j++) {
resList.add(nums[i+1]);
}
}
//将list转化为int[]
int[] resArr = resList.stream().mapToInt(Integer::valueOf).toArray();
return resArr;
}
/**
* 先计算出返回数组的长度,然后向新数组内填充
* 时间复杂度 : O(N)
* 空间复杂度 : O(N)
*/
public int[] decompressRLElist1(int[] nums) {
int len = nums.length;
int newLength = 0;
//偶数位相加即为返回数组的长度
for (int i = 0; i < nums.length ; i+=2) {
newLength += nums[i];
}
int[] res = new int[newLength];
int index = 0;
for (int i = 0; i < len; i+=2) {
int count = nums[i];
int value = nums[i+1];
while (count-- > 0){
res[index++] = value;
}
}
return res;
}
}