给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度。
package LeetCode; import java.util.HashSet; /** * @program:多线程和IO * @descripton:给定一个字符串s,请你找出其中不含有重复字符的最长子串的长度 * @author:ZhengCheng * @create:2021/9/19-21:02 **/ public class LongestSubstring { public static void main(String[] args) { String s = "abcdefgacdefgahijklmabcefghiajklmnopq"; int i = FindLongestSubstring(s); System.out.println(i); } public static int FindLongestSubstring(String s){ char[] str = s.toCharArray(); int i = 0; HashSet<Object> set = new HashSet<>(); int cnt = 0; int max = 0; while (i < str.length){ if (set.add(str[i])){ cnt++; }else { if (cnt > max){ max = cnt; } set.clear(); set.add(str[i]); cnt = 1; } i++; } return cnt>max?cnt:max; } }
本文介绍了一种寻找字符串中无重复字符最长子串的方法,并提供了一个具体的Java实现案例。通过遍历字符串并利用HashSet来检查字符是否重复,该算法能够有效地找到最长的不重复子串。
206

被折叠的 条评论
为什么被折叠?



