LeetCode Longest Substring Without Repeating Characters

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len = s.length();
        if (len < 2) return len;
        char cnt[256];
        memset(cnt, 0, sizeof(cnt));
        
        const char* p = s.c_str();
        const char* q = p + 1;
        const char* end = s.c_str() + len - 1;
        
        int mlen = 1;
        cnt[*p] = 1;
        
        while(q <= end) {
            char ch = *q;
            int clen = q - p + (ch == 0);
            
            if (cnt[ch]) { // the character ch already exist in substr[p,q]
                for(; *p != ch; p++) cnt[*p] = 0;
                p++;
            } else {
                cnt[ch] = 1;
            }
            if (clen > mlen) mlen = clen;
            q++;
        }
        
        return mlen;
    }
};

用[p, q]表示一个子串,cnt[ch]表示在这个子串中字符ch出现的次数,如果q移动到一个新的位置发现该位置上的字符ch对应的cnt[ch]==1,说明字符ch已经在[p, q-1]的子串中出现过,那么符合要求的子串必然不会包括[p, q]因为已经有重复了,那么我们就要让移动到子串中与ch重复的那个字符后面的位置如r(移动的同时要把经过的字符对应的计数清零),那么[r, q]子串中就没有重复了,就又可以继续向后检测了。

第二轮:

Longest Substring Without Repeating Characters

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) {
        if (s.size() <= 1) return s.size();
        int len = s.size();
        int start = 0;
        int dict[256] = {0};
        int maxlen = 1;
        for (int i=0; i<len; i++) {
            dict[s[i]]++;
            if (dict[s[i]] > 1) {
                for (; s[i] != s[start]; start++) {
                    dict[s[start]]--;
                }
                dict[s[start++]]--;
            }
            
            if (i - start + 1 > maxlen) {
                maxlen = i - start + 1;
            }
        }
        return maxlen;
    }
};

还是稍有不畅

依然不畅

 1 // 20:09
 2 class Solution {
 3 public:
 4     int lengthOfLongestSubstring(string s) {
 5         vector<int> stat(256, 0);
 6         int len = s.length();
 7         if (len < 1) return 0;
 8         int res = 0;
 9         int pre = -1;
10         int pos = 0;
11         int maxres = 0;
12         while (pos < len) {
13             char ch = s[pos];
14             stat[ch]++;
15             if (stat[ch] > 1) {
16                 while (s[++pre] != ch) stat[s[pre]]--;
17                 stat[s[pre]]--;
18                 res = pos - pre;
19             } else {
20                 res++;
21             }
22             maxres = max(maxres, res);
23             pos++;
24         }
25         return maxres;
26     }
27 };

看了书,写另一中解法,不过还是不流畅,这题真是。。。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int len = s.size();
        if (len < 2) {
            return len;
        }
        int dict[256];
        for (int i=0; i<256; i++) {
            dict[i] = -1;
        }
        int start = 0;
        int maxlen= 1;
        for (int i=0; i<len; i++) {
            if (dict[s[i]] != -1) {
                start = max(dict[s[i]] + 1, start);
            }
            maxlen = max(maxlen, i - start + 1);
            dict[s[i]] = i;
        }
        return maxlen;
    }
};

 

 

转载于:https://www.cnblogs.com/lailailai/p/3601875.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值