力扣-无重复字符的最长子串
程序员文章站
2022-03-30 23:49:39
package com.JackChen.leetCode.Day1;import java.util.HashSet;import java.util.Scanner;/** * 无重复字符的最长子串 * * 输入: s = "abcabcbb" * 输出: 3 * 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3 * * @author Jack chen * */public class MaxLength {public static void...
package com.JackChen.leetCode.Day1;
import java.util.HashSet;
import java.util.Scanner;
/**
* 无重复字符的最长子串
*
* 输入: s = "abcabcbb"
* 输出: 3
* 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3
*
* @author Jack chen
*
*/
public class MaxLength {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
getMaxLength(scanner.nextLine());
}
}
private static void getMaxLength(String input) {
int maxLegth = 0;
for (int i = 0; i < input.length(); i++) {
for (int j = i + 1; j < input.length(); j++) {
HashSet<Character> set = new HashSet<Character>();
for (int k = i; k <= j; k++) {
if (set.contains(input.charAt(k))) {
break;
}
set.add((char) input.charAt(k));
}
maxLegth = maxLegth > set.size() ? maxLegth : set.size();
}
}
System.out.println(maxLegth);
}
}
本文地址:https://blog.csdn.net/github_28948711/article/details/111940009
上一篇: toString 方法