赞
踩
哈希表记录水果种类 超过两个就把之前的去了
class Solution { public: int totalFruit(vector<int>& fruits) { int res=0,n = fruits.size(); unordered_map<int,int> cnt; for(int i=0,j=0;i<n;i++) { cnt[fruits[i]] ++; while(cnt.size() > 2) { if(-- cnt[fruits[j]] == 0) cnt.erase(fruits[j]); j ++; } res = max(res,i-j+1); } return res; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。