We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
都是O(n)
LeetCode 102题,二叉树的层序遍历
思路:
DFS-Java
class Solution { public List<List<Integer>> res = new LinkedList<>(); public List<List<Integer>> levelOrder(TreeNode root) { dfs(0, root); return res; } private void dfs(int level, TreeNode root){ if(root == null) return; if(res.size() <= level){ res.add(new LinkedList<>()); } res.get(level).add(root.val); dfs(level+1, root.left); dfs(level+1, root.right); } }
BFS-Java
class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> res = new LinkedList<>(); Queue<TreeNode> queue = new LinkedList<>(); if(root == null) return res; queue.add(root); while(!queue.isEmpty()){ int level = queue.size(); List<Integer> tempList = new LinkedList<>(); for(int i = 0; i < level; i++){ if(queue.peek().left != null) queue.add(queue.peek().left); if(queue.peek().right != null) queue.add(queue.peek().right); tempList.add(queue.poll().val); } res.add(tempList); } return res; } }
二分查找的本质是在一组单调、有上下界、可索引的数据中搜索目标值,三大条件缺一不可。
O(logN)
O(1)
LeetCode 69 题,x的平方根
二分查找,因为y=x^2在x正半轴单调递增,且存在上下界1和x,所以可以使用二分查找逼近
牛顿迭代法,利用切线逼近,公式为 x = (x + a/x) / 2
二分查找
class Solution { public int mySqrt(int x) { if(x == 0 || x == 1) return x; long left = 1, right = x, mid = 1; while(left <= right){ mid = left + (right - left) / 2; if(mid * mid > x){ right = mid - 1; } else { left = mid + 1; } } return (int) right; } }
牛顿迭代法
class Solution { public int mySqrt(int x) { long r = x; while (r * r > x) { r = (r + x / r) / 2; } return (int)r; } }
贪心的本质是通过每一步的局部最优,期望实现全局最优的一种算法思想。关键在于局部最优是否真的能实现全局最优。如果能实现,那么贪心算法基本上就是问题的最优解。
LeetCode 55题,跳跃游戏
倒序贪心
class Solution { public boolean canJump(int[] nums) { if (nums == null) return false; int endReachable = nums.length - 1; for (int i = nums.length - 1; i >= 0; i--){ if(nums[i] + i >= endReachable){ endReachable = i; } } return endReachable == 0; } }
正序贪心
class Solution { public boolean canJump(int[] nums) { int maxPos = 0; for (int i = 0; i < nums.length; i++){ if(i > maxLen) return false; maxPos = Math.max(maxPos, nums[i] + i); if(maxPos >= nums.length-1) break; } return true; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
深度优先搜索DFS、广度优先搜索BFS
比较
时间复杂度
都是O(n)
空间复杂度
都是O(n)
经典题目
LeetCode 102题,二叉树的层序遍历
思路:
DFS-Java
BFS-Java
二分查找
本质
二分查找的本质是在一组单调、有上下界、可索引的数据中搜索目标值,三大条件缺一不可。
时间复杂度
O(logN)
空间复杂度
O(1)
经典题目
LeetCode 69 题,x的平方根
思路:
二分查找,因为y=x^2在x正半轴单调递增,且存在上下界1和x,所以可以使用二分查找逼近
牛顿迭代法,利用切线逼近,公式为 x = (x + a/x) / 2
二分查找
牛顿迭代法
贪心算法
本质
贪心的本质是通过每一步的局部最优,期望实现全局最优的一种算法思想。关键在于局部最优是否真的能实现全局最优。如果能实现,那么贪心算法基本上就是问题的最优解。
经典例题
LeetCode 55题,跳跃游戏
思路:
倒序贪心
正序贪心
The text was updated successfully, but these errors were encountered: