您好,欢迎来到汇意旅游网。
搜索
您的当前位置:首页LeetCode|Longest Substring Without Repeating Characters

LeetCode|Longest Substring Without Repeating Characters

来源:汇意旅游网

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

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

class Solution {
public:
    int lengthOfLongestSubstring(string str) {
        int n = str.length();
        if(n == 0) return 0;
        int s = 0;
        bool table[256] = {false};
        int cnt = 0, res = 0;
        for(int i = 0; i < n; i++){
            int index = str[i];
            if(table[index]){
                while(str[s] != str[i]){
                    table[str[s++]] = false;
                    cnt--;
                } s++;
            }else {
                table[index] = true;
                res = max(res, ++cnt);
            }
        } return res;
    }
};

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- hids.cn 版权所有 赣ICP备2024042780号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务