LeetCode-153. Find Minimum in Rotated Sorted Array&154. Find Minimum in Rotated Sorted Array II

网友投稿 933 2022-08-25

LeetCode-153. Find Minimum in Rotated Sorted Array&154. Find Minimum in Rotated Sorted Array II

LeetCode-153. Find Minimum in Rotated Sorted Array&154. Find Minimum in Rotated Sorted Array II

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e.,  ​​[0,1,2,4,5,6,7]​​​ might become  ​​[4,5,6,7,0,1,2]​​).

Find the minimum element.

You may assume no duplicate exists in the array.

Example 1:

Input: [3,4,5,1,2] Output: 1

Example 2:

Input: [4,5,6,7,0,1,2]Output: 0

题解:

154包含重复,同样代码

class Solution {public: int findMin(vector& nums) { if (nums.empty() == true) { return 0; } int n = nums.size(); int i = 1; while (i < n) { if (nums[i] < nums[0]) { return nums[i]; } i++; } return nums[0]; }};

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:HUST-特殊排序
下一篇:编程语言:变革创业思维的工具(创造新的编程语言)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~