Find Minimum in Rotated Sorted Array leetcode java
题目:
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.
解题思路:
首先假设一个sorted没有rotated的数组[1,2,3],假设我们通过一个pivot把这个数组rotate,那么结果可能为:[2,3,1], [3,1,2], 可以发现:num[low]永远大于(或等于)num[high]。因为你之前是sorted的数组,你在一个sorted的数组找了一个pivot进行rotate,那么比如pivot后面的值都大于pivot之前的值。所以依据这个发现,以及二分法查找。我们可以根据以下判断来解题。num[mid]有两种可能性,如果num[mid] > num[high],证明num[mid]在rotated后的那个区间内,这个区间我们刚才已知都大于pivot之前的值,所以最小值就在low=mid+1那个区间内。另一种可能,num[mid] <= num[high],那么我们刚才可以看出来这种可能性说明mid~high以及是排好序的,那么最小值在high=mid这个区间内(mid可能是最小值)。依据此判断可以找到最小值。
代码如下:
1 public int findMin(int[] num) {
2 int low = 0, high = num.length - 1;
3 while (low < high && num[low] >= num[high]) {
4 int mid = (low + high) / 2;
5 if (num[mid] > num[high])
6 low = mid + 1;
7 else
8 high = mid;
9 }
return num[low];
}
Find Minimum in Rotated Sorted Array leetcode java的更多相关文章
- 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现
一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...
- leetcode 154. Find Minimum in Rotated Sorted Array II --------- java
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 153. Find Minimum in Rotated Sorted Array - LeetCode
Question 153. Find Minimum in Rotated Sorted Array Solution 题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成 ...
- Find Minimum in Rotated Sorted Array leetcode
原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...
- Search in Rotated Sorted Array leetcode java
题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- Find Minimum in Rotated Sorted Array——LeetCode
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 153. Find Minimum in Rotated Sorted Array(leetcode, binary search)
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/ leetcode 的题目,binary ...
- Java for LeetCode 154 Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode Find Minimum in Rotated Sorted Array II
原题链接在这里:https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目: Follow up for &qu ...
随机推荐
- [MongoDB]Mongodb攻略
-------------------------------------------------------------------------------------------- [基础] 1. ...
- ORA-01653:表空间扩展失败的问题以及增加表空间
一.脚本修改方式: ----查询表空间使用情况---使用DBA权限登陆SELECT UPPER(F.TABLESPACE_NAME) "表空间名",D.TOT_GROOTTE_MB ...
- LeetCode OJ-- String to Integer (atoi) **
https://oj.leetcode.com/problems/string-to-integer-atoi/ 细节题,把一个字符串转换成整数 class Solution { public: in ...
- MongoDB介绍与windows下安装
MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的.他支持的数据结构非常松散,是类 似json的bjson格式,因此可以存储比较复杂的数据类型. ...
- Oracle中改变表的Owner和tablespace
初用Oracle,很多的不熟悉,建完库,没有建用户,也没创建表空间,就直接system用户建表添加数据,几个月过去,表建了近百个,数据添加了几万条,才越来越觉得这种方式缺点太多: 在PL/SQL中系统 ...
- java中的内部类小结
内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类.如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动).显然, ...
- nodejs:使用多处理器
nodejs是单线程,这意味着Node只能利用一个处理器来工作.但多数服务器都有多个核.好在nodejs提供了cluster模块,可以把任务分配给子进程.每个子进程有些特殊能力,比如能与其他子进程共享 ...
- Find Minimum in Rotated Sorted Array leetcode
原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...
- [Leetcode][JAVA] Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from ...
- Linux sort 多字段分组排序
常用参数: -t: 指定分隔符 -k: 指定域 -u: 去除重复行 -n: 以数值排序 -r: 降序排序 (sort默认的排序方式是升序) -o: 结果重定向输出到文件 1.源文件: # cat hh ...