现在的位置: 首页 > 综合 > 正文

[LeetCode] Longest Substring Without Repeating Characters

2018年04月26日 ⁄ 综合 ⁄ 共 455字 ⁄ 字号 评论关闭

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) {
        int ret = 0;
        int *flag = new int[256];
        fill(flag, flag +256, 0);
        for(int i = 0, j = 0; j < s.size(); j++)
        {
            while(flag[s[j]] == 1)
            {
                flag[s[i]] = 0;
                i++;
            }
            flag[s[j]] = 1;
            ret = max(ret, j - i + 1);
        }
        delete flag;
        return ret;
    }
};

抱歉!评论已关闭.