4_Longest Substring Without Repeating Characters

//abcabcbb    abc    3
//bbbbb       b      1
//pwwkew      wke    3
#include
#include
#include

using namespace std;

int lengthOfLongestSubstring(string s)
{
    if(s.size()==0) return 0;
    
    //unodered_map map;
    vector map(256,-1);
    
    int maxlen=0,count=0;
    
    for(int i=0;i4
            //  ***xbcx, 3->3
            //  ***axcx, 3->2
            int loc=map[c];
            
            if(loc+count4
                count++;
            else if(loc+count==i) // case 3->3
                ;
            else if(loc+count>i)  // case 3->2
                count=i-loc;
        }
        
        map[c]=i;
        
        if(count>maxlen)
            maxlen=count;

    }
    return maxlen;
}

int main()
{
    string str1="abcabcbb";
    cout<

你可能感兴趣的:(算法,数据结构)