# 第三十四周ARTS总结
# Algorithm
2ms | 100.00% Run time
36.3MB | 100.00% Memory
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
Arrays.sort(candidates);
return recursive(candidates, target, 0);
}
/**
* 从startIndex索引起在candidates中选若干个求和等于target的结果集
*
* @param candidates 候选数
* @param target 目标数
* @param startIndex 开始索引
* @return
*/
private List<List<Integer>> recursive(int[] candidates, int target, int startIndex) {
List<List<Integer>> ans = new ArrayList<>();
// 此时无满足条件的结果集
if (startIndex >= candidates.length || target < candidates[startIndex]) {
return ans;
}
for (int i = startIndex; i < candidates.length; i++) {
int thisNum = candidates[i];
// 防止重复答案
if (i > startIndex && thisNum == candidates[i - 1]) {
continue;
}
// 此时已经找到目标值
if (target == thisNum) {
List<Integer> item = new ArrayList<>();
item.add(target);
ans.add(item);
return ans;
}
// 继续迭代找结果集
List<List<Integer>> temp = recursive(candidates, target - thisNum, i + 1);
for (List<Integer> item : temp) {
item.add(0, thisNum);
ans.add(item);
}
}
return ans;
}
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
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
# Review
- Don’t build systems. Build subsystems. (opens new window)
注:不要构建系统,而要构建子系统。
- AndroidDevSummit 2019: What's new in Android Fragments (opens new window)
- Why MVP is outdated in Android (opens new window)
为什么说MVP过时了?
Contract
接口过于庞大Contract
中大多数方法都是无意义的- 可重用度低
- 粒度难以划分
- 循环依赖
- 很难模块化
- Smaller PNGs, and Android’s AAPT tool (opens new window)
# Tip
LayoutInflater
创建的寻找- 由
LayoutInflater#from
可知从context
中获取的系统服务 - 所有的
Context
实际均由ContextImpl
实现 - 由
ContextImpl#getSystemService
类可知从SystemServiceRegistry
注册器中获取 - 由
SystemServiceRegistry#getSystemService
可知最终获取系统服务的是ServiceFetcher
- 所有的系统服务都存放在
SystemServiceRegistry
的SYSTEM_SERVICE_FETCHERS
集合中 - 在静态方法内可以发现实际生成的是
PhoneLayoutInflater
对象
- 由
inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)
方法第三个参数的意义:该布局是否关联到root
中,false
表示root
只用于创建布局的LayoutParams
ART
与Dalvik
的区别:Dalvik
:应用每次运行的时候,字节码都需要通过即时编译器(just in time ,JIT)转换为机器码,这会拖慢应用的运行效率。ART
:应用在第一次安装的时候,字节码就会预先编译成机器码,极大的提高了程序的运行效率,同时减少了手机的耗电量。该过程成为预编译(AOT)。
- 自定义
View
的步骤:- 新建类集成
View
View(Context context)
:通过代码创建View
会调用此构造方法View(Context context, @Nullable AttributeSet attrs)
:在xml创建但是没有指定style会调用此构造方法
- 添加自定义属性
- 在构造方法中获取属性,并初始化画笔等 (注意
TypedArray
需及时回收) - 在
onMeasure(int widthMeasureSpec, int heightMeasureSpec)
中执行测量操作,主要计算控件占多大地方UNSPECIFIED
:任意大小,尽可能大EXACTLY
:一个确定的值,例如100dp
、match_parent
AT_MOST
:包裹内容,例如wrap_content
- 在
onDraw(Canvas canvas)
中做具体的绘制操作
- 新建类集成
- 初始化三方库的一种方法:利用
ContentProvider & Manifest-Merger
的特性