# 第四十七周ARTS总结
# Algorithm
0ms | 100.00% Run time
37.7MB | 5.21% Memory
public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> ans = new ArrayList<>();
if (matrix.length == 0) {
return ans;
}
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;
// 一圈圈的输出
while (top <= bottom && left <= right) {
// 最后一圈为一行或者一列时
if (top == bottom) {
for (int i = left; i <= right; i++) {
ans.add(matrix[top][i]);
}
break;
}
if (left == right) {
for (int i = top; i <= bottom; i++) {
ans.add(matrix[i][left]);
}
break;
}
// 上
for (int i = left; i < right; i++) {
ans.add(matrix[top][i]);
}
// 右
for (int i = top; i < bottom; i++) {
ans.add(matrix[i][right]);
}
// 下
for (int i = right; i > left; i--) {
ans.add(matrix[bottom][i]);
}
// 左
for (int i = bottom; i > top; i--) {
ans.add(matrix[i][left]);
}
top++;
bottom--;
left++;
right--;
}
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
50
51
52
53
54
55
56
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
# Review
# Tip
- 可在
RecyclerView.ViewHolder
里通过getAdapterPosition()
获取当前被点击条目的索引 RecyclerView.Adapter
中的getItemViewType
实现时,则onCreateViewHolder
会被多次调用,因此会创建多个ViewHolder
,即使ViewType
相同也会被多次创建<fragment>
标签中可以用name
属性指定布局- 替换
Fragment
的步骤:FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, new RightFragment());
fragmentTransaction.commit();
- 通过
fragmentTransaction.addToBackStack(null);
可以将替换的Fragment
加入到返回栈 Fragment
的生命周期:onAttach()
:当碎片和活动建立关联时调用onCreateView()
:当为碎片创建布局时调用onActivityCreated()
:与碎片相关联的活动创建完毕后调用onDestroyView()
:当与碎片相关的布局被移除时调用onDetach()
:当碎片与活动解除关联时调用
# Share
暂无内容