My c++ solution


  • 0
    Y
    int lengthOfLongestSubstring(string s) {
    		string sub_s = "";
    		int length = 0;
    		for(auto i : s)
    		{
    			auto pos = sub_s.find(i);
    			if(pos == -1)
    				sub_s.append(1,i);
    			else
    			{
    				length = (length > sub_s.length()? length : sub_s.length());
    				sub_s.erase(0, pos + 1);
    				sub_s.append(1,i);
    			}
    		}
    		length = (length > sub_s.length()? length : sub_s.length());
    		return length;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.