找出其中不含有重复字符的 最长子串 的长度 力扣3

题目:

题目链接:

https://leetcode.cn/problems/longest-substring-without-repeating-characters/

题目内容:

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

题解:语言java

方法1:

没有使用其他数据结构,使用字符串最low的拼接、截取和迭代,耗时和内存占用都很大

代码:

   public int lengthOfLongestSubstring(String strFath) {
        String curr = strFath;
        String temp = "";
        String result = "";
        int i = 0;
        while (i<curr.length()){
            if (temp.indexOf(curr.charAt(i))>-1){
                if (result.length()<temp.length()){
                    result = temp;
                }
                temp = "";
                curr = curr.substring(curr.indexOf(curr.charAt(i))+1,curr.length());
                i = 0;
                continue;
            }
            temp = temp + curr.charAt(i);
            i++;
        }
        return result.length()>temp.length()?result.length():temp.length();
    }

方法2:

方法1改进:只记录了索引,减少大量的字符串裁剪,执行时间300多毫秒

代码:

  public int lengthOfLongestSubstring(String strFath) {
       String curr = strFath;
        String temp = "";
        String result = "";
        int startIndex = 0;
        int exitIndex = 0;
        int i = 0;
        while (i<curr.length()){
            exitIndex = temp.indexOf(curr.charAt(i));
            if (exitIndex >-1){
                if (result.length()<temp.length()){
                    result = temp;
                }
                startIndex = startIndex + exitIndex + 1;
                i = startIndex;
                temp = "";
                continue;
            }
            temp = temp + curr.charAt(i);
            i++;
        }
        return result.length()>temp.length()?result.length():temp.length();
    }

方法3:

引入LinkedList记录已经出现过的字符,利用它可以removeFirst()的优点,减少循环次数提升性能,时间和内存占用提升明显,执行时间20毫秒

 public int lengthOfLongestSubstring(String strFath) {
       String curr = strFath;
        int resu = 0;
        LinkedList<Character> occ = new LinkedList<Character>();
        int n = curr.length();
        char currChar = ' ';
        for (int i = 0;i<n;i++){
            currChar = curr.charAt(i);
            if (!occ.contains(currChar)){
                occ.add(currChar);
            }
            else {
                resu = Math.max(resu,occ.size());
                while (occ.contains(currChar)){
                    occ.removeFirst();
                }
                occ.add(currChar);
            }
        }
        resu = Math.max(resu,occ.size());
        return resu;
    }

方法4:

使用官方给出的解题思路(移动窗口思想),时间直接减少到5ms

 public int lengthOfLongestSubstring(String strFath) {
     Set<Character> resuSet = new HashSet<Character>();
        int ans = 0;
        int k = -1;
        int n = strFath.length();
        for (int i = 0;i<n;i++){
            if (i>0){
                resuSet.remove(strFath.charAt(i-1));
            }
            while (k+1<n && !resuSet.contains(strFath.charAt(k+1))){
                resuSet.add(strFath.charAt(k+1));
                k = k+1;
            }
            ans = Math.max(ans,k+1-i);
        }
        return ans;
    }

感悟:官方的解法还是经典

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值