[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-面试算法经典-Java实现】【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】

【003-Longest Substring Without Repeating Characters(最长非重复子字符串)】给定一个字符串,找字符中的最大非重复子串用start记录当前处理的开始位置...

【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...

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) 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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[LeetCode]Longest Substring Without Repeating Characters
举报原因:
原因补充:

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