[数据结构]排序、二分查找 发表于 2019-05-17 参加Datawhale的编程计划的第3个任务,涉及排序、二分查找,用python实现。。 排序练习:Sliding Window Maximum12345678910111213141516171819202122232425class Solution(object): def maxSlidingWindow(self, nums, k): """ :type nums: List[int] :type k: int :rtype: List[int] """ if not nums: return [] highest = max(nums[:k]) ans = [0]*(len(nums)-k+1) ans[0] = highest last = nums[0] for i in range(1, len(nums)-k+1): if highest == last: highest = max(nums[i:i+k]) elif nums[i+k-1] >= highest: highest = nums[i+k-1] ans[i] = highest last = nums[i] return ans 二分查找练习:Sqrt(x) (x 的平方根)123456789101112131415161718192021class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ if x == 0: return 0 left = 1 right = x / 2 + 1 while left <= right: mid = left + (right - left) / 2 sq = x / mid if sq > mid: left = mid + 1 elif sq < mid: right = mid - 1 else: return mid return right 相关链接编程计划的第3个任务第三次任务 打卡表格