
剑指Offer
我会jvav啊
此人很懒,已经转行摸鱼了
展开
-
剑指 Offer 63. 股票的最大利润(dp)
剑指 Offer 63. 股票的最大利润维护一个买入价格 和 每次卖出的利润的最大值 public int maxProfit(int[] prices) { if (prices.length < 2) return 0; int res = 0; int buy = prices[0]; for (int i = 1; i < prices.length; i++) { if (prices[原创 2021-03-31 14:52:37 · 147 阅读 · 0 评论 -
剑指 Offer 47. 礼物的最大价值(dp)
题目 public int maxValue(int[][] grid) { int row = grid.length; int colum = grid[0].length; int dp[][] = new int[row+1][colum+1]; for (int i = 1; i < row+1; i++) { for (int j = 1; j < colum+1; j++) {原创 2021-03-31 14:27:26 · 120 阅读 · 0 评论 -
剑指 Offer 14- II. 剪绳子 II(dp)
题目long会炸 只能用BIgInteger了注意一下大数的乘法运算和取maxBigInteger.max(BigInteger a)返回自身和a的最大值import java.math.BigInteger;public class Offertwo { public int cuttingRope(int n) { if (n == 2) return 1; if (n == 3) return 2; BigInteger dp[] =原创 2021-03-31 13:50:04 · 131 阅读 · 0 评论 -
剑指 Offer 14- I. 剪绳子(dp)
所有的分解最后都会乘2或者乘3 public int cuttingRope(int n) { if (n == 2) return 1; if (n == 3) return 2; int dp[] = new int[n+1]; dp[0] = 1; dp[1] = 1; dp[2] = 2; dp[3] = 3; for (int i = 4; i < n+1.原创 2021-03-30 19:03:57 · 134 阅读 · 4 评论 -
剑指Offer面试题34. 二叉树中和为某一值的路径(dfs)
题目有很多坑 注意是叶子结点也就是走到二叉树底部开始用linkedlist写的时间2ms换了arraylist就1ms了挺不解的 希望以后理解了回来阐述一下class Solution { List<List<Integer>> res; int target; List<Integer> list; public List<List<Integer>> pathSum(TreeNode root, in原创 2021-03-29 16:44:26 · 156 阅读 · 8 评论 -
剑指 Offer 68 - II. 二叉树的最近公共祖先(递归)
题目 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root == null) return null; if (root.val == p.val || root.val == q.val) return root; TreeNode left = lowestCommonAncestor(root.left, p, q);原创 2021-03-29 15:25:04 · 160 阅读 · 0 评论 -
剑指 Offer 68 - I. 二叉搜索树的最近公共祖先(递归)
题目由于是二叉搜索树所以可以逐步确定区间如果都q和p的值都在当前结点的右侧 向右递归如果都q和p的值都在当前结点的左侧 向左递归如果在两侧直接返回当前结点 public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if (root.val > p.val && root.val > q.val) return lowestCo原创 2021-03-29 15:19:46 · 111 阅读 · 0 评论 -
剑指 Offer 54. 二叉搜索树的第k大节点(递归)
朴素想法就是反中序遍历 用list存下来 找第K个但是太麻烦用递归思维就是反中序遍历的时候遍历到第K个直接返回 int res = 0; int k; public int kthLargest(TreeNode root, int k) { this.k = k; disIn(root); return res; } void disIn(TreeNode root){//反中序遍历 if (k &.原创 2021-03-23 22:40:52 · 91 阅读 · 0 评论 -
剑指 Offer 32 - III. 从上到下打印二叉树 III(层序遍历加双端队列)
要求奇数层逆序那么利用双端队列的性质 奇数层每次添加到队列头 public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) return new ArrayList<>(); List<List<Integer>> res = new ArrayList<>(); Deque<TreeNo.原创 2021-03-23 20:42:55 · 145 阅读 · 2 评论 -
剑指 Offer 32 - II. 从上到下打印二叉树 II(层序)
public List<List<Integer>> levelOrder(TreeNode root) { if (root == null) return new ArrayList<>(); List<List<Integer>> res = new ArrayList<>(); Deque<TreeNode> queue = new LinkedList<...原创 2021-03-23 20:22:56 · 101 阅读 · 0 评论 -
剑指 Offer 32 - I. 从上到下打印二叉树(队列实现层序遍历)
用bfs思想 队列实现先进先出 public int[] levelOrder(TreeNode root) { if (root == null) return new int[0]; ArrayList<Integer> list = new ArrayList<>(); Deque<TreeNode> queue = new LinkedList<>(); queue.offer(.原创 2021-03-23 20:16:42 · 103 阅读 · 0 评论 -
剑指 Offer 28. 对称的二叉树(递归)
题目递归找当前左右结点的值是否相等 并且当前左结点的左结点是否等于当前右结点的右节点 并且 当前左节点的右节点等于当前右节点的左节点 public Boolean recur(TreeNode left ,TreeNode right){ if (left == null && right == null) return true; if (left == null || right == null) return false; r原创 2021-03-23 19:33:24 · 82 阅读 · 0 评论 -
剑指 Offer 27. 二叉树的镜像(递归)
题目和翻转二叉树同题 public TreeNode mirrorTree(TreeNode root) { if (root == null) return null; TreeNode tmp = root.left; root.left = root.right; root.right = tmp; mirrorTree(root.left); mirrorTree(root.right);原创 2021-03-23 13:01:22 · 122 阅读 · 1 评论 -
剑指 Offer 26. 树的子结构(递归)
题目我的想法是找到A树每一个与B树跟结点相同的结点 放入List中遍历list中的结点当做起点去判断 是否B树是A树的子结构class Solution { ArrayList<TreeNode> list = new ArrayList<>(); public void find(TreeNode a,TreeNode b){ if (a == null) return; if (a.val == b.val) list.ad原创 2021-03-23 12:52:48 · 89 阅读 · 0 评论 -
剑指 Offer 55 - I. 二叉树的深度(递归)
public int maxDepth(TreeNode root) { if(root == null) return 0; return Math.max(maxDepth(root.left),maxDepth(root.right))+1; }原创 2021-03-23 11:46:57 · 97 阅读 · 0 评论 -
剑指 Offer 55 - II. 平衡二叉树(递归)
题目判断是否为平衡二叉树需要判断每一个结点的左右子树深度相差是否不超过1第一时间想到了递归 然后过程中用一个flag记录是否平衡 后来去题解学习了一遍nb的写法全局flag法class Solution { boolean flag = true; public int getDeep(TreeNode root){ if (root == null) return 0; return Math.max(getDeep(root.left),getD原创 2021-03-23 11:44:04 · 161 阅读 · 2 评论 -
剑指 Offer 07. 重建二叉树(递归)
递归思想 每个点都当做跟结点去中序数组中划分左右子树 HashMap<Integer,Integer> map = new HashMap<>(); private TreeNode build(int[] pre ,int l1 ,int r1 ,int l2 ,int r2){ if (l1 > r1) return null; TreeNode root = new TreeNode(pre[l1]); i.原创 2021-03-22 21:33:17 · 94 阅读 · 0 评论 -
剑指 Offer 06. 从尾到头打印链表(栈)
public int[] reversePrint(ListNode head) { if (head == null) return new int[0]; Deque<Integer> stack = new LinkedList<>(); int cnt = 0; while (head != null){ stack.push(head.val); he...原创 2021-03-22 16:05:24 · 70 阅读 · 0 评论 -
剑指 Offer 05. 替换空格(字符串)
上来就知道一行代码就可以完成 但是又怕面试的时候认为是投机取巧 再来个朴素写法,时间复杂度一样class Solution { public String replaceSpace(String s) { s = s.replace(" ","%20"); return s; }} public String replaceSpace(String s) { StringBuilder sb = new StringBuild.原创 2021-03-22 15:59:30 · 91 阅读 · 0 评论 -
剑指 Offer 04. 二维数组中的查找(二分思维)
题目从左下为起点进行二分查找当前坐标值小于target向右找当前坐标值大于target向上找 public boolean findNumberIn2DArray(int[][] matrix, int target) { int i = matrix.length - 1; int j = 0; while (i >= 0 && j < matrix[0].length){ if (matr原创 2021-03-22 15:41:19 · 381 阅读 · 0 评论 -
剑指 Offer 03. 数组中重复的数字(哈希表或排序)
题目哈希表 8msclass Solution { public int findRepeatNumber(int[] nums) { HashSet<Integer> set = new HashSet<>(); for (int i = 0; i < nums.length; i++) { if (set.contains(nums[i])) return nums[i]; set原创 2021-03-22 15:23:57 · 151 阅读 · 0 评论