LeetCode-3-Longest Substring Without Repeating Characters
来源:互联网 发布:mac etc目录 编辑:程序博客网 时间:2024/05/01 15:25
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb"
, the answer is "abc"
, which the length is 3.
Given "bbbbb"
, the answer is "b"
, with the length of 1.
Given "pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.
时间复杂度O(n^2)
#include <iostream>
#include <string>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
size_t found;
int max_len = s.empty() ? 0 : 1;
size_t prev = 0, next = 1;
int len = 1;
while (next < s.size()) {
found = find(s.at(next), s, prev, next);
if (found == string::npos) {
++len;
++next;
} else {
if (len > max_len) {
max_len = len;
}
prev = found + 1;
len = next - prev + 1;
++next;
}
}
// 防止最后一个窗口没有将较大的len赋值给max_len,同时考虑s为空的情形
if (len > max_len && !s.empty()) {
max_len = len;
}
return max_len;
}
private:
size_t find(char ch, string s, size_t start_pos, size_t end_pos) {
size_t pos = start_pos;
while (pos < end_pos) {
if (s.at(pos) == ch) {
return pos;
}
pos++;
}
return string::npos;
}
};
int main() {
string str1 = "abcabcbb";
string str2 = "bbbbb";
string str3 = "pwwkew";
string str4 = "au";
string str5 = "a";
string str6;
Solution s;
cout << s.lengthOfLongestSubstring(str1) << endl
<< s.lengthOfLongestSubstring(str2) << endl
<< s.lengthOfLongestSubstring(str3) << endl
<< s.lengthOfLongestSubstring(str4) << endl
<< s.lengthOfLongestSubstring(str5) << endl
<< s.lengthOfLongestSubstring(str6) << endl;
return 0;
}
- LeetCode 3 - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- LeetCode(3) Longest Substring Without Repeating Characters
- leetcode 3LONGEST SUBSTRING WITHOUT REPEATING CHARACTERS
- Leetcode【3】:Longest Substring Without Repeating Characters
- [leetcode 3] Longest Substring Without Repeating Characters
- [Leetcode] 3 - Longest Substring Without Repeating Characters
- LeetCode 3:《Longest Substring Without Repeating Characters》
- [leetcode 3] Longest Substring Without Repeating Characters
- [leetcode] 3 Longest Substring Without Repeating Characters
- [Leetcode]3Longest Substring Without Repeating Characters
- leetcode 3 Longest Substring Without Repeating Characters
- LeetCode #3 Longest Substring Without Repeating Characters
- leetcode-3 Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- leetcode 3: Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- stl--分析空间配置器及源码实现
- hdu 3669 Cross the Wall(给你n个矩形,长宽已知,求用不超过k个大矩形包含所有给定矩形,使得大矩形总面积和最小)
- URI、URL和URN的区别
- 数据库中乐观锁与悲观锁的概念
- opencv异常提示之 (channels() == CV_MAT_CN(dtype)) in copyTo (暂未解决,提供源码)
- LeetCode-3-Longest Substring Without Repeating Characters
- http与HTTPS
- 便利的开发工具 CppUnit 快速使用指南
- 学习PS涂抹工具
- 【BZOJ4582】【Usaco2016 open】Diamond Collector 贪心
- 使用Spring Cloud Config搭建配置中心
- 红黑树java实现
- 通过这次源码分析直播了解到在中国真正关心源码的webrtc开发者只有1%
- bfs 2016.5.2