无重复字符的最长子串

 题解:暴力解题

将每个元素作为最长字串的开始,然后利用set的特性进行插入,如果插入失败,保存当前长度,继续下一个元素作为最长字串重复操作

class Solution { 
public:
    int lengthOfLongestSubstring(string a) {
    set<char> s;
        int ans=0;
    for(int i=0;i<a.size();i++){
        for(int j=i;j<a.size();j++){
            if(!s.insert(a[j]).second){//判断是否插入成功
                break;
            }
        }
        if(s.size()>ans){
            ans=s.size();
        }s.clear();
        
        
    }    

    return ans;
    }
};

关于set的insert的更多信息:https://en.cppreference.com/w/cpp/container/set/insert

不一样的烟火
原文地址:https://www.cnblogs.com/cstdio1/p/11617535.html