摘要:
310. Minimum Height Trees queue: degree为1的顶点 degree[ i ] : 和 i 顶点关联的边数。 先添加整个图,然后BFS删除每一层degree为1的节点。 class Solution { public List<Integer> findMinHei 阅读全文
摘要:
286. Walls and Gates DFS: 思路是,搜索0的位置,每找到一个0,以其周围四个相邻点为起点,开始 DFS 遍历,并带入深度值1,如果遇到的值大于当前深度值,将位置值赋为当前深度值,并对当前点的四个相邻点开始DFS遍历,注意此时深度值需要加1 class Solution { p 阅读全文
摘要:
369. Plus One Linked List 1.第1次while: 从前往后找到第一个不是9的位,记录。 2.第2次while: 此位+1,后面的所有值设为0(因为后面的位都是9)。 返回时注意可能所有位数为9,需要在最前面添加一位,如果dummy.val == 1,则返回dummy位。 时 阅读全文
摘要:
254. Factor Combinations class Solution { public List<List<Integer>> getFactors(int n) { List<List<Integer>> res = new ArrayList<>(); //corner case if 阅读全文
摘要:
298. Binary Tree Longest Consecutive Sequence 先序遍历,根左右。如果该节点的 value == 父节点value + 1, 则长度+1; 否则重置为1。 class Solution { private int res = 0; public int l 阅读全文