https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/

leetcode 的题目,binary search 的变形。去在rotated array里找最小值。

Idea:分析binary search 三种cases:

1. nums[mid] > nums[r]: remove the left part: l = mid+1          e.g. 2 3 4 5 1 2

2. nums[mid] < nums[l] && nums[mid] < nums[r]: h = mid       e.g. 5 6 1 2 3 4

3. nums[mid] <nums[r] && nums[mid] > nums[l]: h = mid                e.g. 1 2 3 4 5 6

Combing the 2nd and 3rd case.

二分查找有点难, left 和 right的分析, 在合适 的时候break while loop.

 public int findMin(int[] nums) {
if(nums.length==0) return 0;
int l = 0, r = nums.length-1; // [)
while(l < r){
int mid = l+(r-l)/2;
if(nums[mid]>nums[r]){
l = mid+1;//remove
}else
r = mid;
}
return nums[l];
}

二分查找还要继续总结一个模板。

to be continued。。。。。

153. Find Minimum in Rotated Sorted Array(leetcode, binary search)的更多相关文章

  1. 153. Find Minimum in Rotated Sorted Array - LeetCode

    Question 153. Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成 ...

  2. [LeetCode] 153. Find Minimum in Rotated Sorted Array_Medium tag: Binary Search

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

  3. 154. Find Minimum in Rotated Sorted Array II(Binary search)

    https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/description/ -- leetcode follo ...

  4. 【LeetCode】153. Find Minimum in Rotated Sorted Array 解题报告(Python)

    [LeetCode]153. Find Minimum in Rotated Sorted Array 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode. ...

  5. 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 ...

  6. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  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 (3 solutions)

    Find Minimum in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you ...

  9. 【刷题-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 ...

随机推荐

  1. lintcode - 恢复ip地址

    class Solution { public: /* * @param s: the IP string * @return: All possible valid IP addresses */ ...

  2. CentOS快速搭建FTP(初级-四步)

    部署FTP,如果之前没有搭建过,刚开始找资料的时候网上各种各样的复杂参数配置,看的头晕,这里就把最核心的部分展示出来. 1.安装 vsftpd yum install -y vsftpd 2.如果是默 ...

  3. 找到一篇关于 Oracle 全文检索实践 的文章

    http://www.iteye.com/topic/1118055 有详细的例子记录了Oracle 全文检索的使用.

  4. 杭电ACM hdu 1398 Square Coins

    Problem Description People in Silverland use square coins. Not only they have square shapes but also ...

  5. InterruptionInJava

    package com.test; public class InterruptionInJava implements Runnable{ public static void main(Strin ...

  6. window.open()总结

    今天找父页面和子页面调去问题. 父页面js 1.打开子页面 可以设置子页面长宽高和位置 win= window.open(url, "", "top=45,left=20 ...

  7. hive参数设置

    -- 设置hive的计算引擎为spark set hive.execution.engine=spark; -- 修复分区 set hive.msck.path.validation=ignore; ...

  8. spring AOP正则表达式的几个问题

    基于包名的正则表达式,是根据抽象父类的包名过滤,还是实现类的包名过滤, 还是抽象父类实现的接口的包名过滤? org.springframework.aop.aspectj.AspectJExpress ...

  9. Vue在单独引入js文件中使用ElementUI的组件

    Vue在单独引入js文件中使用ElementUI的组件 问题场景: 我想在vue中的js文件中使用elementUI中的组件,因为我在main.js中引入了element包和它的css,并挂载到了全局 ...

  10. Jmeter录制pc脚本

    1.打开jmeter后可以看到左边窗口有个“测试计划”和“工作台”,右键“测试计划”,添加 Threads(Users) →线程组,再右键 线程组→添加 配置元件→Http请求默认值 Http请求默认 ...