[LeetCode] Longest Substring Without Repeating Characters

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[LeetCode] Longest Substring Without Repeating Characters相关的知识,希望对你有一定的参考价值。

#include "List.h"
#include <string>
#include <stdlib.h>
using namespace std;

#define max(a, b) (a) > (b) ? (a) : (b)
// LeetCode, Longest Substring Without Repeating Characters
class Solution {
public:
    int lengthOfLongestSubstring(string s) 
    {
        const int ASCII_MAX = 255;
        int last[ASCII_MAX];
        int start = 0;
        int len = s.size();
        fill(last, last + ASCII_MAX, -1);
        int max_len = 0;
        for (int i = 0; i < len; i++) {
            if (last[s[i]] >= start) {
                max_len = max(i - start, max_len);
                start = last[s[i]] + 1;
            }
            last[s[i]] = i;
        }
        
        return max((int)len - start, max_len);
    }
};

int main()
{
//     ListSolution s;
//     ListNode* pHead = nullptr;
// 
//     s.AddToTail(&pHead, 1);
//     s.AddToTail(&pHead, 2);
//     s.AddToTail(&pHead, 3);
//     s.AddToTail(&pHead, 4);
//     s.AddToTail(&pHead, 5);
//     s.AddToTail(&pHead, 6);
//     s.Print(pHead);
//     s.RemoveNode(&pHead, 3);
//     s.RemoveNode(&pHead, 1);
//     s.RemoveNode(&pHead, 2);
//     s.Print(pHead);
//     s.RemoveAll(&pHead);
// 
//     s.AddToTail(nullptr, 1);
//     s.Print(nullptr);
//     s.RemoveNode(nullptr, 1);
//     s.RemoveAll(nullptr);
// 
//     if (nullptr == pHead)
//     {
//         printf("pHead == nullptr\n");
//     }

    string str = "qpxrjxkltzyx";
    Solution s;
    int n = s.lengthOfLongestSubstring(str);
    printf("max_len: %d\n", n);

    return 0;
}

 

以上是关于[LeetCode] Longest Substring Without Repeating Characters的主要内容,如果未能解决你的问题,请参考以下文章

leetcode:longest words

Leetcode409. Longest Palindrome

leetcode:longest-increasing

[Leetcode]Longest Palindromic Substring

leetcode longest consecutive sequence

LeetCode Longest Increasing Subsequence