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

无重复字符的最长子串

题目内容

给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
来源:力扣(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

猜你喜欢

转载自blog.csdn.net/weixin_40741512/article/details/112434661