Longest Substring Without Repeating Characters

原创 2015年11月19日 13:01:00

题意:
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.
代码:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        const int SIZE=256;
        int record[SIZE];
        memset(record,-1,sizeof(record));// 当顺序遍历到字符 s[i],s[i] 最后出现的位置 

        int final_ans=0;
        int new_start=0; // 这一轮不重复字符串的起点
        for(int i=0;i<s.size();++i){
            if(record[(int)s[i]]>=new_start)
                new_start=record[(int) s[i]]+1;//新的起点
            if(i-new_start+1>final_ans) // 更新 final_ans
                final_ans=i-new_start+1;
            record[(int)s[i]]=i; // 更新 s[i] 最后一次出现的地方
        }
        return final_ans;
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【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个字)