[LeetCode]Longest Substring Without Repeating Characters

原创 2015年11月18日 19:25:07

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.

两个指针,采用hash记录出现字母的位置。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int>hash (300,-1);
        int i,j;
        i=0;
        j=0;
        if(s.size()==0)
            return 0;
        if(s.size()==1)
            return 1;
        int ret=0;
        while(i<=j && j<s.length()){
            if(hash[s[j]]!=-1){
                for(;i<hash[s[j]];++i){
                    hash[s[i]] = -1;
                }
                i = hash[s[j]]+1;
                hash[s[j]]=j;
            }
            else{
                hash[s[j]]=j;
            }
            ret = max(ret,j-i+1);
            ++j;
        }
        return ret;
    }
    
};


版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

【LeetCode】Longest Substring Without Repeating Characters

问题描述Given a string, find the length of the longest substring without repeating characters.Examples:G...

[LeetCode - 滑动窗口] 3. Longest Substring Without Repeating Characters

1 问题 Given a string, find the length of the longest substring without repeating characters. Exa...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

Longest Substring Without Repeating Characters-----LeetCode

题目Given a string, find the length of the longest substring without repeating characters.Examples:Giv...

leetcode003 Longest Substring Without Repeating Characters

题目3. Longest Substring Without Repeating Characters Given a string, find the length of the longest ...

leetcode: Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

leetcode4 ---Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters. For example, ...

LeetCode | 3. Longest Substring Without Repeating Characters

leetcode.com 3. Longest Substring Without Repeating Characters

(leetcode) Longest Substring Without Repeating Characters

Question : Given a string, find the length of the longest substring without repeating characters. ...

LeetCode- 3. Longest Substring Without Repeating Characters

Description Given a string, find the length of the longest substring without repeating characters.E...

leetcode_3 - Longest Substring Without Repeating Characters

leetcode_3 : Longest Substring Without Repeating Characters标签(空格分隔): leetcode string twoPoints setGi...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)