File tree Expand file tree Collapse file tree 4 files changed +87
-0
lines changed
0003-Longest-Substring-Without-Repeating-Characters Expand file tree Collapse file tree 4 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
1
+ cmake_minimum_required (VERSION 3.5 )
2
+ project (cpp_0003 )
3
+
4
+ set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11" )
5
+
6
+ set (SOURCE_FILES main.cpp )
7
+ add_executable (cpp_0003 ${SOURCE_FILES} )
Original file line number Diff line number Diff line change
1
+ // / Source : https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
2
+ // / Author : liuyubobobo
3
+ // / Time : 2017-07-12
4
+
5
+ #include < iostream>
6
+
7
+ using namespace std ;
8
+
9
+ // / Using Sliding Window
10
+ // / Time Complexity: O(len(s))
11
+ // / Space Complexity: O(len(charset))
12
+ class Solution {
13
+ public:
14
+ int lengthOfLongestSubstring (string s) {
15
+
16
+ int freq[256 ] = {0 };
17
+
18
+ int l = 0 , r = -1 ; // sliding window: s[l...r]
19
+ int res = 0 ;
20
+
21
+ while ( r + 1 < s.size () ){
22
+
23
+ if ( freq[s[r+1 ]] == 0 )
24
+ freq[s[++r]] ++;
25
+ else // freq[s[r+1]] == 1
26
+ freq[s[l++]] --;
27
+
28
+ res = max ( res , r-l+1 );
29
+ }
30
+
31
+ return res;
32
+ }
33
+ };
34
+
35
+ int main () {
36
+
37
+ cout << Solution ().lengthOfLongestSubstring ( " abcabcbb" )<<endl;
38
+ cout << Solution ().lengthOfLongestSubstring ( " bbbbb" )<<endl;
39
+ cout << Solution ().lengthOfLongestSubstring ( " pwwkew" )<<endl;
40
+ cout << Solution ().lengthOfLongestSubstring ( " " )<<endl;
41
+
42
+ return 0 ;
43
+ }
Original file line number Diff line number Diff line change
1
+ /// Source : https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
2
+ /// Author : liuyubobobo
3
+ /// Time : 2017-10-21
4
+
5
+ /// Using Sliding Window
6
+ /// Time Complexity: O(len(s))
7
+ /// Space Complexity: O(len(charset))
8
+ public class Solution {
9
+ public int lengthOfLongestSubstring (String s ) {
10
+
11
+ int [] freq = new int [256 ];
12
+
13
+ int l = 0 , r = -1 ; //sliding window: s[l...r]
14
+ int res = 0 ;
15
+
16
+ while ( r + 1 < s .length () ){
17
+
18
+ if ( r + 1 < s .length () && freq [s .charAt (r +1 )] == 0 )
19
+ freq [s .charAt (++r )] ++;
20
+ else //freq[s[r+1]] == 1
21
+ freq [s .charAt (l ++)] --;
22
+
23
+ res = Math .max ( res , r -l +1 );
24
+ }
25
+
26
+ return res ;
27
+ }
28
+
29
+ public static void main (String [] args ) {
30
+ System .out .println ((new Solution ()).lengthOfLongestSubstring ( "abcabcbb" ));
31
+ System .out .println ((new Solution ()).lengthOfLongestSubstring ( "bbbbb" ));
32
+ System .out .println ((new Solution ()).lengthOfLongestSubstring ( "pwwkew" ));
33
+ System .out .println ((new Solution ()).lengthOfLongestSubstring ( "" ));
34
+ }
35
+ }
Original file line number Diff line number Diff line change @@ -30,6 +30,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
30
30
31
31
| ID | Problem | C++ | Java |
32
32
| --- | --- | :---: | :---: |
33
+ | 003 | [ Longest-Substring-Without-Repeating-Characters] ( https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ ) | [ C++] ( 0003-Longest-Substring-Without-Repeating-Characters/cpp-0003/ ) | [ Java] ( Longest-Substring-Without-Repeating-Characters/java-0003/src/ ) |
34
+ | | | | |
33
35
| 079 | [ Word Search] ( https://leetcode.com/problems/word-search/description/ ) | [ C++] ( 0079-Word-Search/cpp-0079/ ) | [ Java] ( 0079-Word-Search/java-0079/src/ ) |
34
36
| | | | |
35
37
| 283 | [ Move Zeroes] ( https://leetcode.com/problems/move-zeroes/ ) | [ C++] ( 0283-Move-Zeroes/cpp-0283/ ) | [ Java] ( 0283-Move-Zeroes/java-0283/src/ ) |
You can’t perform that action at this time.
0 commit comments