File tree Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Expand file tree Collapse file tree 2 files changed +41
-0
lines changed Original file line number Diff line number Diff line change 5
5
6
6
public class ScannerDemo {
7
7
public static void main (String [] args ) {
8
+ scannerMore ();
9
+ }
10
+
11
+ private static void scannerMore () {
8
12
//输入输出,牛客网之类的平台会用到
9
13
Scanner scanner = new Scanner (System .in );
10
14
while (scanner .hasNext ()) {
Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments