Find Minimum in Rotated Sorted Array
2015-07-31 22:42
369 查看
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e.,
Find the minimum element.
You may assume no duplicate exists in the array.
The minimum element must satisfy one of two conditions: 1) If rotate, A[min] < A[min - 1]; 2) If not, A[0]. Therefore, we can use binary
search: check the middle element, if it is less than previous one, then it is minimum. If not, there are 2 conditions as well: If it is greater than both left and right element, then minimum element should be on its right, otherwise on its left.
jiuzhang:
只看mid和end的值大小,mid的值如果比end的大,则,左边升序,转折点在mid右边:[4,5,6,7,8,9,10,11,1,2,3]
如果mid的值小于end,则左边升序,转折点在左边:[7,1,2,3,4,5,6]
find minimum in rotated sorted array II有重复元素:
https://leetcode.com/discuss/19746/my-pretty-simple-code-to-solve-it
(i.e.,
0 1 2 4 5 6 7might become
4 5 6 7 0 1 2).
Find the minimum element.
You may assume no duplicate exists in the array.
public class Solution { public int findMin(int[] num) { if (num == null || num.length == 0) { return 0; } if (num.length == 1) { return num[0]; } int start = 0, end = num.length - 1; while (start < end) { int mid = start + (end- start) / 2; if (mid > 0 && num[mid] < num[mid - 1]) { return num[mid]; } //mid的值比start和end的值都大,所以转折点在右边 //e.g[4,5,6,7,8,9,10,11,1,2,3] if (num[start] <= num[mid] && num[mid] > num[end]) { start = mid+1; //转折点在左边 //e.g[8,1,2,3,4,5,6,7] } else { end = mid-1; } } return num[start]; } }
The minimum element must satisfy one of two conditions: 1) If rotate, A[min] < A[min - 1]; 2) If not, A[0]. Therefore, we can use binary
search: check the middle element, if it is less than previous one, then it is minimum. If not, there are 2 conditions as well: If it is greater than both left and right element, then minimum element should be on its right, otherwise on its left.
jiuzhang:
public class Solution { public int findMin(int[] num) { int start = 0, end = num.length - 1; while (start + 1 < end) { int mid = start + (end - start) / 2; if (num[mid] >= num[end]) { start = mid; } else { end = mid; } } if (num[start] < num[end]) { return num[start]; } else { return num[end]; } } }
只看mid和end的值大小,mid的值如果比end的大,则,左边升序,转折点在mid右边:[4,5,6,7,8,9,10,11,1,2,3]
如果mid的值小于end,则左边升序,转折点在左边:[7,1,2,3,4,5,6]
public class Solution { public int findMin(int[] num) { if (num == null || num.length == 0) { return 0; } if (num.length == 1) { return num[0]; } int start = 0, end = num.length - 1; while (start < end) { int mid = start + (end- start) / 2; if (num[mid]>num[end]) { start = mid+1; } else { end = mid; } } return num[start]; } }
find minimum in rotated sorted array II有重复元素:
https://leetcode.com/discuss/19746/my-pretty-simple-code-to-solve-it
相关文章推荐
- Visual Studio 2008 Package Load Failure:未能正确加载包“Microsoft.VisualStudio.Xaml”
- HDU 4313 Matrix(贪心+并查集)
- mysql_fetch_array容易掉入的陷进
- J哥---------Android 多线程下载 仿下载助手(改进版)
- 局部内部类对外部属性和变量的访问测试
- 黑马程序员——走进面向对象
- Redis数据类型之字典
- 直接用socket实现HTTP下载
- JavaScript--时间显示小插件
- Go语言练习:go语言与C语言的交互——cgo
- Android大图片裁剪终极解决方案(下:拍照截图)
- objectiveC【语法】修饰符 static extern const
- Nginx常用命令介绍
- Oracle 学习之RMAN(十六)Catalog
- 我的书单
- Android Service与Activity之间通信的几种方式(1)---通过broadcast(广播)的形式
- Nginx学习总结
- 把握linux内核设计思想(十):内核同步
- web——测试方法总结
- ╮(╯▽╰)╭ 数蚂蚁