File tree Expand file tree Collapse file tree 1 file changed +12
-16
lines changed
src/main/java/leetcode/medium Expand file tree Collapse file tree 1 file changed +12
-16
lines changed Original file line number Diff line number Diff line change 11package leetcode .medium ;
22
3- import java .util .ArrayList ;
4- import java .util .List ;
3+ import java .util .HashSet ;
4+ import java .util .Set ;
55
66/**
77 * Created by nikoo28 on 12/18/17 9:29 PM
@@ -11,27 +11,23 @@ class LongestSubstringWithoutRepeatingCharacters {
1111
1212 int lengthOfLongestSubstring (String s ) {
1313
14- if (s == null || s .length () == 0 )
15- return 0 ;
14+ Set <Character > charSet = new HashSet <>();
1615
17- int maxLen = 1 ;
16+ int maxLength = 0 ;
17+ int left = 0 ;
1818
19- List <Character > x = new ArrayList <>();
20- for (int i = 0 ; i < s .length (); i ++) {
19+ for (int right = 0 ; right < s .length (); right ++) {
2120
22- if (x .contains (s .charAt (i ))) {
23-
24- maxLen = Math .max (maxLen , x .size ());
25-
26- while (x .get (0 ) != s .charAt (i ))
27- x .remove (0 );
28- x .remove (0 );
21+ while (charSet .contains (s .charAt (right ))) {
22+ charSet .remove (s .charAt (left ));
23+ left ++;
2924 }
3025
31- x .add (s .charAt (i ));
26+ charSet .add (s .charAt (right ));
27+ maxLength = Math .max (maxLength , right - left + 1 );
3228 }
3329
34- return Math . max ( maxLen , x . size ()) ;
30+ return maxLength ;
3531 }
3632
3733}
You can’t perform that action at this time.
0 commit comments