Leetcode 刷题 Binary Search Easy难度经验总结
来源:互联网 发布:如何防止网络麻将开挂 编辑:程序博客网 时间:2024/06/18 14:46
刷了4道Binary Search Easy难度的题
二叉搜索思想好想,难在边界的控制上,于是乎我就在思考能不能背下一个定式,遇到二分搜索的题目就套这个定式。
我做的第一道二叉搜索的题目就是一个很好的定式:
744. Find Smallest Letter Greater Than Target
Given a list of sorted characters letters
containing only lowercase letters, and given a target letter target
, find the smallest element in the list that is larger than the given target.
Letters also wrap around. For example, if the target is target = 'z'
and letters = ['a', 'b']
, the answer is 'a'
.
Examples:
Input:letters = ["c", "f", "j"]target = "a"Output: "c"Input:letters = ["c", "f", "j"]target = "c"Output: "f"Input:letters = ["c", "f", "j"]target = "d"Output: "f"Input:letters = ["c", "f", "j"]target = "g"Output: "j"Input:letters = ["c", "f", "j"]target = "j"Output: "c"Input:letters = ["c", "f", "j"]target = "k"Output: "c"
Note:
letters
has a length in range[2, 10000]
.letters
consists of lowercase letters, and contains at least 2 unique letters.target
is a lowercase letter.
class Solution(object): def nextGreatestLetter(self, letters, target): """ :type letters: List[str] :type target: str :rtype: str """ low = 0 high = len(letters) while (low < high) : mid = low + (high - low) / 2 if (letters[mid] <= target): low = mid + 1 else: high = mid return letters[low % len(letters)]这里面的low,high都是指脚标
34. Search for a Range
Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
class Solution(object): def searchRange(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ if len(nums)==0:#空输入处理 return [-1,-1] low = 0 high = len(nums) while (low < high): mid = low + int((high - low) / 2) if (nums[mid] <= target): low = mid + 1 else: high = mid start=low-1#target所在的位置,因为low是比target大的数中最小的一个 end=low-1#target所在的位置,因为low是比target大的数中最小的一个 if (nums[end] != target ):#target不存在于数组中 return [-1,-1] else:#target存在于数组中 while (start != 0 and nums[start] == nums[start - 1]):#看看start左边是不是还有一样的值 start = start - 1 return [start,end]完美地套用了我们的定式,
最后的if -else分支是根据本题再做的逻辑补充
374.Guess Number Higher or Lower
We are playing the Guess Game. The game is as follows:
I pick a number from 1 to n. You have to guess which number I picked.
Every time you guess wrong, I'll tell you whether the number is higher or lower.
You call a pre-defined API guess(int num)
which returns 3 possible results (-1
, 1
, or 0
):
-1 : My number is lower 1 : My number is higher 0 : Congrats! You got it!
Example:
n = 10, I pick 6.Return 6.
# The guess API is already defined for you.# @param num, your guess# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0# def guess(num):class Solution(object): def guessNumber(self, n): """ :type n: int :rtype: int """ low=1#Low index high=n+1#high index while (low<high): mid=low+int((high-low)/2) if (guess(mid)==1):#mid<=target low=mid+1 elif(guess(mid)==-1): high =mid elif(guess(mid)==0): return mid
排序的数组不是nums参数的形式,而是给了一个参数n,对应数组1,2,....n
high在这里还是指向 数组最后一个数还往后移一位
仍然符合定式
278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version are also bad.
Suppose you have n
versions [1, 2, ..., n]
and you want to find out the first bad one, which causes all the following ones to be bad.
You are given an API bool isBadVersion(version)
which will return whether version
is bad. Implement a function to find the first bad version. You should minimize the number of calls to the API.
# The isBadVersion API is already defined for you.# @param version, an integer# @return a bool# def isBadVersion(version):class Solution(object): def firstBadVersion(self, n): """ :type n: int :rtype: int """ low=1#low_index high=n+1#high_index while(low<high): mid=low+int((high-low)/2) if(isBadVersion(mid)==True): high=mid else: low=mid+1 return low
因为现在比如对于 1, 2, 3这个数组
脚标low =1,high=4 类比可得应该return low
并不是实际上的 low_index=0 ,high_index=3 return low-1
依然是定式的变形
总结:
记住定式的套路(<= ,等啊不等啊什么的)
依据题意和实际情况改一下low,high的初始值
和return的值
这个套路目前为止没失败过
- Leetcode 刷题 Binary Search Easy难度经验总结
- leetcode JAVA Unique Binary Search Trees 难度系数3 3.34
- leetcode JAVA Validate Binary Search Tree 难度系数3 3.35
- 235. Lowest Common Ancestor of a Binary Search Tree 难度:easy
- LeetCode刷题(C++)——Convert Sorted Array to Binary Search Tree(Easy)
- LeetCode-Easy刷题(23) Convert Sorted Array to Binary Search Tree
- LeetCode 669. Trim a Binary Search Tree (Easy)
- LeetCode-Easy刷题(10) Search Insert Position
- leetcode Convert Sorted Array to Binary Search Tree 2.20 难度系数2
- leetcode JAVA Unique Binary Search Trees II 4.28 难度系数4
- LeetCode-Easy刷题(14) Add Binary
- LeetCode-Easy刷题(24) Balanced Binary Tree
- 【leetcode】Add Binary[easy]
- LeetCode[136. Single Number] 难度[easy]
- LeetCode[169. Majority Elemen] 难度[easy]
- LeetCode [292. Nim Game] 难度[easy]
- [Leetcode 235, Easy] Lowest Common Ancestor of a Binary Search Tree
- leetcode 235 Lowest Common Ancestor of a Binary Search Tree(难易度:Easy)
- linux基本目录结构
- Windows下用Tensorflow中Android示例实现实时目标检测
- ASP浏览器版本检测+操作系统判断检测
- 重新编译Linux内核踩的坑
- python: return 函数
- Leetcode 刷题 Binary Search Easy难度经验总结
- 清澄A1051. DNA序列
- javascript兼容获取scrollLeft和scrollTop
- Sun的逆向之路(二)——FiF口语训练的刷分剖析
- 设计模式——模板方法模式
- 19. Remove Nth Node From End of List
- ssh框架思维导图
- spring MVC 视屏在线播放(不限 )
- MONGODB的基础知识