LeetCode003 Longest Substring Without Repeating Characters

详细见:leetcode.com/problems/longest-substring-without-repeating-characters/

说明:map/dict/数组


Java Solution:  github

package leetcode;

import java.util.Arrays;

public class P003_LongestSubstringWithoutRepeating {
	public static void main(String[] args) {
		new Solution1().lengthOfLongestSubstring("mabcdabcdmmm");
		new Solution1().lengthOfLongestSubstring("mabcd");
	}
	/*
	 * 	82.35%
	 */
	static class Solution {
		int[] map = new int[128];
	    public int lengthOfLongestSubstring(String s) {
	        if (s == null)
	        	return 0;
	        if (s.length() < 2)
	        	return s.length();
	        int[] preIndex = new int[s.length()];
	        Arrays.fill(map, -1);
	        for (int i = 0; i != preIndex.length; i ++) {
	        	int index = (int)s.charAt(i);
	        	if (map[index] == -1) {
	        		preIndex[i] = -1;
	        		map[index] = i;
	        	} else {
	        		preIndex[i] = map[index];
	        		map[index] = i;
	        	}
	        }
	        int i0 = 0, i1 = 0, max = Integer.MIN_VALUE;
	        for (; i1 != preIndex.length; i1 ++) {
	        	if (preIndex[i1] >= i0) {
	        		if (max < i1 - i0)
	        			max = i1 - i0;
	        		i0 = preIndex[i1] + 1;
	        	}
	        }
    		if (max < i1 - i0)
    			max = i1 - i0;
	        System.out.println(max);
	        return max;
	    }
	}
	/*
	 * 	将两次O(N)整合成一次O(N)
	 * 	86.95%
	 */
	static class Solution1 {
		int[] map = new int[128];
	    public int lengthOfLongestSubstring(String s) {
	        if (s == null)
	        	return 0;
	        int len = s.length();
	        if (len < 2)
	        	return len;
	        Arrays.fill(map, -1);
	        int i0 = 0, i1 = 0, max = Integer.MIN_VALUE, preIndex = 0;
	        for (; i1 != len; i1 ++) {
	        	int index = (int)s.charAt(i1);
	        	if (map[index] == -1) {
	        		preIndex = -1;
	        		map[index] = i1;
	        	} else {
	        		preIndex = map[index];
	        		map[index] = i1;
	        	}
	        	if (preIndex >= i0) {
	        		if (max < i1 - i0)
	        			max = i1 - i0;
	        		i0 = preIndex + 1;
	        	}
	        }
    		if (max < i1 - i0)
    			max = i1 - i0;
	        return max;
	    }
	}
}


C Solution: github

/*
    url: leetcode.com/problems/longest-substring-without-repeating-characters/
    19ms 60.40%
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int lengthOfLongestSubstring(char* s) {
    int m[128];
    int i = 0, j = 0;
    int n = 0, p = 0;
    int * c = NULL;
    int max = 0;
    int v = 0;
    if (s == NULL || * s == '\0')
        return 0;
    for (i = 0; i < 128; i ++)
        m[i] = -1;
    n = strlen(s);
    c = (int *) malloc(sizeof(int) * n);
    for (i = 0,j = 0; j < n; j ++) {
        v = *(s + j);
        p = m[v];
        m[v] = j;
        if (p >= i) {
            max = max < j - i ? j - i : max;
            i = p + 1;
        }
    }
    max = max < j - i ? j - i : max;
    free(c);
    return max;
}

int main() {
    char s[] = "pwwkew";
    printf("answer is %d\r\n", lengthOfLongestSubstring(s));
    return 0;
}


Python Solution: github

#coding=utf-8

'''
    url: leetcode.com/problems/longest-substring-without-repeating-characters/
    dict保存上一次字符出现的位置,出现重复,从下一个开始计数
    @author:     zxwtry
    @email:      zxwtry@qq.com
    @date:       2017年2月12日
    @details:    Solution1: AC 102ms 71.02%
    @details:    Solution1: AC 192ms 17.26%
'''

class Solution1(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if s == None or len(s) == 0: return 0
        d,m,u = {},1,0
        for i in range(len(s)):
            p = d.get(s[i])
            d[s[i]] = i
            if p == None: p = -1
            elif p >= u:
                m = max(m, i - u)
                u = p + 1
        m = max(m, len(s) - u)
        return m

class Solution2(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        if s ==  None or len(s) == 0:
            return 0
        d, m , u = [-1] * 256, -1, -1
        for j in range(len(s)):
            p = d[ord(s[j])]
            d[ord(s[j])] = j
            m = max(m, j - u)
            u = max(u, p + 1)
        m = max(m, len(s) - u)    
        return m
        
if __name__ == "__main__":
    d = "abba"
    s = Solution2()
    print(s.lengthOfLongestSubstring(d))








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值