-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsolution003.cpp
More file actions
57 lines (52 loc) · 1.05 KB
/
solution003.cpp
File metadata and controls
57 lines (52 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* @file Leetcode: Longest Substring Without Repeating Characters
*
* cpselvis ([email protected])
* 2016.7.4
*/
#include<cstdio>
#include<string>
using namespace std;
// Use hash map.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int hash[256];
int maxLen = 0, distance, i, j;
if (s.length() == 1)
{
return 1;
}
for (i = 0; i < s.length(); i ++)
{
memset(hash, 0, sizeof(hash));
hash[s[i]] = 1;
for (j = i + 1; j < s.length(); j ++)
{
if (hash[s[j]] == 0)
{
hash[s[j]] = 1;
}
else
{
distance = j - i;
maxLen = distance > maxLen ? distance : maxLen;
break;
}
}
if (j == s.length() && j - i > maxLen)
{
maxLen = j - i;
}
}
return maxLen;
}
};
int main(int argc, char **argv)
{
Solution s;
printf("%d\n", s.lengthOfLongestSubstring("abcabcbb"));
printf("%d\n", s.lengthOfLongestSubstring("bbbbb"));
printf("%d\n", s.lengthOfLongestSubstring("pwwkew"));
printf("%d\n", s.lengthOfLongestSubstring("bwf"));
}