码迷,mamicode.com
首页 > 其他好文 > 详细

153. Find Minimum in Rotated Sorted Array - LeetCode

时间:2018-07-07 20:47:39      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:nim   esc   分享   元素   array   []   一个   .com   ota   

Question

153.?Find Minimum in Rotated Sorted Array

技术分享图片

Solution

题目大意:给一个按增序排列的数组,其中有一段错位了[1,2,3,4,5,6]变成[4,5,6,1,2,3],把1求出来

思路:遍历,如果当前元素比前一个元素小就是这个元素了

Java实现:

public int findMin(int[] nums) {
    int ans = nums[0];
    for (int i=0; i<nums.length; i++) {
        int pre = i==0?nums[0]:nums[i-1];
        if (nums[i] < pre) {
            ans = nums[i];
            break;
        }
    }
    return ans;
}

153. Find Minimum in Rotated Sorted Array - LeetCode

标签:nim   esc   分享   元素   array   []   一个   .com   ota   

原文地址:https://www.cnblogs.com/okokabcd/p/9277969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!