Hard??没搞错吧,LeetCode水题一道:Find Minimum in Rotated Sorted Array II
2015-03-03 10:48
375 查看
做完Find Minimum in Rotated Sorted Array I觉得很简单,再做2发现也不难,但是LeetCode竟然给了一个hard评级,难道是我编程水平大有长进?哈哈我估计是对复杂度有很高的要求的情况下比较难吧。。
Python(偷懒)做法就是一句话:return min(num)
Java(基本做法)如下:
public class Solution {
public int findMin(int[] num) {
int len=num.length;
if (len==0) return 0;
else if (len==1) return num[0];
else{
for (int i=0;i<len-1;i++){
if (num[i]>num[i+1]) return num[i+1];
}
}
return num[0];
}
}平均、最差时间复杂度都是O(n)。思路很简单,就是顺序依次比较,如果后者小于前者,则后者一定是最小的,输出即可。如果不是则继续比较。
牛逼做法:请出门左拐,这里没有。
Python(偷懒)做法就是一句话:return min(num)
Java(基本做法)如下:
public class Solution {
public int findMin(int[] num) {
int len=num.length;
if (len==0) return 0;
else if (len==1) return num[0];
else{
for (int i=0;i<len-1;i++){
if (num[i]>num[i+1]) return num[i+1];
}
}
return num[0];
}
}平均、最差时间复杂度都是O(n)。思路很简单,就是顺序依次比较,如果后者小于前者,则后者一定是最小的,输出即可。如果不是则继续比较。
牛逼做法:请出门左拐,这里没有。
相关文章推荐
- [Leetcode 154, Hard] Find Minimum in Rotated Sorted Array II
- 154.leetcode Find Minimum in Rotated Sorted Array II(hard)[排序数组 重复元素 二分查找]
- 【LeetCode】【Solution】Find Minimum in Rotated Sorted Array I & II
- Find Minimum in Rotated Sorted Array II c++ leetcode
- LeetCode 154 Find Minimum in Rotated Sorted Array II
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
- leetcode--Find Minimum in Rotated Sorted Array II
- Leetcode_find-minimum-in-rotated-sorted-array-ii(c++ version)
- Leetcode-Find Minimum in Rotated Sorted Array II
- Leetcode: Find Minimum in Rotated Sorted Array II
- leetcode Find Minimum in Rotated Sorted Array II
- [Leetcode]Find Minimum in Rotated Sorted Array I & II
- leetcode:Find Minimum in Rotated Sorted Array II
- [leetcode] Find Minimum in Rotated Sorted Array II
- [LeetCode] Find Minimum in Rotated Sorted Array II
- Find Minimum in Rotated Sorted Array II -- LeetCode
- Leetcode: Find Minimum in Rotated Sorted Array II
- leetcode find-minimum-in-rotated-sorted-array-ii
- leetcode:Find Minimum in Rotated Sorted Array II
- 【Leetcode】Find Minimum in Rotated Sorted Array II