前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >L_00003_LengthOfLongestSubstring:MAP滑窗解决最长非重复字串

L_00003_LengthOfLongestSubstring:MAP滑窗解决最长非重复字串

作者头像
mingjie
发布2022-05-12 10:09:47
1390
发布2022-05-12 10:09:47
举报
文章被收录于专栏:Postgresql源码分析

题目

https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/

代码语言:javascript
复制
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: "abcabcbb"
输出: 3 
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
示例 2:

输入: "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
示例 3:

输入: "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
     请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

解法

代码语言:javascript
复制
public class L_00003_LengthOfLongestSubstring {
    public int lengthOfLongestSubstring1(String s) {
        if (s == null) {
            return 0;
        }
        Set<Character> charSet = new HashSet<>();
        int result = 0;
        int len = s.length();

        for (int i = 0; i < len; i++) {
            int curLen = 0;
            for (int j = i; j < len; j++) {
                if (charSet.contains(s.charAt(j))) {
                    break;
                }
                charSet.add(s.charAt(j));
                curLen++;
            }
            charSet.clear();
            result = Math.max(result, curLen);
        }

        return result;
    }

    public int lengthOfLongestSubstring2(String s) {
        int n = s.length();
        Set<Character> set = new HashSet<>();
        int ans = 0, i = 0, j = 0;
        while (i < n && j < n) {
            // try to extend the range [i, j]
            if (!set.contains(s.charAt(j))){
                set.add(s.charAt(j++));
                ans = Math.max(ans, j - i);
            }
            else {
                set.remove(s.charAt(i++));
            }
        }
        return ans;
    }

    public int lengthOfLongestSubstring3(String s) {
        int n = s.length(), ans = 0;
        Map<Character, Integer> map = new HashMap<>();
        for (int j = 0, i = 0; j < n; j++) {
            if (map.containsKey(s.charAt(j))) {
                i = Math.max(map.get(s.charAt(j)) + 1, i);
            }
            ans = Math.max(ans, j - i + 1);
            map.put(s.charAt(j), j);
        }
        return ans;
    }
}

lengthOfLongestSubstring2是常规的滑窗方式:

代码语言:javascript
复制
| a   b   c   d   e   c   o   p   s
| a   b | c   d   e   c   o   p   s
| a   b   c | d   e   c   o   p   s
| a   b   c   d | e   c   o   p   s
| a   b   c   d   e | c   o   p   s
| a   b   c   d   e   c | o   p   s   :出现重复了,右窗不动,左窗平移
  a | b   c   d   e   c | o   p   s  
  a   b | c   d   e   c | o   p   s  
  a   b   c | d   e   c | o   p   s   :直到重复值被移出窗口
  a   b   c | d   e   c   o | p   s   
  a   b   c | d   e   c   o   p | s   
  a   b   c | d   e   c   o   p   s | :结果为6

lengthOfLongestSubstring3对上述进行优化,遇到重复值没必要一个一个移动,左窗可以直接跳到str[重复值+1]的位置。

实现使用MAP记录每个值的位置,如果发现重复,让left_index直接跳到重复值位置+1。

代码语言:javascript
复制
i = Math.max(map.get(s.charAt(j)) + 1, i);

左窗口索引要保证只能向右滑动:

代码语言:javascript
复制
| a   b   c   b   a   d   0
| a | b   c   b   a   d   1
| a   b | c   b   a   d   2 
| a   b   c | b   a   d   3 
| a   b   c   b | a   d   3
  a   b | c   b | a   d   3 又遇到a了,如果使用a的索引值左窗就又滑回最左侧
  a   b | c   b   a | d   3
  a   b | c   b   a   d | 4

lengthOfLongestSubstring2:O(2n)

lengthOfLongestSubstring3:O(n)

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 解法
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档