欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > > 文章正文

LeetCode Longest Substring Without Repeating Characters,leetcoderepeating

来源: javaer 分享于  点击 25068 次 点评:117

LeetCode Longest Substring Without Repeating Characters,leetcoderepeating


Longest Substring Without Repeating Characters

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.

Solution:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        map<char,int> mymap;
        vector<int> pos;
        for(int i=0;i<s.size();++i){
            pos.push_back(mymap[s[i]]);
            mymap[s[i]]=i+1;
        }
        int result=0,curr=0;
        for(int j=0;j<s.size();++j){
            curr=max(curr,pos[j]);
            result=max(result,j-curr+1);            
        }
        return result;
    }
};


相关文章

    暂无相关文章

用户点评