lintcode,寻找旋转排序数组中的最小值
来源:互联网 发布:移动宽带网络客服电话 编辑:程序博客网 时间:2024/04/27 05:55
假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。
你需要找到其中最小的元素。
你可以假设数组中不存在重复的元素。
样例
给出[4,5,6,7,0,1,2] 返回 0
一刷ac
解题思路:把中点位置拿出来和两端比较,但是实际上为了找到最小值只需要让中点和最右点比较即可。
public class Solution { /** * @param nums: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] nums) { if(nums == null || nums.length == 0) return 0; int left = 0; int right = nums.length-1; int res = nums[left]; while(left + 1 < right){ int mid = (left + right) / 2; if(nums[mid] > nums[left]){ if(nums[left] > nums[right]) left = mid+1; else right = mid-1; }else{ right = mid; } } if(nums[left] < nums[right]) return nums[left]; return nums[right]; }}
public class Solution { /** * @param num: a rotated sorted array * @return: the minimum number in the array */ public int findMin(int[] num) { if(num == null || num.length == 0) return 0; int left = 0; int right = num.length - 1; while(left < right){ int mid = (left + right) / 2; if(num[mid] > num[right]){ left = mid + 1; }else{ right = mid; } } return num[left]; }}
0 0
- LintCode-寻找旋转排序数组中的最小值
- LintCode 寻找旋转排序数组中的最小值
- LintCode-寻找旋转排序数组中的最小值
- lintcode 寻找旋转排序数组中的最小值
- lintcode,寻找旋转排序数组中的最小值
- lintcode,寻找旋转排序数组中的最小值
- LintCode : 寻找旋转排序数组中的最小值
- lintcode--寻找旋转排序数组中的最小值
- 寻找旋转排序数组中的最小值-LintCode
- LintCode-寻找旋转排序数组中的最小值 II
- lintcode- 寻找旋转排序数组中的最小值-159
- LintCode-剑指Offer-(159)寻找旋转排序数组中的最小值
- LintCode-剑指Offer-(28)寻找旋转排序数组中的最小值
- 寻找旋转排序数组中的最小值——LintCode
- lintcode 160 寻找旋转排序数组中的最小值 II
- Lintcode 寻找旋转排序数组中的最小值 II
- [LintCode]159.寻找旋转排序数组中的最小值
- lintcode --寻找旋转排序数组中的最小值 II
- 如何编写一个简单群聊
- 单例设计模式
- electron+react+redux+material-ui桌面应用小结
- 删除文件夹
- Faster RCNN代码理解(Python)
- lintcode,寻找旋转排序数组中的最小值
- 程序的内存分配
- LCA(st算法)
- 《Effective Java》第3章 对于所有对象都通用的方法
- jQuery全选和范选,按钮样式变化及取选中项的值
- 初学Python-threadpool多线程编程
- ionic安卓真机debug调试方法,定位js错误
- PE 108 Diophantine reciprocals I (数论:分式个数)
- POJO 类中的任何boolean布尔类型的变量,都不要加 is,否则部分框架解析会引起序列化错误