码迷,mamicode.com
首页 > 编程语言 > 详细

寻找旋转排序数组中的最小值

时间:2017-05-02 13:58:00      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:binary   public   ini   ext   dmi   exist   tar   lint   turn   

寻找旋转排序数组中的最小值 

假设一个旋转排序的数组其起始位置是未知的(比如0 1 2 4 5 6 7 可能变成是4 5 6 7 0 1 2)。

你需要找到其中最小的元素。

你可以假设数组中不存在重复的元素。

 注意事项

You may assume no duplicate exists in the array.

样例

给出[4,5,6,7,0,1,2]  返回 0

标签 
 
 1 class Solution {
 2 public:
 3     /**
 4      * @param nums: a rotated sorted array
 5      * @return: the minimum number in the array
 6      */
 7     int findMin(vector<int> &nums) {
 8         // write your code here
 9         int size = nums.size();
10         if(size == 0)
11             return 0;
12 
13         int min = nums[0];
14         for(int i=1; i<size; i++) {
15             if(min > nums[i])
16                 min = nums[i];
17         }
18         return min;
19     }
20 };

 

寻找旋转排序数组中的最小值

标签:binary   public   ini   ext   dmi   exist   tar   lint   turn   

原文地址:http://www.cnblogs.com/libaoquan/p/6795520.html

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