-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3.无重复字符的最长子串_wrong.cpp
61 lines (57 loc) · 1.39 KB
/
3.无重复字符的最长子串_wrong.cpp
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
58
59
60
61
/*
* @lc app=leetcode.cn id=3 lang=cpp
*
* [3] 无重复字符的最长子串
*/
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
// @lc code=start
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
vector<int> repeatedTime;
for (int k = 0; k < s.size(); k++)
{
char ltr = s[k];
int i;
for (i = k + 1; i < s.size(); i++)
{
if (s[i] == ltr)
{
break;
}
}
int index1 = k, index2;
if (i == s.size() && s[i] != ltr)
{
index2 = k;
}
else
{
index2 = i;
}
cout << index1 << endl;
cout << index2 << endl;
int len = index2 - index1;
if (len == 0)
{
len = s.size() - index1;
}
cout << "the letter " << ltr << " has" << len << endl;
if (len != s.size())
{
repeatedTime.push_back(len);
}
}
int maxLen = *max_element(repeatedTime.begin(), repeatedTime.end());
cout << maxLen << endl;
return maxLen;
}
};
// @lc code=end