Having fun on Leetcode with C++, Java, Python & GO
Problem 3. Longest Substring Without Repeating Characters
My attempt(s)
Code
C++
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
|
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s=="")
return 0;
int head=0;
int LongestSize=1;
int CurrentSize=1;
bool breaked=0;
for (int i=0;i<s.length();i++)
{
breaked=0;
CurrentSize=1;
for (int j=head;j<i;j++)
{
if (s[j]==s[i])
{
head=j+1;
breaked=1;
}
if ((s[j]!=s[i])&&(breaked==0))
CurrentSize=CurrentSize+1;
}
if (CurrentSize>LongestSize)
LongestSize=CurrentSize;
}
return LongestSize;
}
};
|
C++
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
|
class Solution {
public:
int lengthOfLongestSubstring(string s) {
if (s=="")
return 0;
int head=0;
int LongestSize=1;
int CurrentSize=1;
bool breaked=0;
for (int i=0;i<s.length();i++)
{
breaked=0;
CurrentSize=1;
for (int j=head;j<i;j++)
{
if (s[j]==s[i])
{
head=j+1;
breaked=1;
break;
}
if ((s[j]!=s[i])&&(breaked==0))
CurrentSize=CurrentSize+1;
}
if (CurrentSize>LongestSize)
LongestSize=CurrentSize;
}
return LongestSize;
}
};
|
Explanation of idea
Good solution ref.
Code
Analysis