
LC二分搜索算法
LC二分搜索算法的专栏
996冲冲冲
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode793. 阶乘函数后 K 个零
找出满足条件k的最大的n和最小的n分别是多少,两数相减再加1就是答案。计算可以用二分搜索来减少时间,算阶乘的0的个数参考172题。原创 2023-02-19 16:21:16 · 117 阅读 · 0 评论 -
leetcode354. 俄罗斯套娃信封问题
思路:因为不允许相同宽度或者长度的信封嵌套,所以可以先对长度进行升序排序,长度相同对宽度进行降序排序。之后再根据宽度计算最长子序列,这样就避免了相同的宽度或者长度嵌套。最后就是经典的计算最长子序列的方法了。动态规划计算最长子序列,超时。原创 2023-01-18 23:07:24 · 209 阅读 · 0 评论 -
剑指 Offer 53 - II. 0~n-1中缺失的数字
异或运算def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ xor = 0 for i in range(len(nums)): xor ^= (i+1) ^ nums[i] #使缺失值前面的所有位都错开,缺失值开始后面的都对上。因为有0所以刚好错开位数等于缺失值 re原创 2022-05-05 16:57:05 · 332 阅读 · 0 评论 -
LC1631. 最小体力消耗路径
BFS, 找一条高度差的最大值最小的路径。二分法搜索当前给定一个值,看是否能到达最后一个节点,如果可以缩小这个值。知道最后找到最小的值def minimumEffortPath(self, heights): """ :type heights: List[List[int]] :rtype: int """ m,n = len(heights),len(heights[0]) left,right = 0,1原创 2022-05-03 19:23:05 · 377 阅读 · 0 评论 -
leetcode1300. 转变数组后最接近目标值的数组和
二分法def findBestValue(self, arr, target): l, r = 0, max(arr) def Sum(arr, k): sum = 0 for num in arr: sum += min(k, num) return sum while l < r: #这里取<的目的是保证l原创 2022-03-31 09:29:33 · 72 阅读 · 0 评论 -
leetcode658. 找到 K 个最接近的元素
二分法查找左端点def findClosestElements(self, arr, k, x): """ :type arr: List[int] :type k: int :type x: int :rtype: List[int] """ l,r = 0,len(arr)-k #因为是查找左起始点,所以这里是左起始点可能的范围 while l < r:原创 2022-03-30 10:51:26 · 124 阅读 · 0 评论 -
leetcode611. 有效三角形的个数
def triangleNumber(self, nums): """ :type nums: List[int] :rtype: int """ res = 0 nums.sort() for i in range(len(nums)-1,-1,-1): #先取最大的数 l,r = 0,i-1 # 最小的数和第二大的数 while l &...原创 2022-03-27 22:01:24 · 247 阅读 · 0 评论 -
二分搜索总结
二分搜索首先要注意模板,一般来说有两种:首先是l能等于r,之后在判断时也需要l或者r等于m最终使得一个边界出界,返回另一个边界。此时需要注意的是如果有重复元素,我们需要明白找上界还是下界,如果找上界,就是等于时让l+1,返回r找下届就是等于时让r-1,返回lwhile l <= r:m = l + (r - l) // 2if target <= nums[m]: #首先 找的时候由于相等时不会改变l只有大于时会改变l,所以最终l最找到等于target的第一个位置不动r =原创 2022-03-27 16:06:37 · 628 阅读 · 0 评论 -
leetcode410. 分割数组的最大值
def splitArray(self, nums, m): """ :type nums: List[int] :type m: int :rtype: int """ def helper(mid): i, sum = 1, 0 for x in nums: if sum + x > mid:原创 2022-03-27 11:04:38 · 542 阅读 · 0 评论 -
leetcode436. 寻找右区间
二分加哈希表加排序def findRightInterval(self, intervals): """ :type intervals: List[List[int]] :rtype: List[int] """ def bs(array,k): l,r = 0,len(intervals)-1 if array[r] < k: retur原创 2022-03-26 22:11:00 · 203 阅读 · 0 评论 -
leetcode4. 寻找两个正序数组的中位数
二分法 def findMedianSortedArrays(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: float """ def helper(nums1, nums2, k): i, j = 0, 0 while True:原创 2022-03-26 16:50:46 · 180 阅读 · 0 评论 -
leetcode441. 排列硬币
等比数列求和公式 an = n(a1+an)/2 an = a1 + (n-1)ddef arrangeCoins(self, n): """ :type n: int :rtype: int """ l,r = 0, n while l <= r: mid = l + (r-l)//2 if 2*n >= mid*(1+mid): l = mid + 1 els原创 2022-03-24 21:09:35 · 203 阅读 · 0 评论 -
leetcode275. H 指数 II
def hIndex(self, citations): """ :type citations: List[int] :rtype: int """ l, r = 0, len(citations) - 1 while l <= r: m = l + (r - l) // 2 if citations[m] >= len(citations) - m:原创 2022-03-24 19:53:06 · 101 阅读 · 0 评论 -
leetcode350. 两个数组的交集 II
排序加双指针def intersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ nums1.sort() nums2.sort() res = [] i,j = 0,0 while i < len(原创 2022-03-24 15:45:09 · 106 阅读 · 0 评论 -
leetcode349. 两个数组的交集
def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ def helper(nums1,nums2): res = [] for i in set(nums2): if i in set(nums1): res原创 2022-03-24 10:14:06 · 75 阅读 · 0 评论 -
leetcode400. 第 N 位数字
def findNthDigit(self, n): """ :type n: int :rtype: int """ x = 1 while n - 9 * x * (10 ** (x - 1)) > 0: n -= 9 * x * (10 ** (x - 1)) x += 1 print(x) n -= 1 #这里-1是因为从两位数开始每个数都是从0开始的。一位数的原创 2022-03-23 21:48:40 · 133 阅读 · 0 评论 -
leetcode367. 有效的完全平方数
在python 2.5版本中存在两种除法运算,即所谓的true除法和floor除法。第一,当使用x/y形式进行除法运算时,如果x和y都是整形,那么运算会对结果进行截取,取运算的整数部分,比如2/3的运算结果是0;如果x和y中有一个是浮点数,那么会进行所谓的true除法,比如2.0/3的结果是 0.66666666666666663。第二,另外一种除法是采用x//y的形式,那么这里采用的是所谓floor除法,即得到不大于结果的最大整数值,这个运算时与操作数无关的。比如2//3的结果是0,-2//3的结果是原创 2022-03-23 19:54:12 · 1134 阅读 · 0 评论 -
leetcode378. 有序矩阵中第 K 小的元素
def kthSmallest(self, matrix, k): """ :type matrix: List[List[int]] :type k: int :rtype: int """ def count(mid, n): #小于等于中间值的数 i, j = n, 0 sum = 0 while j <= n and i &g原创 2022-03-23 10:04:16 · 266 阅读 · 0 评论 -
leetcode222. 完全二叉树的节点个数
弄清递归很简单,相当于每次把二叉树给砍掉一般。不断的向下最终获得结果。class Solution(object): def countNodes(self, root): """ :type root: TreeNode :rtype: int """ def helper(root): level = 0 while root: lev原创 2022-03-22 21:22:40 · 345 阅读 · 0 评论 -
leetcode167. 两数之和 II - 输入有序数组
双指针class Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """ l,r = 0,len(numbers)-1 while l < r: sum = numbers原创 2022-03-21 21:52:06 · 182 阅读 · 0 评论 -
leetcode287. 寻找重复数
【代码】leetcode287. 寻找重复数。原创 2022-03-21 16:13:21 · 223 阅读 · 0 评论 -
leetcode34. 在排序数组中查找元素的第一个和最后一个位置
def searchRange(nums, target): res = [] l, r = 0, len(nums) - 1 while l <= r: m = l + (r - l) // 2 if target <= nums[m]: #首先 找的时候由于相等时不会改变l只有大于时会改变l,所以最终l最找到等于target的第一个位置不动 r = m - 1 #其次 小于等于时r缩小到m原创 2022-03-21 10:12:55 · 231 阅读 · 0 评论 -
leetcode35. 搜索插入位置
class Solution(object):def searchInsert(self, nums, target):“”":type nums: List[int]:type target: int:rtype: int“”"l, r = 0, len(nums) - 1while l <= r:m = l + (r - l + 1) // 2if nums[m] >= target: #大于等于的时候改变r,有target时r是小于target的第一个位置。原创 2022-03-21 09:32:36 · 63 阅读 · 0 评论 -
leetcode704. 二分查找
二分查找简单版class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ r = 0 ; l = len(nums)-1 while r <= l: m = r+(l-r)/2原创 2021-10-05 22:06:47 · 73 阅读 · 0 评论 -
Leetcode374. 猜数字大小
猜数字大小class Solution(object): def guessNumber(self, n): """ :type n: int :rtype: int """ l = 0 r = n while l <= r: pick = l + (r - l)/2 if guess(pick) == -1:原创 2021-10-07 15:36:51 · 100 阅读 · 0 评论 -
leetcode278. 第一个错误的版本
class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ l = 1 r = n while l < r: m = l + (r - l)//2 if isBadVersion(m): r原创 2021-10-07 16:52:15 · 64 阅读 · 0 评论 -
leetcode240. 搜索二维矩阵 II
class Solution(object): def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ row,col = 0, len(matrix[0]) - 1 while row < len(matrix) and原创 2022-03-20 15:19:52 · 77 阅读 · 0 评论 -
leetcode162. 寻找峰值
这里可以这样写的原因是如果nums[x+1]>nums[x],那么右边一定能找到的原因是如果出现降序就找到,如果一直升序那么最后一个数就是答案。否则向左nums[x+1]<nums[x]也是,如果降序出现了一个那么就找到,如果一直没降序,那么就是最后一个数。 所以不管舍弃的有没有,留下的一定能找到一个。class Solution(object): def findPeakElement(self, nums): """ :type nums: L原创 2022-03-03 21:29:20 · 165 阅读 · 0 评论 -
leetcode154. 寻找旋转排序数组中的最小值 II
class Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtype: int """ l,r = 0,len(nums)-1 while l < r: m = l + (r-l)//2 if nums[m] > nums[r]:原创 2022-03-20 15:18:19 · 71 阅读 · 0 评论 -
leetcode153. 寻找旋转排序数组中的最小值
class Solution(object): def findMin(self, nums): """ :type nums: List[int] :rtype: int """ l,r = 0,len(nums)-1 while l < r: m = l + (r-l)//2 if nums[m] > nums[r]:原创 2022-03-20 15:17:07 · 78 阅读 · 0 评论 -
leetcode81. 搜索旋转排序数组 II
class Solution(object): def search(self, nums, target): """ :type nums: List[int] :type target: int :rtype: bool """ l, r = 0, len(nums) - 1 while l <= r: m = l + (r - l) // 2原创 2022-03-20 15:15:36 · 79 阅读 · 0 评论 -
leetcode74. 搜索二维矩阵
def searchMatrix(self, matrix, target): """ :type matrix: List[List[int]] :type target: int :rtype: bool """ row,col = len(matrix) - 1, 0 while row >= 0 and col < len(matrix[0]): if原创 2022-03-20 15:13:22 · 75 阅读 · 0 评论 -
leetcode69. Sqrt(x)
查找x的平方根,并且结果向下取整。class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ l = 0 r = x ans = - 1 if x == 0: return 0 elif x==1: return原创 2021-10-05 22:05:42 · 77 阅读 · 0 评论 -
leetcode33. 搜索旋转排序数组
class Solution(object):def search(self, nums, target):“”":type nums: List[int]:type target: int:rtype: int“”"r = len(nums) - 1l = 0while l <= r:m = l + (r - l)/2if nums[m] == target:return mif nums[0] <= nums[m]: (注意此处有等于,否则r无法=l导致用例不通过原创 2021-10-07 15:30:41 · 77 阅读 · 0 评论