力扣LeetCode算法题第3题-无重复字符的最长子串

要求:

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

我一开始采用的第一种方法是使用hashmap去比对大小,在idea上可以跑通程序,但在leatcode的编译器中,无法通过字符串s="" 和s=" "的校验,很奇怪。

package com.zhm.test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author bige
 * @Date: 2022/11/18 8:39
 * @ApiNote: 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
 */
public class Leatcode_test003 {
    public static int lengthOfLongestSubstring(String s) {
        //每一道算法题,先想出解题思路。
        //1.判空
        //2.思路想法。
        // 2.1.可以使用hashMap<int,String>类型
        // 2.2 key作为索引下标0,1,2,3.   每个字母作为value
        // 2.3 循环比对所有的value,返回key. 求出key的差值大小=length。

        Map<Integer,String> stringHashMap = new HashMap<Integer, String>();
        int max = 0;
        String[] split = s.split("");
        for (int i = 0; i < split.length; i++) {
            stringHashMap.put(i,split[i]);
        }

        if(split.length<1){
            max=0;
        }else if(split.length==1){
            max=1;
        }else{
            for (Map.Entry<Integer, String> entry : stringHashMap.entrySet()) {
                for (int j = entry.getKey()+1; j < split.length; j++) {
                    if(entry.getValue().equals(split[j])){
                        max=(max < (j - entry.getKey()) ) ? (max = (j - entry.getKey())) : max;
                        break;
                    }
                }
            }
        }
    return max;
    }

    public static void main(String[] args){
        String s = " ";
        System.out.println("max="+lengthOfLongestSubstring(s));
    }
}

可以在idea中输出正确的结果。

尝试使用下第二种方法去验证。

利用String本身的方法去进行调用和判断,利用indexOf()方法,可以进行返回首次出现的下标。

public static int lengthOfLongestSubstring(String s) {
        int index=0;//索引
        int des=0;//差值
        for (int i = 0; i < s.length(); i++) {
            if(s.indexOf(s.charAt(i),index) < i){
                index=s.indexOf(s.charAt(i),index)+1;
            }else {
                //des=Math.max( des, i - index + 1);
                des=des>(i-index+1)?des :(i-index+1);
            }
        }
        return des;
    }

代码解析:

1.进入if的唯一条件是出现了同样的字母。

例如测试数据:

String s = "abcabcbb";

首次出现:int index=0;

再次出现:int lastIndex =3;

但由于我们使用的IndexOf()方法,再次出现的a字母,会导致索引返回第一次出现字母a的索引index=0。 所以才能进入到if的判断语句中。

if()方法中,+1的作用是因为每再次遇到相同字母就+1.

比如测试数据:

String s1 = "cacccc";

首次c的索引=0,index=0;

第二次遇到c的索引=2,index=1;

第三次遇到c的索引=3,index=2;

第三次遇到c的索引=4,index=3;

第三次遇到c的索引=5,index=4;

主要是防止重头开始索引,需要移动下标。

贴上所有代码,可以在idea中进行数据测试。

package com.zhm.test;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author bige
 * @Date: 2022/11/18 8:39
 * @ApiNote: 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
 */
public class Leatcode_test003 {
    public static int lengthOfLongestSubstring1(String s) {
        //每一道算法题,先想出解题思路。
        //1.判空
        //2.思路想法。
        // 2.1.可以使用hashMap<int,String>类型
        // 2.2 key作为索引下标0,1,2,3.   每个字母作为value
        // 2.3 循环比对所有的value,返回key. 求出key的差值大小=length。

        Map<Integer,String> stringHashMap = new HashMap<Integer, String>();
        int max = 0;
        String[] split = s.split("");
        for (int i = 0; i < split.length; i++) {
            stringHashMap.put(i,split[i]);
        }

        if(split.length<1){
            max=0;
        }else if(split.length==1){
            max=1;
        }else{
            for (Map.Entry<Integer, String> entry : stringHashMap.entrySet()) {
                for (int j = entry.getKey()+1; j < split.length; j++) {
                    if(entry.getValue().equals(split[j])){
                        max=(max < (j - entry.getKey()) ) ? (max = (j - entry.getKey())) : max;
                        break;
                    }
                }
            }
        }
    return max;
    }

    public static int lengthOfLongestSubstring(String s) {
        int index=0;//索引
        int des=0;//差值
        for (int i = 0; i < s.length(); i++) {
            if(s.indexOf(s.charAt(i),index) < i){
                index=s.indexOf(s.charAt(i),index)+1;
            }else {
                //des=Math.max( des, i - index + 1);
                des=des>(i-index+1)?des :(i-index+1);
            }
        }
        return des;
    }

    public static void main(String[] args){
        String s = "abcabcbb";
        String s1 = "cccccc";
        System.out.println("length="+s.length());
        System.out.println("max="+lengthOfLongestSubstring(s));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值