leetcode-Longest Substring Without Repeating Characters

来源:互联网 发布:优惠券系统源码 编辑:程序博客网 时间:2024/05/05 01:00
Difficulty:Medium

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

class Solution {public:    int lengthOfLongestSubstring(string s) {        if(s.empty())            return 0;        map<char,int> check;        int pre=0;        int length=s.length();        int maxLen=INT_MIN;        for(int i=0;i<length;++i){            if(check.find(s[i])!=check.end()&&pre<=check[s[i]])                pre=check[s[i]]+1;            maxLen=max(maxLen,i-pre+1);            check[s[i]]=i;        }        return maxLen;    }};


0 0