![](https://img-blog.csdnimg.cn/20201014180756918.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode Binary Search
Tech In Pieces
这个作者很懒,什么都没留下…
展开
-
LeetCode424. Longest Repeating Character Replacement
//这题目待挺奇怪 给一个字符串 只含有大写 然后我们可以在其中任选k个变成其他的大写字母 //我们要求返回最长的只含有一个字母的子字符串 只要求返回长度就行//也就是说 我们要细致选取要要变化的那一部分//这道题真的是一点思路都没有//直接看答案吧class Solution { public int characterReplacement(String s, int k) { int len = s.length(); int[] count = n原创 2021-08-25 04:50:25 · 81 阅读 · 0 评论 -
LeetCode 360. Sort Transformed Array
360. Sort Transformed ArrayDifficulty: MediumRelated Topics: Array, Math, Two Pointers, SortingGiven a sorted integer array nums and three integers a, b and c, apply a quadratic function of the form f(x) = ax<sup>2</sup> + bx + c to each ele原创 2021-08-25 04:49:40 · 201 阅读 · 0 评论 -
LeetCode 345. Reverse Vowels of a String
题意跟简单 就是反转一个string里面所有的vowels就好像其他的非vowels不存在一样。很简单 就是知道几个重要函数String.valueOf(char[] chars): StringSystem.out.println(Arrays.toString(chars))class Solution { public String reverseVowels(String s) { if (s.length() <= 1) return s;原创 2021-08-25 04:49:10 · 104 阅读 · 0 评论 -
LeetCode 287. Find the Duplicate Number
//找出唯一的duplicate number 不能修改nums 用O(1)space//想到了swap 这里有个技巧 我们swap到对应的index 如果nums[index]被index这个数已经占了 就说明index这个数字有两个//但是写完开始跑才发现不太对 因为swap了之后 index=i位置上并没有“排好” 而是index=nums[i]的排好了。所以这个时候i往后走是很不负责任的。class Solution { public int findDuplicate(int[]原创 2021-08-25 04:48:29 · 93 阅读 · 0 评论 -
Binary Search相关精简总结
我们推荐左闭右闭写法。就是说lo = 0, hi = len - 1;binarySearch的写法 根据我们的不同需要,一般有以下几个重点:while(left < right) OR while (left <= right) OR while(left < right - 1)left = mid/mid+1 right = mid/mid-1;return left OR right OR left -1 etc首先 我们要注意 如果我们想写left = mid或者ri原创 2021-07-24 04:47:38 · 91 阅读 · 0 评论 -
LeetCode: Hidden Problems that can be Solved by Binary Search -- 那些能用二分搜索解决的问题合集
4.Median of Two Sorted Arrays29.Divide Two Integers50.Pow(x, n)69.Sqrt(x)162. Find Peak Element163. Dungeon Game164. Minimum Size Subarray Sum165. Count Complete Tree Nodes166. Kth Smallest Element in a BST167. H-Index II278 first bad version287原创 2020-12-19 07:17:09 · 154 阅读 · 0 评论 -
LeetCode035 Search Insert Position
given an array that contains no duplicate value, and a target value, search the insert position this target should be.it’s just a regular binary search.class Solution { public int searchInsert(int[] nums, int target) { int left = 0, right = nums.le原创 2020-12-12 00:56:40 · 121 阅读 · 0 评论 -
BinarySearch边界处理
https://www.1point3acres.com/bbs/thread-432793-1-1.html如果左闭右开 即l=0 r=len,那么r指针是取不到的 while只是小于号就还可以。当target大于中值,l=mid+1, 当target小于中值,r=mid.如果左闭右闭 即l=0 r=len-1,那么r指针是要取到的 while需要用小于等于号。当target大于中值,l=m...原创 2020-12-12 00:55:05 · 167 阅读 · 0 评论 -
Thoughts about Binary Search (From LeetCode 378)
inspired by Leetcode 378The key point for any binary search is to figure out the “Search Space”. For me, I think there are two kind of “Search Space” – index and range(the range from the smallest num...原创 2020-12-11 08:06:50 · 181 阅读 · 0 评论