简单的算法题, 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 ...
随机推荐
- Java之this详解
1. this是指当前对象自己. 用类名定义一个变量的时候,定义的应该只是一个引用,外面可以通过这个引用来访问这个类里面的属性和方法,那们类里面是够也应该有一个引用来访问自己的属性和方法纳?呵呵,JA ...
- boost-内存管理(scoped_array)
# include <algorithm> string *p=new string[20]; scoped_array<string> sp(p); fill_ ...
- crawler spec
使用说明 0.写在前面 1.本程序完成的抓取网页并保存其文件的工作. 2.目前的版本还需将工程文件导入eclipse中运行. 3.加载主类MyCrawler生成可执行文件. 4.程序主界面: 1 准备 ...
- Careercup - Facebook面试题 - 5890898499993600
2014-05-01 02:30 题目链接 原题: Given a matrix of letters and a word, check if the word is present in the ...
- android 弹出框(输入框和选择框)
1.输入框: final EditText inputServer = new EditText(this); inputServer.setFilters(new InputFilter[]{new ...
- Sublime text 取消记住上一次打开的,这功能太墨迹了!
比较恨,这sublime text的配置全部都是配置文件. 选择菜单:Preferences->Settings-User,增加配置项 //热退出,其实实现一种模拟没有退出的状态,当程序再次启动 ...
- IOS 后台运行
默认情况下,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作.但是应用可以调用UIApplication的beginBackgroundTaskWithExpirat ...
- HDU 4811 Ball 贪心
题目链接: 题目 Ball Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 问题描述 ...
- Extjs关于FormPanel布局
Extjs关于FormPanel布局 FormPanel有两种布局:form和column,form是纵向布局,column为横向布局.默认为后者.使用layout属性定义布局类型.对于一个复杂的布局 ...
- ZOJ Monthly, August 2014
A Abs Problem http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5330 找规律题,构造出解.copyright@ts ...