Leetcode 3 无重复字符的最长子串(中等)(滑动窗口)

该博客介绍了如何使用滑动窗口算法来寻找字符串中不包含重复字符的最长子串长度。在Java实现中,通过HashMap存储字符及其索引,不断更新最大子串长度。当遇到重复字符时,调整窗口左边界。博客深入解析了HashMap在算法中的应用和滑动窗口的原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。

示例 1:

输入: s = “abcabcbb”
输出: 3
解释: 因为无重复字符的最长子串是 “abc”,所以其长度为 3。

package com.xuyinda;

    import java.util.HashMap;
    import java.util.List;

    public class Solution {

            public static int lengthOfLongestSubstring(String s) {
                if(s==null){
                    return 0;
                }
                HashMap<Character, Integer> map = new HashMap<Character, Integer>();
                int left = 0;
                int max = 0;
                for(int i = 0; i <s.length();i++){
                    if(map.containsKey(s.charAt(i))) {
                    left = Math.max(left,map.get(s.charAt(i))+1);
                    }
                    //解释点一
                    map.put(s.charAt(i),i);
                    max = Math.max(max,i - left + 1);
                    //解释点二
                }
                return max;


            }

    }


Explanation 2 :It is easy to understand this part. (i-left+1) means the current window’s length. And the variable max represents the longest window’s length.
If the currrent window’s length is bigger than the last max, the value of max will be replaced by the bigger one.

Explanation 1 :If the new character repeats with the character in the map, the window’s left border will change. At first, I thought left = Math.max(map.get(s.charAt(i))+1) was enough. However, I missed one situation that the biggest index number of the character in the map might be smaller than the index number of current left border.

Summary:By resolving this question, I get a deep understanding of Hash Map and the algorithm of sliding window.

### LeetCode &#39;无重复字符长子&#39; 的 Python 实现 此问题的目标是从给定字符中找到不包含任何重复字符的长子长度。以下是基于滑动窗口算法的一种高效解决方案。 #### 方法概述 通过维护一个动态窗口来跟踪当前无重复字符的子范围,可以有效地解决该问题。具体来说,利用哈希表记录每个字符近一次出现的位置,并调整窗口左边界以排除重复项。 #### 代码实现 以下是一个标准的 Python 解决方案: ```python def lengthOfLongestSubstring(s: str) -> int: char_index_map = {} # 存储字符及其新索引位置 max_length = 0 # 记录大子长度 start = 0 # 当前窗口起始位置 for i, char in enumerate(s): # 遍历字符 if char in char_index_map and char_index_map[char] >= start: # 如果发现重复字符,则更新窗口起点 start = char_index_map[char] + 1 char_index_map[char] = i # 更新或新增字符对应的索引 current_length = i - start + 1 # 当前窗口大小 max_length = max(max_length, current_length) # 更新大长度 return max_length ``` 上述代码的核心逻辑在于使用 `char_index_map` 来存储已访问过的字符以及它们后出现的位置[^1]。当遇到重复字符时,重新计算窗口的起始点并继续扩展窗口直到遍历结束[^2]。 对于输入 `"abcabcbb"`,执行过程如下: - 初始状态:`start=0`, `max_length=0` - 处理到第3个字符 `&#39;c&#39;` 之前未检测到重复,此时 `max_length=3` - 发现有重复字符 `&#39;a&#39;` 后移动窗口左侧至新位置,终返回结果为 `3`. 同样地,在处理像 `"bbbbb"` 这样的极端情况时也能正确得出答案为 `1`[^4]. #### 时间复杂度与空间复杂度分析 时间复杂度 O(n),其中 n 是字符长度;因为每个字符多被访问两次——加入和移除窗口各一次。 空间复杂度 O(min(m,n)) ,m 表示 ASCII 字符集大小 (通常固定为128), 而 n 取决于实际输入字符长度[^5]. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值