LeetCode03-无重复字符的最长子串

本文探讨了三种高效算法来解决寻找字符串中最长无重复字符子串的问题,包括使用哈希映射、双指针技术和滑动窗口方法,旨在帮助读者理解和掌握不同场景下的最优解策略。
题目描述

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

示例 :

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

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

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers

代码
  • 方法一(自己思路实现)

    思路和下面大神一的差不多:首先将字符按照先后次序依次存入map,当发现待存入字符在map中已有的时候,更新最长字符串的长度值(longSize),删除map中value值小于该字符在map存储的数组索引值的元素—>更新map,再将该元素存入map,重复上面过程,知道数组遍历结束

    public int lengthOfLongestSubstring(String s) {
        int longSize = 0;
        char[] arr = s.toCharArray();
        Map<Character, Integer> map = new HashMap<Character, Integer>();
        for (int i = 0; i < arr.length; i++) {
            if (!map.containsKey(arr[i])) {
                map.put(arr[i], i);
            } else {
                if (map.size() > longSize)
                    longSize = map.size();
                Iterator<Map.Entry<Character, Integer>> iterator = map.entrySet().iterator();
                int limit = map.get(arr[i]);
                while (iterator.hasNext()) {
                    Map.Entry<Character, Integer> key = iterator.next();
                    if (key.getValue() <= limit) {
                        iterator.remove();
                    }
                }
                map.put(arr[i], i);
            }
        }
        longSize = map.size() > longSize ? map.size() : longSize;
        return longSize;
    }
    
    
  • 方法二(大神一)

    The idea is use a hash set to track the longest substring without repeating characters so far, use a fast pointer j to see if character j is in the hash set or not, if not, great, add it to the hash set, move j forward and update the max length, otherwise, delete from the head by using a slow pointer i until we can put character j to the hash set.
    这个想法是使用一个散列将跟踪到目前为止最长的没有重复子串字符,使用快速指针j看该字符是否在散列集,如果没有,很好,将它添加到散列集,推动j和更新的最大长度,否则,删除从头部通过使用一个指针缓慢我直到我们可以把字符j散列集。

    public int lengthOfLongestSubstring(String s) {
        int i = 0, j = 0, max = 0;
        Set<Character> set = new HashSet<>();
        
        while (j < s.length()) {
            if (!set.contains(s.charAt(j))) {
                set.add(s.charAt(j++));
                max = Math.max(max, set.size());
            } else {
                set.remove(s.charAt(i++));
            }
        }
        
        return max;
    }
    
  • 方法三(大神二:滑动窗口+数组)

    参考:参考链接
    记录当前最长字符串,同时窗口向右移动,无论何时都使得当前窗口含有当前最长不重复子串,知道窗口移到底部。返回最大长度。
    注:有个地方取了巧,在遍历字符串的时候将遍历过的字符记录在自定义长度为95的数组里(与ASCII码表相对应)。

    public int lengthOfLongestSubstring(String s)
    {
        char[] strs = s.toCharArray();
        int[] isValid = new int[95];
        int result = 0;
        int max = 0;
        for (int i = 0; i < strs.length; i++)
        {
            if (isValid[strs[i] - ' '] != 0)
            {
                result = max > result ? max : result;
                for (int j = i - max; j < isValid[strs[i] - ' ']; j++)
                {
                    max--;
                    isValid[strs[j] - ' '] = 0;
                }
            }
            max++;
            isValid[strs[i] - ' '] = i + 1;
        }
        return max > result ? max : result;
    }
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值