LeetCode 题解(31): 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.

题解:

用一个vector统计每个位置可达到的“最长-无重复字符-子串”,用map记录每个字符最后一次出现的位置。

当前最长子串 = min(当前位置 - 当前位置字符上次出现位置, 上一个位置的最长子串+1)。如下例所示:

位置 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18

字符 w l r b b m q b h c  d  a   r    z   o    w  k   k   y

长度 1 2 3 4 1 2 3  3 4 5 6  7   8   9   10  11 12 1   2


class Solution {
public:
    int lengthOfLongestSubstring(string s) {

		if(!s.length())
            return 0;

        vector length(s.length(), 0);
		map position;
        for(int i = 0; i < s.length(); i++)
        {
            if(!i)
            {
                length[i] = 1;
				position.insert(pair(s[i], i));
                continue;
            }
            
            if(position.count(s[i]) > 0)
			{
				length[i] = min(i - position[s[i]], length[i-1]+1);
				position[s[i]] = i;
			}
            else
			{
                length[i] = length[i-1] + 1;
				position[s[i]] = i;
			}
        }

		sort(length.begin(), length.end());
		return length.back();

    }
};

八个月后重做:忘记了规律,想了好久。若某字符(现在出现的位置-上次出现的位置)> max[current-1],则max[current] = max[current-1] + 1,否则max[current] = current_pos - last_pos。

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        if(!s.length())
            return 0;
        
        int pos[256];
        for(int i = 0; i < 255; i++)
            pos[i] = -1;
        
        int* max = new int[s.length()];
        for(int i = 0; i < s.length(); i++)
            max[i] = 0;
        max[0] = 1;
        pos[s[0]] = 0;
        
        int current_max = 1;

        for(int i = 1; i < s.length(); i++) {
            max[i] = min(max[i-1] + 1, i - pos[s[i]]);
            if(max[i] > current_max)
                current_max = max[i];
            pos[s[i]] = i;
        }
        
        return current_max;
    }
};

Java版

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length() == 0)
            return 0;
        
        int[] pos = new int[256];
        for(int i = 0; i < 255; i++)
            pos[i] = -1;
        
        int[] max = new int[s.length()];
        for(int i = 0; i < s.length(); i++)
            max[i] = 0;
        
        max[0] = 1;
        pos[s.charAt(0)] = 0;
        
        int current_max = 1;

        for(int i = 1; i < s.length(); i++) {
            max[i] = Math.min(max[i-1] + 1, i - pos[s.charAt(i)]);
            if(max[i] > current_max)
                current_max = max[i];
            pos[s.charAt(i)] = i;
        }
        
        return current_max;        
    }
}

Python版

class Solution:
    # @return an integer
    def lengthOfLongestSubstring(self, s):
        if len(s) == 0:
            return 0
        
        pos = [-1] * 256
        pos[ord(s[0])] = 0
        max = [0] * len(s)
        max[0] = 1
        current_max = 1
        for i in range(1, len(s)):
            max[i] = min(max[i-1] + 1, i - pos[ord(s[i])])
            if max[i] > current_max:
                current_max = max[i]
            pos[ord(s[i])] = i
        return current_max


你可能感兴趣的:(算法)