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-面试算法经典-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个字)