LeetCode T3 Longest Substring Without Repeating Characters

题目地址:

中文:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
英文:https://leetcode.com/problems/longest-substring-without-repeating-characters/

题目描述:

Given a string s, find the length of the longest substring without repeating characters.

Example 1:

Input: s = “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.

Example 2:

Input: s = “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.

Example 3:

Input: s = “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.

Example 4:

Input: s = “”
Output: 0

Constraints:

0 <= s.length <= 5 * 10^4 s consists of English letters, digits,
symbols and spaces.

思路:

思路1:硬暴力

硬暴力,双层循环遍历所有子串,最后找到答案。
但是暴力超时。
题解1是我最初写的,有一个样例过不了。
题解2是官方给的一个暴力答案,还是过不了最长的那个样例。

思路2:滑动窗口

滑动窗口,设置两个指针i和j向前移动,如果当前的字符串字母都是不同的,那么继续扩大窗口j++,直到不符合要求为止,如果不符合要求那就继续i++,直到重复的字母被跳过去为止,然后继续判断。代码见题解3。
题解3的代码慢一点,题解4的更快。思路是一样的。题解3的代码要反复创建hashset进行判断。

题解:

题解1:
//错误示范 超时一个样例
public int lengthOfLongestSubstring(String s) {
    if(s==null) return 0;
    char [] stringArr = s.toCharArray();
    int res = 0;
    int[] flag = new int[128];
    for(int i=0;i<128;i++) flag[i] = 0;
    int len = s.length();

    int cnt=0;
    for(int i=0;i<len;i++){
        for(int j=0;j<len;j++){
            for(int k=i;k<=j;k++){
                int temp = stringArr[k] - ' ';
                if(flag[temp]==0) {flag[temp]=1;cnt++;}
                else if(flag[temp]!=0) {
                    cnt = 0;
                    for(int m=0;m<128;m++) flag[m] = 0;
                    break;
                }
            }
            for(int m=0;m<128;m++) flag[m] = 0;
            res = Math.max(cnt,res);
            cnt = 0;
        }
    }
    return res;
}
题解2:
public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int n = s.length();
        int ans = 0;
        for (int i = 0; i < n; i++)
            for (int j = i + 1; j <= n; j++)
                if (allUnique(s, i, j)) ans = Math.max(ans, j - i);
        return ans;
    }

    public boolean allUnique(String s, int start, int end) {
        Set<Character> set = new HashSet<>();
        for (int i = start; i < end; i++) {
            Character ch = s.charAt(i);
            if (set.contains(ch)) return false;
            set.add(ch);
        }
        return true;
    }
}
题解3:
class Solution {
    public static int lengthOfLongestSubstring(String s) {
        if(s.equals("")) return 0;
        int res = 0;
        int i=0,j=1;

        while(i<s.length()&&j<=s.length()){
            if(allUnique(s,i,j))
            {
                res = Math.max(res,j-i);
                j++;
            }
            else {
                i++;
            }
        }

        return res;
    }

    public static boolean allUnique(String s, int start, int end) {
        Set<Character> set = new HashSet<>();
        for (int i = start; i < end; i++) {
            Character ch = s.charAt(i);
            if (set.contains(ch)) return false;
            set.add(ch);
        }
        return true;
    }
}
题解4:
public class Solution {
    public int lengthOfLongestSubstring(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;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值