摘要: 73. Set Matrix Zeroes - 先扫描第一行第一列,如果有0,则将各自的flag设置为true- 然后扫描除去第一行第一列的整个数组,如果有0,则将对应的第一行和第一列的数字赋0- 再次遍历除去第一行第一列的整个数组,如果对应的第一行和第一列的数字有一个为0,则将当前值赋0- 最后根 阅读全文
posted @ 2019-11-25 16:49 阿飞哦 阅读(102) 评论(0) 推荐(0) 编辑
摘要: 154. Find Minimum in Rotated Sorted Array II 当数组中存在大量的重复数字时,就会破坏二分查找法的机制,将无法取得 O(lgn) 的时间复杂度,又将会回到简单粗暴的 O(n),比如这两种情况:{2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 1, 阅读全文
posted @ 2019-11-24 23:32 阿飞哦 阅读(96) 评论(0) 推荐(0) 编辑
摘要: 160. Intersection of Two Linked Lists 分别从AB循环两次。如果第一次没循环到,第二次就会在节点相遇。 public class Solution { public ListNode getIntersectionNode(ListNode headA, List 阅读全文
posted @ 2019-11-23 17:37 阿飞哦 阅读(338) 评论(0) 推荐(0) 编辑
摘要: 120. Triangle 从倒数第二行找,然后逐个遍历这个DP数组,对于每个数字,和它之后的元素比较选择较小的再加上面一行相邻位置的元素做为新的元素,然后一层一层的向上扫描 class Solution { public int minimumTotal(List<List<Integer>> t 阅读全文
posted @ 2019-11-22 21:50 阿飞哦 阅读(72) 评论(0) 推荐(0) 编辑
摘要: 216. Combination Sum III 保证subset.size( ) == k && target == 0的时候结束DFS subset.size( ) > k || target < 0返回。 class Solution { public List<List<Integer>> 阅读全文
posted @ 2019-11-21 23:44 阿飞哦 阅读(93) 评论(0) 推荐(0) 编辑
摘要: 39. Combination Sum Combination,组合问题用DFS罗列全部的答案。 class Solution { public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List< 阅读全文
posted @ 2019-11-21 16:07 阿飞哦 阅读(99) 评论(0) 推荐(0) 编辑
摘要: 110. Balanced Binary Tree 方法是如果我们发现子树不平衡,则不计算具体的深度,而是直接返回-1。那么优化后的方法为:对于每一个节点,我们通过checkDepth方法递归获得左右子树的深度,如果子树是平衡的,则返回真实的深度,若不平衡,直接返回-1,此方法时间复杂度O(N),空 阅读全文
posted @ 2019-11-20 21:37 阿飞哦 阅读(88) 评论(0) 推荐(0) 编辑
摘要: 113. Path Sum II 利用DFS的三要素, 出口1,出口2,拆解,记得回溯的时候要回退一位path。 class Solution { public List<List<Integer>> pathSum(TreeNode root, int sum) { List<List<Integ 阅读全文
posted @ 2019-11-20 14:40 阿飞哦 阅读(129) 评论(0) 推荐(0) 编辑
摘要: 50. Pow(x, n) abs (Integer.MIN_VALUE) > Integer.MAX_VALUE class Solution { public double myPow(double x, int n) { if(n == 0) return 1; if(n == Integer 阅读全文
posted @ 2019-11-19 20:57 阿飞哦 阅读(94) 评论(0) 推荐(0) 编辑
摘要: 49. Group Anagrams class Solution { public List<List<String>> groupAnagrams(String[] strs) { HashMap<String, List<String>> map = new HashMap<>(); if(s 阅读全文
posted @ 2019-11-18 21:08 阿飞哦 阅读(114) 评论(0) 推荐(0) 编辑