forked from rost0413/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLongest_Substring_Without_Repeating_Characters.cpp
More file actions
54 lines (50 loc) · 1.22 KB
/
Longest_Substring_Without_Repeating_Characters.cpp
File metadata and controls
54 lines (50 loc) · 1.22 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
/*
Author: Weixian Zhou, ideazwx@gmail.com
Date: Jul 3, 2012
Problem: Longest Substring Without Repeating Characters
Difficulty: easy
Source: http://www.leetcode.com/onlinejudge
Notes:
Given a string, find the length of the longest substring without repeating
characters. For example, the longest substring without repeating letters for
"abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring
is "b", with the length of 1.
Solution:
Implement a queue.
*/
#include <vector>
#include <set>
#include <climits>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
queue<int> *que = new queue<int>;
bool map[26];
unsigned maxlen = 0;
for (int i = 0; i < 26; i++) {
map[i] = 0;
}
for (unsigned i = 0; i < s.length(); i++) {
if (map[s[i] - 'a']) {
while (que->front() != s[i] - 'a') {
map[que->front()] = false;
que->pop();
}
que->pop();
} else {
map[s[i] - 'a'] = true;
}
que->push(s[i] - 'a');
maxlen = (que->size() > maxlen ? que->size() : maxlen);
}
delete que;
return maxlen;
}
};