-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode3.cpp
More file actions
35 lines (30 loc) · 859 Bytes
/
LeetCode3.cpp
File metadata and controls
35 lines (30 loc) · 859 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
//
// LeetCode3.cpp
// AlgorithmLibrary
//
// Created by liuchunxi on 2019/5/24.
// Copyright © 2019年 imera. All rights reserved.
// 题目:
// 给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。
// 示例 1:
// 输入: "abcabcbb"
// 输出: 3
// 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
#include "LeetCode3.hpp"
#include <unordered_set>
int lengthOfLongestSubstring(string s) {
if(s.size() == 0) {
return 0;
}
unordered_set<char> lookup;
int maxLength = 0;
int left = 0;
for (int i=0; i<s.size(); i++) {
while (lookup.find(s[i]) != lookup.end()) {
lookup.erase(s[left++]);
}
maxLength = max(maxLength, i-left+1);
lookup.insert(s[i]);
}
return maxLength;
}