https://leetcode.com/problems/super-palindromes/description/
题目大意:找数组中 最大连续的 只有两个不同数字的序列。
解题思路:暴力查找,稍微剪枝了一下,如果已经判断到了末尾就结束,水过去了。标准解法应该采用滑动窗口
class Solution {
public:
int totalFruit(vector<int>& tree) {
int n = tree.size();
int res = 0;
int cnt =0;
int tmp=0;
for(int i=0;i<n;i++)
{
int a = tree[i];
int b=-1,flag=0;
for(int j=i+1;j<n;j++)
{
if(tree[j]!=a && b==-1) b=tree[j];
else if(a!=tree[j] && b!=tree[j] && b!=-1) { tmp = j-i; flag=1; break; }
}
if(flag==0) { tmp = n-i; res = max(res,tmp); break; }
//cout<<tmp<<" "<<a<<" "<<b<<" "<<flag<<endl;
res=max(res,tmp);
}
return res;
}
};