Leetcode题解:Longest Substring Without Repeating Characters

原题:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

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

Example 1:

Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.

Example 2:

Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.

Example 3:

Input: “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

题目给定一个字符串s,要求从中找出最长的不包含重复字符的子串。暴力解法则是遍历从有的子串,并检验是否包含重复字符,从而找到符合条件的最长字串,时间复杂度为 O ( n 3 ) O(n^3) O(n3)。在这个过程中会有大量不必要的重复计算,也就是判断是否包含重复字符这个过程重复的遍历字符。

如果我们能够在遍历的时候能够用一定的数据结构来记录我们遍历过程中获取到的信息(即那些字符是已经有的),那么我们就不必要重复地遍历来获取到这些信息了。

为了记录各个字符是否包含在已经遍历过的字符串中,我们用一个长度为255的数组 status 来记录。status[i] 代表ASCII码为 i 的字符是字符串中的位置(下标),若该字符不在字符串中,则 status[i] 的值为-1。(这里假设字符串包含的字符均为ASCII字符,实际上也可以用长度为128的数组)

通过对字符串 s 进行一次遍历,便能将各个字符是否出现的信息记录到数组 status 中。在遍历过程中可通过 status 来判断是否有出现重复字符。 以下为复杂度为 O ( n ) O(n) O(n)的算法实现:

// golang
// 4ms, 987 test cases
func lengthOfLongestSubstring(s string) int {
    //当前不出现重复字符的子串在s中的偏移量,该子串从s[offset]开始
    offset := 0
    //当前找到的最长的不含重复字符的子串长度
    maxlen := 0
    var curlen int
    var status [255]int 
    //初始化status,-1表示相应的字符未出现
    for i,_ := range status {
        status[i] = -1
    }
    //判断字符s[i]是否出现在子串s[offset:i)中
    for i := int(0); i < len(s); i++ {
        curlen++
        // 字符s[i]与子串s[offset:i)中的字符s[ status[s[i]] ]重复
        if status[s[i]] >= 0 && status[s[i]] >= offset {
            // curlen为子串s[offset:i](注意是闭区间)的长度,因此s[offset:i)的长度为curlen - 1 
            if curlen - 1 > maxlen {
                maxlen = curlen - 1
            }
            // 计算新的子串 s[ status[s[i]] + 1 : i]的offset和curlen
            offset = status[s[i]] + 1
            curlen = i - offset + 1
        } 
        status[s[i]] = i
    }
    if curlen > maxlen {
        maxlen = curlen
    }
    return maxlen
}

其他语言的实现:

# python 68ms
class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        offset = maxlen = curlen = 0
        status = {}
        for i in range(len(s)):
            curlen = curlen + 1
            if status.has_key(s[i]) and status[s[i]] >= offset :
                if curlen - 1 > maxlen :
                    maxlen = curlen - 1
                offset = status[s[i]] + 1
                curlen = i - offset + 1
            status[s[i]] = i
        if curlen > maxlen :
            maxlen = curlen
        return maxlen
//c 12ms
int lengthOfLongestSubstring(char* s) {
    int offset = 0, maxlen = 0, curlen = 0;
    int status[255];
    int i;
    for (i = 0;i < 255;i++) {
        status[i] = -1;
    }
    int len = strlen(s);
    for (i = 0;i < len;i++) {
        curlen++;
        if (status[s[i]] >= 0 && status[s[i]] >= offset) {
            if (curlen - 1 > maxlen) {
                maxlen = curlen - 1;
            }
            offset = status[s[i]] + 1;
            curlen = i - offset + 1;
        }
        status[s[i]] = i;
    }
    
    if (curlen > maxlen) {
        maxlen = curlen;
    }
    return maxlen;
}
//java 27ms
class Solution {
    public int lengthOfLongestSubstring(String s) {
        int offset = 0, maxlen = 0, curlen = 0;
        int[] status;
        status = new int[255];
        int i;
        for (i = 0;i < 255;i++) {
            status[i] = -1;
        }
        int len = s.length();
        for (i = 0;i < len;i++) {
            curlen++;
            int index = (int)s.charAt(i);
            if (status[index] >= 0 && status[index] >= offset) {
                maxlen = Math.max(maxlen,curlen - 1);
                offset = status[index] + 1;
                curlen = i - offset + 1;
            }
            status[index] = i;
        }

        maxlen = Math.max(maxlen,curlen);
        return maxlen;
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值