Leetcode Find Minimum in Rotated Sorted Array I and II
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
在任何一个sublist中,如果头元素大于尾元素,那么这个minimum一定在这个sublist中间的某一个位置。可以用二分法找到这个元素,复杂度是O(logN)。本题O(N)的算法也可以通过OJ,思路就是最简答的从头元素往后比,直到出现 nums[i] > nums[i+1]的情况,那么nums[i+1]就是Minimum element。
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
left = 0
right = n-1
while left < right:
mid = (left+right)//2
if nums[mid] > nums[right]:
left = mid + 1
else:
right = mid
return nums[left]
以下引用来自: http://bangbingsyb.blogspot.com/2014/11/leecode-find-minimum-in-rotated-sorted.html
3 3 3 3 1 2 3
class Solution(object):
def findMin(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n = len(nums)
left = 0
right = n-1
while left < right:
mid = (left+right)//2
if nums[mid] > nums[right]:
left = mid + 1
elif nums[mid] < nums[right]:
right = mid
else:
right -= 1 return nums[left]
Leetcode Find Minimum in Rotated Sorted Array I and II的更多相关文章
- Leetcode Find Minimum in Rotated Sorted Array 题解
Leetcode Find Minimum in Rotated Sorted Array 题目大意: 对一个有序数组翻转, 就是随机取前K个数,移动到数组的后面,然后让你找出最小的那个数.注意,K有 ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
- LeetCode Find Minimum in Rotated Sorted Array
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Method 1 就是找到第一个违反升序的值,就 ...
- Leetcode | Find Minimum in Rotated Sorted Array I && II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode——Find Minimum in Rotated Sorted Array II
Question Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allo ...
- LeetCode——Find Minimum in Rotated Sorted Array
Description: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 ...
- [LeetCode] Find Minimum in Rotated Sorted Array 二分搜索
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- web cache server方案比较:varnish、squid、nginx
linux运维中,web cache server方案的部署是一个很重要的环节,选择也有很多种比如:varnish.squid.nginx.下面就对当下常用的这几个web cache server做一 ...
- python实现一个图灵机器人
这标题就是个噱头...其实用的别人的接口,就是这货. 下面是代码: # -*- coding: utf-8 -*- import urllib,urllib2 import sys import js ...
- asp.net sql 分页,,优化 排序 及分页,
调用代码: <%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix ...
- 截取视图某一段另存为部分视图(Partial View)
在做ASP.NET MVC后台管理程序时,根据程序需要,Isus.NET需要实现一个功能,就是动态截取视图某一段另存为部分视图Partial View. 思路为在视图中,使用jQury的程序截图以及P ...
- C# 6.0
C# 6.0 的新语法特性 回眸 C# 的前世今生 - 见证 C# 6.0 的新语法特性 序 目前最新的版本是 C# 7.0,VS 的最新版本为 Visual Studio 2017 RC,两者都 ...
- Linux 中 Weblogic 启动和关闭
a.start weblogic1)找到 /Oracle/Middleware/user_projects/domains/ 用户_domain目录. nohup ./startWebLogic.sh ...
- sql基本命令
--------------------------------------------------------SQL基本命令开始----------------------------------- ...
- 【算法之美】你可能想不到的归并排序的神奇应用 — leetcode 327. Count of Range Sum
又是一道有意思的题目,Count of Range Sum.(PS:leetcode 我已经做了 190 道,欢迎围观全部题解 https://github.com/hanzichi/leetcode ...
- SQL Server Data Tools – Business Intelligence for Visual Studio 2012安装时提示“The CPU architecture....”的解决方法
SQL Server Data Tools – Business Intelligence for Visual Studio 2012,一个很强大的工具,下载地址:http://www.micros ...
- 20160205 - Windows 10 家庭版没有组策略
问题描述:买笔电自带的正版系统,有一个需要使用gpedit.msc,发现并不存在. 解决办法:升级到 Windows 10 专业版