leetoj Longest Substring Without Repeating Characters

原创 2015年07月10日 15:28:08

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.

本题可以通过寻找最近的两个相同元素之间的元素个数总数加上1来不断的更新最长长度可以得出结果

class Solution 
{
public:
    int lengthOfLongestSubstring(string s) 
    {
        int length=0;
        int begin=0;
        vector<int> vec(256,-1);
        for(int i=0;i<s.size();i++)
        {
            begin=max(begin,vec[s[i]]+1);//表示的是之前元素和现在正在访问的相同元素的起点的位置
            vec[s[i]]=i;//更新位置
            length=max(length,i-begin+1); 
        }      
        return length;
    }
};

下面是使用map来实现的

class Solution 
{
public:
    int lengthOfLongestSubstring(string s) 
    {
        int length=0;
        int begin=0;
        int pre=-1;
        map<char,int> ma;
        map<char,int>::iterator it;
        for(int i=0;i<s.size();i++)
        {
            it=ma.find(s[i]);
            if(it!=ma.end())
            {
                pre=it->second;
            }
            else
            {
                pre=-1;
            }
            begin=max(begin,pre+1);//表示的是之前元素和现在正在访问的相同元素的起点的位置
            ma[s[i]]=i;//更新位置
            length=max(length,i-begin+1); 
        }
        return length;
    }
};
版权声明:可以随意转载

相关文章推荐

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

3. Longest Substring Without Repeating Characters

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

leetcode: Longest Substring Without Repeating Characters

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

Longest Substring Without Repeating Characters

题目:Given a string, find the length of the longest substring without repeating characters. For exampl...
  • vgxpm
  • vgxpm
  • 2015-08-29 03:17
  • 552

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. ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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