Skip to content

Commit ba22abf

Browse files
committed
add 48
1 parent f40aa51 commit ba22abf

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
3+
剑指Offer
4+
面试题48:最长不含重复字符的子字符串
5+
6+
题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子字符串的长度。
7+
假设字符串中只包含'a' ~ 'z'的字符。例如,在字符串"arabcacfr"中,最长的不含重复字符的子字符串是"acfr",
8+
长度为4。
9+
10+
*/
11+
12+
#include <iostream>
13+
#include <vector>
14+
#include <algorithm>
15+
16+
using namespace std;
17+
18+
class Solution {
19+
public:
20+
int longestSubstringWithoutDuplication(string s) {
21+
if(s.empty())
22+
return 0;
23+
vector<int> hash(26, -1);
24+
int start = 0;
25+
int res = 0;
26+
for(int i = 0; i < s.size(); i++){
27+
if(hash[s[i] - 'a'] >= start)
28+
start = hash[s[i] - 'a'] + 1;
29+
hash[s[i] - 'a'] = i;
30+
res = max(res, i - start + 1);
31+
}
32+
return res;
33+
}
34+
};
35+
36+
int main()
37+
{
38+
Solution solution;
39+
cout << solution.longestSubstringWithoutDuplication("arabcacfr") << endl;
40+
return 0;
41+
}

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,5 @@
5151
|44|[数字序列中某一位的数字](https://www.acwing.com/problem/content/52/)|[C++](https://github.com/htdwade/CodingInterviews/blob/master/44.DigitsInSequence/DigitsInSequence.cpp)|
5252
|45|[把数组排成最小的数](https://www.nowcoder.com/practice/8fecd3f8ba334add803bf2a06af1b993?tpId=13&tqId=11185&tPage=2&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)|[C++](https://github.com/htdwade/CodingInterviews/blob/master/45.SortArrayForMinNumber/SortArrayForMinNumber.cpp)|
5353
|46|[把数字翻译成字符串](https://www.acwing.com/problem/content/55/)|[C++](https://github.com/htdwade/CodingInterviews/blob/master/46.TranslateNumbersToStrings/TranslateNumbersToStrings.cpp)|
54-
|47|[礼物的最大价值](https://www.acwing.com/problem/content/56/)|[C++](https://github.com/htdwade/CodingInterviews/blob/master/47.MaxValueOfGifts/MaxValueOfGifts.cpp)|
54+
|47|[礼物的最大价值](https://www.acwing.com/problem/content/56/)|[C++](https://github.com/htdwade/CodingInterviews/blob/master/47.MaxValueOfGifts/MaxValueOfGifts.cpp)|
55+
|48|[最长不含重复字符的子字符串](https://www.acwing.com/problem/content/57/)|[C++](https://github.com/htdwade/CodingInterviews/blob/master/48.LongestSubstringWithoutDup/LongestSubstringWithoutDup.cpp)|

0 commit comments

Comments
 (0)