-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path153.cpp
More file actions
52 lines (48 loc) · 1.36 KB
/
153.cpp
File metadata and controls
52 lines (48 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// 153.cpp
// LeetCode
//
// Created by 张佐玮 on 15/6/17.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Find Minimum in Rotated Sorted Array
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int findMin(vector<int>& nums) {
int start = 0, end = (int) nums.size(), minimum = nums[0];
while (start < end) {
int middle = start + (end - start) / 2;
if (nums[start] < nums[middle]) {
minimum = min(minimum, nums[start]);
start = middle + 1;
}
else {
minimum = min(minimum, nums[middle]);
end = middle;
}
}
return minimum;
}
int findMin_compare3(vector<int>& nums) {
int start = 0, end = (int) nums.size() - 1;
while (start <= end) {
int middle = start + (end - start) / 2;
int smaller = (nums[start] > nums[middle]) ? nums[middle] : nums[start];
int smallest = (nums[end] > smaller) ? smaller : nums[end];
if (nums[start] == smallest) {
return smallest;
}
else if (nums[end] == smallest) {
start = middle + 1;
}
else {
end = middle;
}
}
return INT_MIN;
}
};