题目:

Suppose an array sorted in ascending order 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.

思路:

1.题目中指出sorted array,find target使用二分法。

2.分为以下三种情况

(1)数组只有一个元素:nums[0]即为最小值。

(2)数组不是rotated sorted array,退化为sorted array:nums[0]即为最小值。

(3)数组是rotated sorted array:nums[start] > nums[end]:

a:nums[mid]大于nums[start].

b:nums[mid]小于nums[start].

代码:

 public class Solution {
public int findMin(int[] nums) {
int start = 0,end = nums.length-1,mid = 0;
//length equals 1;
if(nums.length == 1){
return nums[0];
}
//is rotated sorted array;
while(nums[start] > nums[end] && start + 1 < end){
mid = start + (end - start)/2;
if(nums[mid] > nums[start]){
start = mid;
}else if(nums[mid] < nums[start]){
end = mid;
}
}
//is not rotated sorted array;
if(nums[start] < nums[end]){
return nums[start];
}
return nums[end];
}
}

LeetCode 153.Find Minimum in Rotated Sorted Array(M)(P)的更多相关文章

  1. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  2. [LeetCode] 153. Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  3. Java for LeetCode 153 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 ...

  4. leetcode 153. 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 ...

  5. leetcode 153. Find Minimum in Rotated Sorted Array --------- java

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  6. LeetCode 153. Find Minimum in Rotated Sorted Array (在旋转有序数组中找到最小值)

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  7. Leetcode 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种

    Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...

  8. LeetCode 153. Find Minimum in Rotated Sorted Array寻找旋转排序数组中的最小值 (C++)

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

  9. [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 ...

随机推荐

  1. 吴裕雄--天生自然C语言开发:递归

    void recursion() { statements; ... ... ... recursion(); /* 函数调用自身 */ ... ... ... } int main() { recu ...

  2. mui弹出输入法遮住input表单元素

    转自https://www.cnblogs.com/devilyouwei/p/6293190.html mui弹出输入法遮住input表单元素   问题如下:当我用mui开发app时,在mui-sc ...

  3. dedecms_5.7 download.php SQL注入

    最近在看Web渗透与漏洞挖掘,这本书的编写目的感觉非常的不错,把渗透和代码审计结合在一起,但是代码审计部分感觉思路个人认为并不是很清晰,在学习dedecms v5.7 SQL注入的时候就只看懂了漏洞, ...

  4. Mysql 和 Java对比异同

    1.求两个时间的差(天数) mysql : to_days 距离公元0年的天数 select TO_DAYS('2017-10-18 00:00:00'),TO_DAYS(NOW()), (TO_DA ...

  5. VBA自动点击IE的浏览按钮、自动选择路径、自动关闭打开文件对话框

    VBA调用InternetExplorer操作IE浏览器,自动弹出文件选择对话框时,VBA会处于阻塞状态,你必须手工关闭文件选择对话框,VBA才能继续向后运行. 例如下面网址,就有一个文件浏览按钮: ...

  6. 获取指定网卡对应的IP地址

    #include <stdio.h> #include <string.h> #include <sys/socket.h> #include <sys/ty ...

  7. python常用包

    今日所得 collections模块 时间模块 random模块 os模块 sys模块 序列化模块 subprocess模块 collections模块 namedtuple:具名元组 #定义方式一: ...

  8. 吴裕雄--天生自然操作系统操作笔记:window10显示隐藏文件夹

    基于安全考虑,操作系统会隐藏一些文件和文件夹,防止误删除操作.但有可能是个别人为了隐藏一些私密数据,也同样采取隐藏的方式.

  9. JAVA数据结构——单链表

    链表:一. 顺序存储结构虽然是一种很有用的存储结构,但是他有如下几点局限性:1. 因为创造线性表的时候已经固定了空间,所以当需要扩充空间时,就需要重新创建一个地址连续的更大的存储空间.并把原有的数据元 ...

  10. java生成饼图

    pom.xml文件导入依赖包 <!-- https://mvnrepository.com/artifact/org.jfree/jfreechart --> <dependency ...