『典型的滑动窗口题目』题解 | #最长无重复子数组#
最长无重复子数组
http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4
- 典型的“滑动窗口机制”题目
滑动窗口机制模板题
class Solution { public: /** * * @param arr int整型vector the array * @return int整型 */ int maxLength(vector<int>& arr) { // write code here int res=0; int Len=arr.size(); //典型的滑动窗口题目 int Left=0,Right=0; map<int,int> mp; while( Right<Len ) { if( 0==mp[ arr[Right] ] ) { mp[ arr[Right] ]++; ++Right; } else { res=max( res, Right-Left ); while( mp[ arr[Right] ]>0 ) { mp[ arr[Left] ]--; ++Left; } mp[ arr[Right] ]++; ++Right; } } //记得“擦屁股”,因为可能2,3,4,5这样,就每次都没有重复,但是while中没有判断 res=max( res, Right-Left ); return res; } };