资讯专栏INFORMATION COLUMN

[Leetcode] Longest Substring Without Repeating Cha

FleyX / 2796人阅读

摘要:哈希表是最自然的想法。在遍历字符串时,我们先根据哈希表找出该字符上次出现的位置,如果大于等于子字符串首,便更新子字符串首。结束后,将该字符新的位置放入哈希表中。

Longest Substring Without Repeating Characters 最新更新解法:https://yanjia.me/zh/2018/12/...
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.
哈希表双指针法 Hash Table with Two Pointers 复杂度

时间O(n) 空间O(1) 字符种类数有限

思路

根据最长不重复子字符串的定义,我们在遍历字符串时一旦遇到当前子字符串中已有字符,那么新的子字符串应该从这个重复点的下一位开始。所以为了在一次遍历中就能找出最大长度,我们需要记录两个东西。第一是某个字母是否在当前子字符串中出现过,第二是如果出现过那它在字符串中的位置是什么。而最简单的判断某个字符是否在当前子字符串中出现过的方法,就是看它上次出现的位置是在当前子字符串第一个字符的前面还是后面,所以我们只要记录该字符上次出现的位置就可以同时满足这两个要求。哈希表是最自然的想法。在遍历字符串时,我们先根据哈希表找出该字符上次出现的位置,如果大于等于子字符串首,便更新子字符串首。因为这里相当于上一个子字符串已经结束,我们还要更新最大长度。结束后,将该字符新的位置放入哈希表中。

注意

遍历完成后还要有一次额外的更新最大长度的操作,以处理最长字符串在末尾的情况。

新的子字符串的起点应该是重复元素的下标加一

字符串问题要注意处理空字符串的特例

所记录的上次出现位置大于等于lowerBound时就需要更新了,注意这个last >= lowerBound

代码
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s == null || s.length() == 0){
            return 0;
        }
        Map table = new HashMap();
        int lowerBound = 0, max = 1;
        for(int i = 0; i < s.length(); i++){
            Character c = s.charAt(i);
            Integer last = table.get(c);
            if(last != null && last >= lowerBound){
                max = Math.max(i - lowerBound, max);
                lowerBound = last + 1;
            }
            table.put(c, i);
        }
        return Math.max(s.length() - lowerBound, max);
    }
}

2018/2

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        """
        :type s: str
        :rtype: int
        """
        characters = {}
        startIndex = 0
        endIndex = 0
        maxLen = 0
        while endIndex < len(s):
            c = s[endIndex]
            maxLen = max(maxLen, endIndex - startIndex)
            if c in characters and characters[c] >= startIndex:
                startIndex = characters[c] + 1
            characters[c] = endIndex
            endIndex = endIndex + 1
        maxLen = max(maxLen, endIndex - startIndex)
        return maxLen
后续 Follow Up

Q: 能否不用哈希表完成本题?
A:可以将哈希表换成和字符一一映射的数组。如果是ASCII字符集,可以初始化一个256个元素的数组,数组的下标对应于字符的ASCII码,而数组的内容则是每个字符上次出现的位置。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/64392.html

相关文章

  • [LeetCode] Longest Substring Without Repeating Cha

    Problem Given a string, find the length of the longest substring without repeating characters. Examples Given abcabcbb, the answer is abc, which the length is 3. Given bbbbb, the answer is b, with the...

    graf 评论0 收藏0
  • [LeetCode] Longest Substring Without Repeating Cha

    摘要:建立数组,存储个字符最近一次出现的位置。首次出现某字符时,其位置标记为,并用无重复字符计数器记录无重复字符的长度,再在更新其最大值。循环完整个字符串后,返回最大值。 Problem Given a string, find the length of the longest substring without repeating characters. Examples: Given ...

    CoderStudy 评论0 收藏0
  • leetcode 3 Longest Substring Without Repeating Cha

    摘要:题目详情题目要求输入一个字符串,我们要找出其中不含重复字符的最长子字符串,返回这个最长子字符串的长度。对于字符串中的每一个字符,先判断中是否已经存在这个字符,如果不存在,直接将添加到,如果已存在,则新的字符串就从不含前一个字符的地方开始。 题目详情 Given a string, find the length of the longest substring without repe...

    xcold 评论0 收藏0
  • [LintCode] Longest Substring Without Repeating Cha

    摘要:哈希表法双指针法只有当也就是时上面的循环才会结束,,跳过这个之前的重复字符再将置为 Problem Given a string, find the length of the longest substring without repeating characters. Example For example, the longest substring without repeat...

    Scliang 评论0 收藏0
  • [Leetcode]Longest Substring Without Repeating Char

    摘要:解题思路本题借助实现。如果字符未出现过,则字符,如果字符出现过,则维护上次出现的遍历的起始点。注意点每次都要更新字符的位置最后返回时,一定要考虑到从到字符串末尾都没有遇到重复字符的情况,所欲需要比较下和的大小。 Longest Substring Without Repeating CharactersGiven a string, find the length of the lon...

    awesome23 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<