Skip to content

Commit f926863

Browse files
牛客平台Scanner测试
1 parent 3f9f59b commit f926863

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/main/java/com/base/ScannerDemo.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
public class ScannerDemo {
77
public static void main(String[] args) {
8+
scannerMore();
9+
}
10+
11+
private static void scannerMore() {
812
//输入输出,牛客网之类的平台会用到
913
Scanner scanner = new Scanner(System.in);
1014
while (scanner.hasNext()) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.string;
2+
3+
import java.util.*;
4+
public class MaxSubLength {
5+
public static void main(String[] args) {
6+
Scanner in = new Scanner(System.in);
7+
int result = getMaxSubLength(in.next());
8+
System.out.println(result);
9+
}
10+
11+
/**
12+
* 最长不重复子串的长度。abcdcad
13+
*/
14+
public static int getMaxSubLength(String word) {
15+
if (word==null) {
16+
return 0;
17+
}
18+
int max=0;
19+
int left=0;
20+
Map<Character,Integer> map = new HashMap<>();
21+
for (int i=0;i<word.length();i++) {
22+
//如果重复,重新算下标
23+
if (map.containsKey(word.charAt(i))) {
24+
left = Math.max(left, map.get(word.charAt(i))+1);
25+
}
26+
map.put(word.charAt(i), i);
27+
//最大长度
28+
max = Math.max(max, i-left+1);
29+
30+
}
31+
return max;
32+
33+
34+
}
35+
36+
}
37+

0 commit comments

Comments
 (0)