简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。
简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。
题目:
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.
这是 LeetCode 上的一道算法题,题意是一个已经排序的数组,截断后重新拼接,找出数组中最小的数字。
这道题目用 Python 来实现的话太简单了。代码如下:
class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
return min(num)
用了 Python 内置的 min 函数,提交后通过,耗时是:Runtime: 184 ms
完成后被吐槽:“你这样有意思么?”,额,的确很没意思。那就考虑优化一下吧。
Python 内置的 min 函数是会遍历整个数组的,时间复杂度为 O(n),不过这个数组本来是有序的了,所以可以稍微做点优化,代码如下:
class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
for i in range(0, len(num)-1):
if(num[i] > num[i+1]):
return num[i+1]
return num[0]
当找到第一个值小于它之前的数字的时候,就可以结束循环了。此时代码理论上的复杂度还是 O(n),但实际上是会快一点点的。
提交后通过,看测试结果,耗时是:Runtime: 164 ms,看来优化还是有点效果的,快了一点点。
还能不能在快一点呢?题目中提到的 Sorted (已排序),很容易让人想到使用二分法来查找,不过这个排序不是真的排序,是被截断过的,所以要稍微做些变通。尝试加入二分查找的代码如下:
class Solution:
# @param num, a list of integer
# @return an integer
def findMin(self, num):
i = 0
j = len(num)-1
while(i < j-1):
point = int((i + j)/2)
if(num[point] > num[i]):
i = point
if(num[point] < num[j]):
j = point
return min(num[0], num[i], num[j])
使用二分法不断逼近最小的数字,此时代码的时间复杂度为 O(log2n) ,提交后通过,看测试结果,耗时是:Runtime: 140 ms。效果还行。
这个结果应该是我能做到的最优结果了。
简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。的更多相关文章
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- leetcode 【 Find Minimum in Rotated Sorted Array 】python 实现
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- [leetcode]Find Minimum in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 解题思路:这道题和上一道题的区别是,数组中 ...
- 【刷题-LeetCode】154 Find Minimum in Rotated Sorted Array II
Find Minimum in Rotated Sorted Array II Suppose an array sorted in ascending order is rotated at som ...
- 【刷题-LeetCode】153 Find Minimum in Rotated Sorted Array
Find Minimum in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some p ...
- [OJ] Find Minimum in Rotated Sorted Array
LintCode 159. Find Minimum in Rotated Sorted Array (Medium) LeetCode 153. Find Minimum in Rotated So ...
- 【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数
Add Date 2014-10-15 Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some p ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- Oracle物理的体系结构
体系结构图的学习: 老余服装店的故事 结构图: SQL查询语句 SGA 共享池shared pool 数据缓存区Buffer cache PGA 进程 SQL更新语句 SGA: 日志缓存区 日志文件 ...
- PropertyDrawer 自定义属性绘图
public class PlayerAttributeExample : MonoBehaviour { //无滑块的属性 ; //特性限定,有滑块 [Range(, )] ; } Range特性的 ...
- Wireshark 入门
1.过滤目的地是百度的IP包. 百度的ip: 命令:ip.src eq 61.135.169.125 过滤ip来源是61.135.169.125 ip.dst eq 61.135.169.125 过滤 ...
- VBS基础篇 - 队列
VBS中的队列需要使用System.Collections.Queue '建立队列 Dim Que : Set Que = CreateObject("System.Collections. ...
- android中的selector背景选择器的用法
关于listview和button都要改变android原来控件的背景,在网上查找了一些资料不是很全,所以现在总结一下android的selector的用法. 首先android的selector是在 ...
- [C#]Linq To Xml 介绍- 转
LINQ to XML 类概述 LINQ to XML 旨在使 XML 名称尽可能简单. XAttribute 类 XAttribute 表示一个 XML 属性. XCData 类 XCDat ...
- C#中类型分析中的常见问题 Type - 转
http://www.cnblogs.com/yuanyuan/archive/2012/08/16/2642281.html 写代码的时候经常需要分析已有类型的信息例如:分析现有类型自动生成类, 或 ...
- bug集合
解决方法:vertical-align:top; 垂直对齐方式:对浮动元素无效浮动类: ie 6 7要在一行显示多个div要 给每一个元素浮动 否则会出间隙. ie6双倍边距bug 1. bug条件 ...
- 3563: DZY Loves Chinese - BZOJ
Description神校XJ之学霸兮,Dzy皇考曰JC.摄提贞于孟陬兮,惟庚寅Dzy以降.纷Dzy既有此内美兮,又重之以修能.遂降临于OI界,欲以神力而凌♂辱众生. 今Dzy有一魞歄图,其上有N座祭 ...
- 【BZOJ】【2154】Crash的数字表格
莫比乌斯反演 PoPoQQQ讲义第4题 题解:http://www.cnblogs.com/jianglangcaijin/archive/2013/11/27/3446169.html 感觉两次sq ...