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.


题解:就是找数组中第一个“下凹”的地方,可以遍历,不过也可以用二分法,速度更快。

特别注意边界的处理:如果当前的中间值mid在0这个位置,只要考虑它后面的元素是不是小于它;如果在数组最后的位置上,要考虑它前面的元素是不是大于它。最后当发现当前mid所指的值不是所求的“下凹点”的时候,要把它和最后数组的最后一位比较,如果它比数组的最后一位大,说明要找的“下凹点”在mid所指位置的后面,否则在mid所指位置的前面。

JAVA版本代码如下:

 import java.awt.datatransfer.StringSelection;

 public class Solution {
public static void main(String[] args){
int num[] = {3,4,5,1,2};
Solution s = new Solution();
System.out.println(s.findMin(num));
}
public int findMin(int[] num) {
int begin = 0;
int end = num.length-1;
while(begin <= end){
int mid = (begin + end)/2;
if(mid == 0){
if(mid+1 < num.length && num[mid+1] < num[mid])
return num[mid+1];
else {
return num[0];
}
}
if(mid == num.length-1){
if(mid-1 >=0 && num[mid-1] > num[mid])
return num[mid];
else {
return num[0];
}
}
if(num[mid-1] > num[mid] && num[mid+1] > num[mid])
return num[mid]; if(num[num.length-1] < num[mid])
begin = mid+1;
else {
end = mid-1;
} }
return num[0];
}
}

【leetcode刷题笔记】Find Minimum in Rotated Sorted Array的更多相关文章

  1. 【leetcode刷题笔记】Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  2. 【leetcode刷题笔记】Search 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 ...

  3. 【leetcode刷题笔记】Remove Duplicates from Sorted Array II

    Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...

  4. 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

    简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...

  5. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  6. LeetCode(153) Find Minimum in Rotated Sorted Array

    题目 Total Accepted: 65121 Total Submissions: 190974 Difficulty: Medium Suppose a sorted array is rota ...

  7. 【leetcode刷题笔记】Minimum Window Substring

    Given a string S and a string T, find the minimum window in S which will contain all the characters ...

  8. 【leetcode刷题笔记】Minimum Depth of Binary Tree

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  9. 【leetcode刷题笔记】Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  10. (python)leetcode刷题笔记04 Median of Two Sorted Arrays

    4. Median of Two Sorted Arrays There are two sorted arrays nums1 and nums2 of size m and n respectiv ...

随机推荐

  1. 【BZOJ】2060: [Usaco2010 Nov]Visiting Cows 拜访奶牛(树形dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2060 裸的树形dp d[x][1]表示访问x的数量,d[x][0]表示不访问x的数量 d[x][1] ...

  2. Android 消息处理源代码分析(2)

    Android 消息处理源代码分析(1)点击打开链接 继续接着分析剩下的类文件 Looper.java public final class Looper { final MessageQueue m ...

  3. 【Python】求素数-稍加优化

    print 'Find prime number smaller then input number \n' print 'Please input a number:' import datetim ...

  4. Lumen rule

    之前写了了laravel表单验证的生命周期:https://www.cnblogs.com/cxscode/p/7561277.html 今天来总结一下lumen的Validator的一些使用心得 可 ...

  5. p​h​p​面​试​题​笔​试​题​ ​比较有用

    一.选择题1.php的源代码是 (A )A.开放的 B.封闭的 C.需购买的 D.完全不可见的2.php的输出语句是 ( C )A.out.print B.response.write C.echo ...

  6. Python创建数组

    1  创建数组 array函数 >>> a=([1,2],[3,4]) >>> array(a) array([[1, 2], [3, 4]]) arange函数: ...

  7. 爬虫实战【9】Selenium解析淘宝宝贝-获取宝贝信息并保存

    通过昨天的分析,我们已经能到依次打开多个页面了,接下来就是获取每个页面上宝贝的信息了. 分析页面宝贝信息 [插入图片,宝贝信息各项内容] 从图片上看,每个宝贝有如下信息:price,title,url ...

  8. Scrapy命令和备注

    Scrapy命令和备注 1.创建一个新项目(命令行) project是项目名 scrapy startproject <project_name> 2.调试项目(pycharm) 在pyc ...

  9. python之django直接执行sql语句

    python之django直接执行sql语句 sql = 'select * from stu' info = 模型类.objects.raw(sql)

  10. shell输出颜色

    #!/bin/bash # #下面是字体输出颜色及终端格式控制 #字体色范围:- echo -e "\033[30m 黑色字 \033[0m" echo -e "\033 ...