Find Minimum in Rotated Sorted Array LeetCode solution in C++

1
class Solution {
2
public:
3
int findMin(vector<int> &nums) {
4
int result = -1;
5
int first = nums[0];
6

7
// check the array for rotated point when it is obtained break the loop and
8
// assign result as rotation point
9
for (int i = 1; i < nums.size(); i++) {
10
if (nums[i - 1] > nums[i]) {
11
result = nums[i];
12
break;
13
}
14
}
15
// if the array is not sorted return first element
16
if (result == -1) {
17
return first;
18
}
19
return result;
20
}
21
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0