# 第三十四周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

# Review

# Tip

  • LayoutInflater创建的寻找
    1. LayoutInflater#from可知从context中获取的系统服务
    2. 所有的Context实际均由ContextImpl实现
    3. ContextImpl#getSystemService类可知从SystemServiceRegistry注册器中获取
    4. SystemServiceRegistry#getSystemService可知最终获取系统服务的是ServiceFetcher
    5. 所有的系统服务都存放在SystemServiceRegistrySYSTEM_SERVICE_FETCHERS集合中
    6. 在静态方法内可以发现实际生成的是PhoneLayoutInflater对象
  • inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot)方法第三个参数的意义:该布局是否关联到root中,false表示root只用于创建布局的LayoutParams
  • ARTDalvik的区别:
    • Dalvik:应用每次运行的时候,字节码都需要通过即时编译器(just in time ,JIT)转换为机器码,这会拖慢应用的运行效率。
    • ART:应用在第一次安装的时候,字节码就会预先编译成机器码,极大的提高了程序的运行效率,同时减少了手机的耗电量。该过程成为预编译(AOT)。
  • 自定义View的步骤:
    1. 新建类集成View
      • View(Context context):通过代码创建View会调用此构造方法
      • View(Context context, @Nullable AttributeSet attrs):在xml创建但是没有指定style会调用此构造方法
    2. 添加自定义属性
    3. 在构造方法中获取属性,并初始化画笔等 (注意TypedArray需及时回收)
    4. onMeasure(int widthMeasureSpec, int heightMeasureSpec)中执行测量操作,主要计算控件占多大地方
      • UNSPECIFIED:任意大小,尽可能大
      • EXACTLY:一个确定的值,例如100dpmatch_parent
      • AT_MOST:包裹内容,例如wrap_content
    5. onDraw(Canvas canvas)中做具体的绘制操作
  • 初始化三方库的一种方法:利用ContentProvider & Manifest-Merger的特性

# Share

更新时间: 10/20/2022, 7:04:01 AM