前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[LeetCode] 153. Find Minimum in Rotated Sorted Array

[LeetCode] 153. Find Minimum in Rotated Sorted Array

作者头像
用户1148830
发布2018-01-03 17:43:59
5480
发布2018-01-03 17:43:59
举报

【原题】 Suppose an array sorted in ascending order 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.

【解释】 给定一个旋转过的有序数组,要求返回该数组的最小值 【思路】 一个简单粗暴的方法就是O(n)查找,但是如果是这样的话,题目当中的有序就没有任何意义了。当在有序数组查找的时候,为了减少时间复杂度,可以很自然的想到二分查找,即使这里是部分有序。参见这里

代码语言:javascript
复制
public class Solution {
    public int findMin(int[] nums) {
      int left=0,right=nums.length-1;
        while(left<right){
            int mid=(left+right)/2;
            if(nums[mid]>nums[right])
                left=mid+1;//最小元素在右半部分
            else right=mid;//最小元素在左半部分,但mid也有可能是最小元素的index
        }
        return nums[left];
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年06月24日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档