首页 > 代码库 > LeetCode Find Minimum In Rotated Sorted Array
LeetCode Find Minimum In Rotated Sorted Array
#include <iostream>#include <cstdlib>#include <vector>using namespace std;// 4 5 1 2// 1 2class Solution {public: int findMin(vector<int> &num) { int len = num.size(); if (len < 1) { // should not happened; return 0; } if (len == 1) { return num[0]; } int p = 0; int q = len - 1; // array with no rotation if (num[p] < num[q]) { return num[p]; } // array been rotated while (p + 1 < q) { int m = (p + q) / 2; if (num[m] > num[q]) { p = m; } else if (num[m] < num[q]){ q = m; } } return num[q]; }};int main() { //int arr[] = {4, 5, 6, 7, 0, 1, 2}; //int arr[] = {1, 2, 3}; int arr[] = {1, 2}; //int arr[] = {2, 1}; vector<int> array(arr, arr + sizeof(arr) / sizeof(int)); Solution s; cout<<s.findMin(array)<<endl; return 0;}
好像已经用这题,重复添加了
LeetCode Find Minimum In Rotated Sorted Array
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。