-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03-LengthOfLongestSubString.h
More file actions
55 lines (47 loc) · 948 Bytes
/
03-LengthOfLongestSubString.h
File metadata and controls
55 lines (47 loc) · 948 Bytes
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
#pragma once
#include <iostream>
#include <set>
#include <string>
#include <queue>
#include <hash_map>
#include <unordered_set>
using namespace std;
class Solution
{
public:
int lengthOfLongestSubString(string s);
int lengthSubString(string s);
};
//»¬¶¯´°¿Ú 48ms 13.6MB
int Solution::lengthOfLongestSubString(string s)
{
int nLen = 0;
int nLenMax = 0;
unordered_set<char> listCh;
int nlengthStr = s.length();
for (int iLoop = 0; iLoop < nlengthStr; ++iLoop)
{
while (listCh.find(s[iLoop]) != listCh.end())
{
listCh.erase(s[nLen]);
nLen++;
}
nLenMax = max(nLenMax, iLoop - nLen + 1);
listCh.insert(s[iLoop]);
}
return nLenMax;
}
// 16ms 9MB//»¬¶¯´°¿Ú×Ô½¨hash_map
int Solution::lengthSubString(string s)
{
int ans = 0;
int hashmap[128] = { 0 };
char chTmp = 0;
for (int j=0, i=0;j<s.length();++j)
{
i = max(hashmap[s.at(j)], i);
ans = max(ans, j - i + 1);
hashmap[s.at(j)] = j + 1;
}
return ans;
}