本文主要是介绍leetcode456_3-24每日题:132模式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
class Solution {
public:bool find132(vector<int>& nums,int start,int end){if(end-start+1<3) return false;int max = nums[start];int maxn = start;for(int i=start+1;i<=end;i++){maxn = nums[i]>=max?i:maxn;max = nums[i]>=max?nums[i]:max;}if(maxn == start){return find132(nums,start+1,end);} else if(maxn == end){return find132(nums,start,end-1);} else{int a = findmin(nums,start,maxn-1);int b = findmax(nums,maxn+1,end);if(a<b) return true;else return find132(nums,start,maxn-1)||find132(nums,maxn+1,end);}}int findmin(vector<int> nums,int start,int end){int min=nums[start];for(int i=start;i<=end;i++){min = nums[i]<min?nums[i]:min;}return min;}int findmax(vector<int> nums,int start,int end){int max=nums[start];for(int i=start;i<=end;i++){max = nums[i]>max?nums[i]:max;}return max;}bool find132pattern(vector<int>& nums) {return find132(nums,0,nums.size()-1);}
};
分治法。
这篇关于leetcode456_3-24每日题:132模式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!