Longest Substring Without Repeating Characters

原创 2016年06月02日 10:07:41

题意:给定一个字符串,找出不含相同字母的连续子串


思路:复杂度o(n)

用一个数组used[500]记录每个字符出现的位置,用tempRes记录当前子串的长度。用pointer记录当前不重复的字串开始位置

若出现重复,则tempRes的初始值为i-used[s[i]](就是该字母上一次出现的位置到当前位置,能保证之前的不重复),同时将pointer更新为used[s[i]+1],即该字母重复出现的次数的下一个。字符串标记从1开始,所以一开始要加一个随机字符(这里是q)上代码

    int lengthOfLongestSubstring(string s) {
        string temp="";
        int i=0,length=s.length(),ans=0,pointer=1,tempRes=0;
        if(length==0) return 0;
        int used[500]={0};
        temp+="q";
        temp+=s;
        length=temp.length();
        for(i=1;i<length;i++)
        {
            if(used[temp[i]]==0||used[temp[i]]<pointer)
            {
                tempRes++;
                used[temp[i]]=i;
            }
            else
            {
                pointer=used[temp[i]]+1;
                ans=max(tempRes,ans);
                tempRes=i-used[temp[i]];
                used[temp[i]]=i;
            }
        }
        ans=max(tempRes,ans);
        return ans;
    }

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

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