
二分查找
力扣和剑指of的二分查找不会题
水之积也不厚,则其负大舟也无力
渣硕求个大厂实习。。。
展开
-
LeetCode 540. 有序数组中的单一元素
官方题解//解法一:判断左右两边哪个长度为奇数,答案就在哪边class Solution { public int singleNonDuplicate(int[] nums) { int l = 0; int r = nums.length - 1; int L = 0;//左边长度 int R = 0;//右边长度 while(l < r){ int mid = l + r >.原创 2021-02-01 22:26:10 · 98 阅读 · 0 评论 -
LeetCode 300. 最长递增子序列
//解法一:自己写出来的基本的动态规划class Solution { public int lengthOfLIS(int[] nums) { int n = nums.length; int[] dp = new int[n]; dp[0] = 1; int ans = 1; for(int i = 1; i < n; i++){ int len = 0; f.原创 2021-02-01 21:50:17 · 89 阅读 · 0 评论 -
LeetCode 287. 寻找重复数
//解法一:排序+二分 自己写的 好慢。。。。class Solution { public int findDuplicate(int[] nums) { Arrays.sort(nums); for(int i = 0; i < nums.length; i++) System.out.print(nums[i] + " "); int n = nums.length; int l = 0; .原创 2021-02-01 20:51:57 · 104 阅读 · 2 评论 -
LeetCode 270 最接近的二叉搜索树值 (锁题)
https://blog.youkuaiyun.com/u011646912/article/details/102580812(转载)1.题目描述给定一个不为空的二叉搜索树和一个目标值 target,请在该二叉搜索树中找到最接近目标值 target 的数值。注意:给定的目标值 target 是一个浮点数题目保证在该二叉搜索树中只会存在一个最接近目标值的数示例:输入: root = [4,2,5,1,3],目标值 target = 3.714286 4 / \ 2 5...转载 2021-01-31 23:09:34 · 675 阅读 · 0 评论 -
LeetCode 275. H 指数 II
//其实能做出来的,起始的边界没想清楚public class Solution { // 以下的代码注释是 @coder_hezi 帮助添加的,在此表示感谢 public int hIndex(int[] citations) { int len = citations.length; if (len == 0 || citations[len - 1] == 0) { return 0; } .原创 2021-01-31 22:50:56 · 82 阅读 · 0 评论 -
LeetCode 240. 搜索二维矩阵 II
//写法一:逐行二分 可提前判断是否有解class Solution { public boolean searchMatrix(int[][] matrix, int target) { for(int i = 0; i < matrix.length; i++){ if(matrix[i][0] > target) break; if(matrix[i][matrix[i].len..原创 2021-01-31 22:25:49 · 83 阅读 · 0 评论 -
LeetCode 230. 二叉搜索树中第K小的元素
官方题解//解法一:利用中序遍历将值存储在数组里class Solution { public ArrayList<Integer> inorder(TreeNode root, ArrayList<Integer> arr) { if (root == null) return arr; inorder(root.left, arr); arr.add(root.val); inorder(root.right, arr); ret.原创 2021-01-31 21:23:41 · 105 阅读 · 0 评论 -
LeetCode 222. 完全二叉树的节点个数
官方二分解法//二分解法 真没想到class Solution { public int countNodes(TreeNode root) { if (root == null) { return 0; } int level = 0; TreeNode node = root; while (node.left != null) { level++; .原创 2021-01-31 20:52:25 · 78 阅读 · 0 评论 -
LeetCode 209. 长度最小的子数组(二分没写出来滑动窗口写出来了)
//解法一:前缀和 + 二分//https://leetcode-cn.com/problems/minimum-size-subarray-sum/solution/qian-zhui-he-er-fen-hua-dong-chuang-kou-by-coldme-/class Solution { public int minSubArrayLen(int s, int[] nums) { int n = nums.length; int[] sum = n.原创 2021-01-30 22:53:45 · 120 阅读 · 0 评论 -
LeetCode 174. 地下城游戏
//解法一:二分+正向dp//https://leetcode-cn.com/problems/dungeon-game/solution/er-fen-cwo-te-yao-sou-bao-by-tian-xin-yue-yuan-3/class Solution { private boolean search(int[][] d, int hp){ int m = d.length; int n = d[0].length; int[][.原创 2021-01-30 22:22:01 · 133 阅读 · 0 评论 -
LeetCode 154. 寻找旋转排序数组中的最小值 II(元素重复)
大佬题解.//解法一:分治class Solution { public int findMin(int[] nums) { int n = nums.length; int l = 0; int r = n - 1; return bsearch(nums, l, r); }/*发现了一个规律 递增的旋转数组找最小值 mid 只能与 right比较*/ private int bsearch(int[] .原创 2021-01-30 18:18:22 · 105 阅读 · 0 评论 -
LeetCode 153. 寻找旋转排序数组中的最小值(元素不重复)
【大佬题解】https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/solution/er-fen-fa-fen-zhi-fa-python-dai-ma-java-dai-ma-by-///解法一:mid只能与right比较不能与left比较public class Solution { public int findMin(int[] nums) { int len = nums.leng.原创 2021-01-30 12:59:36 · 132 阅读 · 0 评论 -
LeetCode 81. 搜索旋转排序数组 II(元素重复找target)
【大佬题解】https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/solution/er-fen-cha-zhao-by-liweiwei1419///解法一:mid 与 left 比较分类讨论public class Solution { public boolean search(int[] nums, int target) { int len = nums.length; .原创 2021-01-30 11:54:40 · 73 阅读 · 0 评论 -
LeetCode 33. 搜索旋转排序数组(元素不重复找target)
//解法一: 一次二分 需要考虑好多边界class Solution { public int search(int[] nums, int target) { int n = nums.length; int l = 0; int r = n - 1; while(l < r){ int mid = l + r >> 1; if(nums[mid] >= nu.原创 2021-01-30 11:16:06 · 109 阅读 · 0 评论 -
LeetCode 29. 两数相除
//快速乘法+二分//另附快速乘法和快速幂模板class Solution { public int divide(int a, int b) { long x = a, y = b; boolean isNeg = false; if ((x > 0 && y < 0) || (x < 0 && y > 0)) isNeg = true; if (x < 0) x =.原创 2021-01-30 10:47:35 · 108 阅读 · 0 评论 -
LeetCode 4. 寻找两个正序数组的中位数
//解法一://转化为找第k小的数class Solution { public double findMedianSortedArrays(int[] nums1, int[] nums2) { int n = nums1.length; int m = nums2.length; int tol = n + m; if(tol % 2 == 1)//奇数找第 tol / 2 + 1的数 return d.原创 2021-01-30 10:41:00 · 74 阅读 · 0 评论