目录
- 79. Word Search
- 48. Rotate Image
- 23. Merge k Sorted Lists
- 56 Merge Intervals
- 72 Edit Distance
- 78 Subsets
- 102. Binary Tree Level Order Traversal
- 105. Construct Binary Tree from Preorder and Inorder Traversal
- 406. Queue Reconstruction by Height
- 437. Path Sum III
- 103. Binary Tree Zigzag Level Order Traversal
- 1269. Number of Ways to Stay in the Same Place After Some Steps
- 912. Sort an Array
- 189. Rotate Array
- 145. Binary Tree Postorder Traversal
- 142. Linked List Cycle II
79. Word Search
掌握C语言中二维指针的定义(不定长度二维数组传参需要使用):
int** visited &#61; malloc(sizeof(int*) * boardSize);for (int i &#61; 0; i < boardSize; i&#43;&#43;) {visited[i] &#61; malloc(sizeof(int) * boardColSize[0]);memset(visited[i], 0, sizeof(int) * boardColSize[0]);}
48. Rotate Image
四个变量a, b, c, d, a赋值给b&#xff0c;b给c&#xff0c;c给d&#xff0c;d给a&#xff0c;只需要一个临时变量。
23. Merge k Sorted Lists
Java中优先队列的用法&#xff08;本质是堆排序&#xff0c;每次维护堆消耗logn时间&#xff09;&#xff1a;
class Status implements Comparable<Status> {int val;ListNode ptr;Status(int val, ListNode ptr) { this.val &#61; val;this.ptr &#61; ptr;}public int compareTo(Status status2) {return this.val - status2.val;}}PriorityQueue<Status> queue &#61; new PriorityQueue<Status>(); queue.offer(new Status(node.val, node)); Status f &#61; queue.poll();
定义和初始化更简洁的写法&#xff1a;
Queue<ListNode> pq &#61; new PriorityQueue<>((v1, v2) -> v1.val - v2.val);
56 Merge Intervals
Java中可实现堆栈的双端队列用法&#xff1a;
Deque deque &#61; new LinkedList();
队列操作&#xff1a;addLast(e)&#xff0c;removeFirst()&#xff0c;peekFirst()
栈操作&#xff1a;addFirst(e)&#xff0c;removeFirst()&#xff0c;peekFirst()
72 Edit Distance
二维动态规划基本操作&#xff1a;
- 定义长宽分别为m&#43;1、n&#43;1的二维数组&#xff1b;
- 第0行与第0列分别初始化&#xff1b;
- 1-m, 2-n 填表
78 Subsets
dfs循环时一定不要忘记恢复初始状态。如果考虑是否跳过当前元素&#xff0c;可以不使用循环&#xff0c;更简单。
102. Binary Tree Level Order Traversal
广度优先搜索时&#xff0c;为了判断当前在第几层&#xff0c;可每层使用一次for循环&#xff0c;循环次数为开始时队列的长度&#xff0c;即当前层的节点数。
注意&#xff1a;循环条件不能是从0到队列长为止&#xff0c;因为队列长度在变化。可以用一个变量记录下当前队列长&#xff0c;或者从队列长递减到0。
105. Construct Binary Tree from Preorder and Inorder Traversal
Java中哈希的使用&#xff1a;
private Map<Integer, Integer> indexMap;
indexMap &#61; new HashMap<Integer, Integer>();
indexMap.put(key, value);
int value&#61; indexMap.get(key);
int value&#61; indexMap.getOrDefault(key, -1);
406. Queue Reconstruction by Height
数组排序重写&#xff1a;
Arrays.sort(people, new Comparator<int[]>() { public int compare(int[] person1, int[] person2) {if (person1[0] !&#61; person2[0]) {return person2[0] - person1[0]; } else {return person1[1] - person2[1]; }}});
1482. Minimum Number of Days to Make m Bouquets补充&#xff1a;如何对int数组排序&#xff1f;
直接使用Arrays.sort(a,Collections.reverseOrder());
&#xff1b;
自己重写Comparator&#xff0c;数组必须为Integer对象数组才行&#xff01;
补充&#xff1a;其实这也得是对象数组。int降序要么转为integer&#xff0c;要么升序再颠倒
Java ArrayList toArray() 方法的语法为&#xff1a;
arraylist.toArray(T[] arr)
T [] arr&#xff08;可选参数&#xff09;- 用于存储数组元素的数组
如果参数 T[] arr 作为参数传入到方法&#xff0c;则返回 T 类型的数组&#xff08;否则Object型&#xff09;
349. Intersection of Two Arrays补充&#xff1a;Integer类型的ArrayList不能使用toArray()转成int []&#xff0c;只能循环。
可以先声明一个长数组&#xff0c;再使用Arrays.copyOfRange(T[ ] original,int from,int to);
实现数组复制去除多余的零元素。
437. Path Sum III
List元素修改函数为&#xff1a;arraylist.set(int index, E element)
。
无论是使用for迭代还是get()得到的元素都不能直接赋值修改。
103. Binary Tree Zigzag Level Order Traversal
假设有以下代码&#xff1a;
List<Integer> slist&#61;new ArrayList();
slist.add(1);
List<List<Integer>> list&#61;new ArrayList();
list.add(slist);
slist&#61;new ArrayList();
那么问题来了&#xff0c;list中第一个元素是空列表还是包含“1”的列表&#xff1f;
实际上是包含“1”的列表&#xff0c;因为Java的集合类的元素是对象引用&#xff0c;但注意slist本身也只是引用&#xff0c;真正在list.add(slist)
添加的引用是第一行的new ArrayList()&#xff1a;
参考Java中list存放的是值还是对象的引用问题
1269. Number of Ways to Stay in the Same Place After Some Steps
动态规划&#xff0c;两个数组交替运行下面的语句&#xff1a;
nw[j]&#61;od[j]&#43;od[j-1]&#43;od[j&#43;1];
题目中温馨提示&#xff1a;
Since the answer may be too large, return it modulo 10^9 &#43; 7.
如果不做任何处理&#xff0c;数组元素不断达到231−12^{31}-1231−1然后负溢出。处理也很简单&#xff0c;因为题目计算只有加法&#xff0c;所以在计算的时候不断模109&#43;710^{9}&#43;7109&#43;7就可以了。
109&#43;710^{9}&#43;7109&#43;7是接近于int表示最大正数的一个大质数&#xff0c;这个数的两倍还在int表示范围内&#xff0c;三倍就会溢出。如果我们把该语句改成
nw[j]&#61;(od[j]%m&#43;od[j-1]%m&#43;od[j&#43;1]%m)%m;
是否就可以了呢&#xff1f;答案是否定的。od[j]%m
这一步没有必要&#xff0c;因为我们计算时已经保证od数组都不会大于m&#xff1b;od[j]%m&#43;od[j-1]%m&#43;od[j&#43;1]%m
这一步反而是有可能溢出的&#xff0c;如果这三个元素都比较接近m的话。由于两个m相加不会溢出&#xff0c;所以应该改成&#xff1a;
nw[j]&#61;((od[j]&#43;od[j-1])%m&#43;od[j&#43;1])%m;
912. Sort an Array
堆排序要点&#xff1a;
左子节点、右子节点分别为
int lson &#61; (i << 1) &#43; 1;int rson &#61; (i << 1) &#43; 2;
循环条件是左子节点存在&#xff0c;循环内部取右子节点值时需要判断是否超范围&#xff1b;
要使结果为正序&#xff0c;需要建立大根堆&#xff0c;不断把根放最后&#xff1b;
调整堆参数要包括起止点。
189. Rotate Array
最大公约数求法&#xff1a;
public int gcd(int x, int y) {return y > 0 ? gcd(y, x % y) : x;}
145. Binary Tree Postorder Traversal
- 使用两个栈辅助。主栈不断弹出一个节点存入辅栈&#xff0c;再压入左右非空子节点&#xff0c;这样访问顺序为右子树→左子树→父节点&#xff0c;辅栈倒过来就对了&#xff1a;
public class Test{Stack<TreeNode> stack1 &#61; new Stack<TreeNode>();Stack<TreeNode> stack2 &#61; new Stack<TreeNode>();public void postOrder(TreeNode root){stack1.push(root);while(!stack1.isEmpty()){TreeNode pop &#61; stack1.pop();stack2.push(pop);if(pop.left!&#61;null)stack1.push(pop.left);if(pop.right!&#61;null)stack1.push(pop.right);}while(!stack2.isEmpty()){System.out.println(stack2.pop().val&#43;" ");}}
- 使用一个集合存储第一次回溯遇到的父节点&#xff08;或使用标志位&#xff09;&#xff1a;
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> ans &#61; new ArrayList<>();Stack<TreeNode> s &#61; new Stack<>();Set<TreeNode> seen &#61; new HashSet<>();while (root !&#61; null || !s.isEmpty()) {if (root &#61;&#61; null && seen.contains(s.peek())) {ans.add(s.pop().val);} else if (root &#61;&#61; null) {seen.add(s.peek());root &#61; s.peek().right;} else {s.push(root);root &#61; root.left;}}return ans;}
}
- 只需要一个栈和一个辅助节点prev&#xff0c;不断向左遍历压栈&#xff0c;到头不断向右遍历压栈&#xff0c;到头记录并更新prev&#xff0c;从而根据
root.right &#61;&#61; prev
判是否为第二次回到root&#xff1a;
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res &#61; new ArrayList<Integer>();if (root &#61;&#61; null) {return res;}Deque<TreeNode> stack &#61; new LinkedList<TreeNode>();TreeNode prev &#61; null;while (root !&#61; null || !stack.isEmpty()) {while (root !&#61; null) {stack.push(root);root &#61; root.left;}root &#61; stack.pop();if (root.right &#61;&#61; null || root.right &#61;&#61; prev) {res.add(root.val);prev &#61; root;root &#61; null; } else {stack.push(root);root &#61; root.right;}}return res;}
}
142. Linked List Cycle II
关键在于2(a&#43;b)&#61;a&#43;b&#43;n(b&#43;c)⟹a&#61;c&#43;(n−1)(b&#43;c)2(a&#43;b)&#61;a&#43;b&#43;n(b&#43;c)⟹a&#61;c&#43;(n−1)(b&#43;c)2(a&#43;b)&#61;a&#43;b&#43;n(b&#43;c)⟹a&#61;c&#43;(n−1)(b&#43;c)&#xff0c;这里把a放到等式一端&#xff0c;就能看出来所求的头节点到入环点的距离与相遇点到入环点的距离差了整环长的倍数。