Leetcode03

2018/10/25 算法题

Leetcode03

使用res数组记录每个字符出现的最后一个位置,之后start是一个标志位,记录上一次重复的最后位置,每一次长度都要从start开始算

static const auto io_sync_off = []() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    return nullptr;
}();
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        vector<int> res (256 , -1);
        int start = -1,maxLen = 0;
        for(int i = 0 ; i < s.length() ; i ++){
            if(res[s[i]] > start)
                start = res[s[i]];
            res[s[i]] = i;
            maxLen = max(maxLen , i - start);
        }
        return maxLen;


Search

    Table of Contents

    本站总访问量