栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

[LeetCode]3.Longest Substring Without Repeating Characters 无重复字符的最长子串(Java)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

[LeetCode]3.Longest Substring Without Repeating Characters 无重复字符的最长子串(Java)

原题地址:longest-substring-without-repeating-characters
题目描述:

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

示例 1:

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

提示:

  • 0 <= s.length <= 5 * 104
  • s 由英文字母、数字、符号和空格组成
解答方法:

滑动窗口算法在一个特定大小的字符串或数组上进行操作,而不在整个字符串和数组上操作,这样就降低了问题的复杂度,从而也达到降低了循环的嵌套深度。其实这里就可以看出来滑动窗口主要应用在数组和字符串上。

1.两个变量记录大小

最开始代码(错误):

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int head = 0, rest = 1, max = 1, i, j;
        for(i = 2; i < s.length(); i++){
            for(j = head + 1; j <= rest; j++){
                if(s.charAt(i) == s.charAt(j)){
                    head = j;
                }
            }
            rest++;
            max = Math.max(max, rest - head);
            }
            return max;
        }
    }

问题:

  • 当字符串长度为0时没考虑
  • 两个变量的走向也没考虑完全

通过代码:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int head = 0, rest = 0, max = 1, i, j, temp = 1;
        if(s.length() == 0)return 0;
        for(i = rest + 1; i < s.length(); i++){
            for(j = head; j <= rest; j++){
                if(s.charAt(i) == s.charAt(j)){
                    head = j + 1;
                }
            }
            rest++;
            temp = rest - head + 1;
            if(rest == head) temp = 1;
            max = Math.max(max, temp);
            }
            return max;
        }
    }

2.HashMap

用哈希表做每个字符和其最后出现位置的映射,减少了遍历的时间。

class Solution {
    public int lengthOfLongestSubstring(String s) {
        if(s.length() == 0) return 0;
        HashMap map = new HashMap();
        int max = 0;
        int left = 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;
    }
}

3.整型数组

用整型数组代替哈希表,在数组中更新left值

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int[] m = new int[256];
        Arrays.fill(m, -1);
        int res = 0, left = -1;
        for (int i = 0; i < s.length(); ++i) {
            left = Math.max(left, m[s.charAt(i)]);
            m[s.charAt(i)] = i;
            res = Math.max(res, i - left);
        }
        return res;
    }
}

4.HashSet

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int res = 0, left = 0, right = 0;
        HashSet t = new HashSet();
        while (right < s.length()) {
            if (!t.contains(s.charAt(right))) {
                t.add(s.charAt(right++));
                res = Math.max(res, t.size());
            } else {
                t.remove(s.charAt(left++));
            }
        }
        return res;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/323068.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号