# 第十五周ARTS总结
# Algorithm
288ms | 5.02% Run time
58.4MB | 8.69% Memory
public List<List<Integer>> fourSum(int[] nums, int target) {
// 排序
Arrays.sort(nums);
List<Integer> numsList = new ArrayList<>();
for (int num : nums) {
numsList.add(num);
}
return findX(numsList, 4, target);
}
/**
* 从source中找出size个数,使之和为sum,并返回所有的序列(不重复)
*
* @param source 序列
* @param size 个数
* @param sum 总和
*/
public List<List<Integer>> findX(List<Integer> source, int size, int sum) {
List<List<Integer>> list = new ArrayList<>();
// 用二分法找到所需要的值
if (size == 1) {
int leftIndex = 0;
int rightIndex = source.size() - 1;
while (rightIndex - leftIndex > 1) {
int middleIndex = (leftIndex + rightIndex) / 2;
if (sum < source.get(middleIndex)) {
rightIndex = middleIndex;
} else if (sum > source.get(middleIndex)) {
leftIndex = middleIndex;
} else {
List<Integer> ans = new ArrayList<>();
ans.add(sum);
list.add(ans);
break;
}
}
// rightIndex - leftIndex == 1 的情况
if (list.size() == 0) {
if (sum == source.get(leftIndex) || sum == source.get(rightIndex)) {
List<Integer> ans = new ArrayList<>();
ans.add(sum);
list.add(ans);
}
}
return list;
}
for (int i = 0; i < source.size() - size + 1; i++) {
// 力求不重复
if (i > 0 && source.get(i).equals(source.get(i - 1))) {
continue;
}
int thisNum = source.get(i);
// 获取剩余部分的序列
List<List<Integer>> tempList = findX(source.subList(i + 1, source.size()), size - 1, sum - thisNum);
// 拼上第一个数
for (List<Integer> temp : tempList) {
temp.add(0, thisNum);
list.add(temp);
}
}
return list;
}
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
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
给XSum
类型的题给了一种通用的解法,其中X
为任意整数
# Review
# Tip
FragmnetPageAdapter
在每次切换页面时,只是将Fragment
进行分离,适合页面较少的Fragment
使用以保存一些内存,对系统内存不会多大影响FragmentPageStateAdapter
在每次切换页面的时候,是将Fragment
进行回收,适合页面较多的Fragment
使用,这样就不会消耗更多的内存fragment
不通过构造函数进行传值的原因是因为横屏切换的时候获取不到值Viewpager
配合fragment
使用,默认加载前两个fragment
。很容易造成网络丢包、阻塞等问题。因此对于Fragment
多的可采用懒加载
# Share
暂无内容
← 第十四周ARTS总结 第十六周ARTS总结 →