题解 | #找到字符串的最长无重复字符子串#

找到字符串的最长无重复字符子串

http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4

更简洁的写法
class Solution {
public:

int maxLength(vector<int>& arr) {

    //哈希
    unordered_map<int,int> mp;
    int maxLength =0;

    for(int i=0; i<arr.size(); ){
        if(mp.find(arr[i]) == mp.end()){
            mp.insert(make_pair(arr[i],i));
            maxLength = max(maxLength, int(mp.size())); 
            i++; //注意
        }
        else{
            i = mp[arr[i]]+1;
            mp.clear();
        }  

    }
    return maxLength;
}

};

全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务