算法四:无重复字符的最长子串

无重复字符的最长子串

题目内容

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters

算法思想

该算法为求不含有重复字符的最长子串的长度。算法思想我们用一下图片进行描述:
算法思想

整体算法

public class Day_04 {
    static Scanner input=new Scanner(System.in);
    public static int lengthOfLongestSubstring(String s) {
        int count=0;
        int [] number=new int[s.length()];
        int l=0;
        if(s.isEmpty()){
            return count;
        }else {
            for (int i = 1; i < s.length(); i++) {
                for (int k = i-count-1; k < i; k++) {
                    if(s.charAt(i)==s.charAt(k)){
                        i=k+1;
                        number[l]=count;
                        l++;
                        count=0;
                        break;
                    }else{
                        if(i==(k+1)){
                            count=count+1;
                        }
                    }
                }
            }
            number[l]=count;
        }
        Arrays.sort(number);
        return number[number.length-1]+1;
    }

    public static void main(String[] args) {
        String s=input.nextLine();
        int count=lengthOfLongestSubstring(s);
        System.out.println(count);
    }
}

尾语

以上属于个人见解,有好的想法可以在下方评论写出自己的想法,大家一起进步。该题是力扣上的题,若有侵权,请及时告知。该题链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Lveson

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

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

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

打赏作者

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

抵扣说明:

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

余额充值