lintcode,寻找旋转排序数组中的最小值
来源:互联网 发布:数控编程g71 编辑:程序博客网 时间:2024/04/27 09:51
假设一个旋转排序的数组其起始位置是未知的(比如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 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 low = 0; int high = num.length - 1; int res = num[low]; while(low + 1 < high){ if(num[low] < num[high]) break; if(num[(low+high)/2] > num[low]){ low = (low+high)/2 + 1; }else{ high = (low+high)/2; } } res = Math.min(num[low], num[high]); return res; }}
优化一下
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
- css3之canvas 基础
- VC++常见错误原因解析--error LNK2019: 无法解析的外部符号 "public: void __thiscall
- windows问题集合
- java 方法(method)
- C语言知识碎片
- lintcode,寻找旋转排序数组中的最小值
- 欧几里得算法,扩展欧几里得算法及裴蜀定理证明
- centos 7没有创建启动加载器,没有创建可引导分区
- Android屏幕适配
- Android UI设计之布局-详细解析布局实现(线性布局)
- 对目录的操作指令
- 2016/12/14 java.util.concurrent.ExecutionException:com.android.ide.common.process.ProcessExc
- 【django】django render()和render_to_response()和direct_to_template()和locals()
- Ubuntu各种命令使用 笔记