leetcode算法—无重复字符的最长子串 Longest Substring Without Repeating Characters

关注微信公众号:CodingTechWork,一起学习进步。
在这里插入图片描述

题目

Longest Substring Without Repeating Characters:
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.

题解

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if (s.length() == 0) {
            return 0;
        }
        //HashMap key记录字符,value记录索引
        Map<Character, Integer> map = new HashMap<>();
        //记录滑动过程中最大子串长度
        int max = 0;
        //记录窗口左边下标索引
        int left = 0;
        for (int i = 0; i < s.length(); i++) {
            if (map.containsKey(s.charAt(i))) {
                //遇到相同的字符,窗口左索引往右移动一位,所以要+1
                left = Math.max(left, map.get(s.charAt(i)) + 1);
            }
            //覆盖重复的key,如abca,第一次是a对应的i=0,第二次put时,a对应的i=3
            map.put(s.charAt(i), i);
            max = Math.max(max, i-left+1);
        }
        return max;
    }
}

只遍历一次,时间复杂度为o(n)

1. 初始状态
在这里插入图片描述

2. 滑动i=0,left=0
在这里插入图片描述
i = 0时:

  1. map中不包含a,将a放入map,且map.get(‘a’) = 0,
  2. left=Math.max(left, map.get(s.charAt(i)) + 1)=0,
  3. max=Math.max(max, i - left + 1) = 1;

3. 滑动i=1,left=0
在这里插入图片描述
i = 1时:

  1. map中不包含b,将b放入map,且map.get(‘b’) = 1,
  2. left=Math.max(left, map.get(s.charAt(i)) + 1)=0,
  3. max=Math.max(max, i - left + 1) = 2;

2. 滑动i=2,left=0
在这里插入图片描述
i = 2时,

  1. map中不包含c,将c放入map,且map.get(‘a’) = 2,
  2. left=Math.max(left, map.get(s.charAt(i)) + 1) = 0,
  3. max=Math.max(max, i - left + 1) = 3;

4. 滑动i=3,left=1
在这里插入图片描述
i = 3时:

  1. map中包含a,map.get(s.charAt(i)) = 0, left=Math.max(left, map.get(s.charAt(i)) + 1) = 1,向右移动1位。
  2. 将a放入map,且map.get(‘a’) = i = 3,
  3. max=Math.max(max, i - left + 1) = 3;

4. 滑动i=4,left=2
在这里插入图片描述
i = 4时:

  1. map中包含b,map.get(s.charAt(i)) = 1, left=Math.max(left, map.get(s.charAt(i)) + 1) = 2,向右移动1位。
  2. 将b放入map,且map.get(‘b’) = i = 4,
  3. max=Math.max(max, i - left + 1) = 3;

5. 滑动i=5,left=3
在这里插入图片描述
i = 5时:

  1. map中包含c,map.get(s.charAt(i)) = 2, left=Math.max(left, map.get(s.charAt(i)) + 1) = 3,向右移动1位。
  2. 将c放入map,且map.get(‘c’) = i = 5,
  3. max=Math.max(max, i - left + 1) = 3;

6. 滑动i=6,left=5
在这里插入图片描述
i = 6时:

  1. map中包含b,map.get(s.charAt(i)) = 4, left=Math.max(left, map.get(s.charAt(i)) + 1) =5,向右移动2位。
  2. 将b放入map,且map.get(‘b’) = i = 6,
  3. max=Math.max(max, i - left + 1) = 3;

7. 滑动i=7,left=5
在这里插入图片描述
i = 7时:

  1. map中包含b,map.get(s.charAt(i)) = 6, left=Math.max(left, map.get(s.charAt(i)) + 1) =7,向右移动2位。
  2. 将b放入map,且map.get(‘b’) = i = 7,
  3. max=Math.max(max, i - left + 1) = 3;

参考 Longest Substring Without Repeating Characters

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值