【leetcode刷题笔记】003.Longest Substring Without Repeating Characters

日期:20180910
题目描述:

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

Examples:
Input: "abcabcbb"
Output: 3 
Explanation: The answer is "abc", which the length is 3.
详解:

这道题一看,突然想到第一题,这不就是遍历每一个的时候查找一下之前有没有重复的吗,既然是查找那再来个哈希表吧。然后我就模仿第一题来了一遍。结果弄巧成拙还不如简单的循环来的实在。为啥呢?原来是因为哈希表虽然查找快速,但是建表较慢,而我的程序中需要不停的建表。还不如老老实实循环。唉,偷鸡不成蚀把米!

我的440ms答案:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map myMap;
        int max = 0,i = 0,tmp = 0;
        while(s[i]!='\0'){
            if(myMap.find(s[i])!=myMap.end()){//找到啦
                if(max>tmp) tmp = max;
                max = 0;
                i = myMap[s[i]];
                myMap.clear();
            }else{//没找到
                max++;
                myMap[s[i]] = i;
            }
            i++;
        }
        return max>tmp?max:tmp;
    }
};

12ms的答案:

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int result = 0;
        vector cache(256, 0);
        for (int i = 0, j = 0; i < s.size(); i++) {
            j = max(j, cache[s[i]]);
            cache[s[i]] = i + 1;
            result = max(result, i - j + 1);
        }
        return result;
    }
};

上面的答案的基本思路是,找到当前元素之前最近的重复元素,这里的重复元素分两种情况,一种是和当前元素相同,一种是本身属于重复元素。举个例子,abccefga中,如果遍历到最后一个a,那么这时应该找到的是第一个c的位置。那么怎么保证找到的是c而不是a呢,这里的变量j就起到了这个作用。如果有比cache[s[i]]还大的j,那就找到j,因为j是保存的上一个重复元素的位置。

这个算法非常简洁,也非常的绕,循环里面的三行代码顺序很重要,如果各位看不明白一定要细细揣摩,才能明白其中的玄妙。

你可能感兴趣的:(【leetcode刷题笔记】003.Longest Substring Without Repeating Characters)