[LeetCode][Hard] 154. Find Minimum in Rotated Sorted Array II
Leetcode 網址: https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii/
題目說明:
這題跟 153 一樣差別在於 153 保證不會有重複的元素,但 154 可能有重複的元素。
解題說明:
由於是否重複對於我們 153 的解法並無影響,所以直接套用 153 的程式即可解決此題。
程式碼
class Solution {
public:
int findMin(vector<int>& nums) {
for (int i = 0, maxNum = INT_MIN; i < nums.size(); i++) {
if (nums[i] >= maxNum) maxNum = nums[i];
else return nums[i];
}
return nums[0];
}
};
結果
Runtime: 4 ms, faster than 98.88% of C++ online submissions for Find Minimum in Rotated Sorted Array II.
Memory Usage: 12.2 MB, less than 96.69% of C++ online submissions for Find Minimum in Rotated Sorted Array II.
留言
張貼留言