LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求:
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.
题目分析
这是一道基础的字符串处理的题,题目要求找出一个给定的字符串的最长不重复子串。
思路
首先这题用简单的两层循环肯定是不行的,会超时,所以要尽可能的减少重复的操作。
步骤如下:
1.首先定义两个指针变量,用来控制输入串的子串的头和尾
2.设置有一个vector存储读入的子串
3.头部指针不动,尾部指针向后移动,如果尾指针指向的字符没有出现在vector容器中,则加入,否则找到在vector中的位置。由于遍历时顺序的遍历,头指针直接指到与尾指针重复的字符的后一个,这一步操作非常重要,关系到代码的效率,如果重复直接头指针后移一个,会有重复操作导致超时。
4.重复3的步骤即可直到末尾,记得注意更新max长度,需要加入就更新,因为跳出循环不一定是重新重复步骤也可以是到字符串末尾。
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<char> cv;
int length = s.size();
const char * str = s.c_str();
const char * p1 = str;
const char * p2 = str;
int max_length = 0;
int flag = 0;
while(*p2 !='\0')
{
while(find(cv.begin(),cv.end(),*p2) == cv.end())
{
cv.push_back(*p2);
int temp_length = cv.size();
if (temp_length > max_length)
{
max_length = temp_length;
}
p2++;
if(*p2 == '\0')
{
return max_length;
}
}
if(flag == 0)
{
cv.erase(cv.begin(),find(cv.begin(),cv.end(),*p2)+1);
cv.push_back(*p2);
}
int temp_length = cv.size();
if (temp_length > max_length)
{
max_length = temp_length;
}
p2++;
}
return max_length;
}
};
int main()
{
Solution s1;
string inputString = "lwcjjuasgydqamj";
cout<<"result"<<s1.lengthOfLongestSubstring(inputString)<<endl;
}
LeetCode 3 Longest Substring Without Repeating Characters 解题报告的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Leetcode:Longest Substring Without Repeating Characters 解题报告
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- SqlServer定时备份数据库和定时杀死数据库死锁解决
上周五组长对我说了一句要杀死数据库的死锁进程,有时候同一时刻不停写入数据库会造成这种情况的发生,因为自己对数据库不是很熟悉,突然组长说了我也就决定一定要倒腾一下,不然自己怎么提高呢?现在不研究,说不定 ...
- win32程序启用控制台
#include <cstdio> #define USE_WIN32_CONSOLE int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _I ...
- 【BZOJ 4551】【TJOI2016】【HEOI2016】树
http://www.lydsy.com/JudgeOnline/problem.php?id=4551 题目描述 给定一棵有根树(根为 1),有以下两种操作:1. 标记操作:对某个结点打上标记(在最 ...
- Treap树的基础知识
原文 其它较好的的介绍:堆排序 AVL树 树堆,在数据结构中也称Treap(事实上在国内OI界常称为Traep,与之同理的还有"Tarjan神犇发明的"Spaly),是指有一个随 ...
- [PHP] 自动加载的实现
基于psr的规范,使用命名空间和spl_autoload_register()来实现自动加载 文件结构: |--Api |--Account.php |--User.php|--Service |-- ...
- js 倒计时 跳转
1. setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. setTimeout() 只执行 code 一次.如果要多次调用,请使用 setInterval() 或者让 code ...
- css权重是什么
css权重是什么? 概述 css Specificity中文一般译为css优先级.css权重.相比"权重","优先级"更好理解,mozilla官方中文文档就翻译 ...
- The system clock has been set back more than 24 hours
由于破解调试需要,更改了系统时间,打开ArcMap会出现"The system clock has been set back more than 24 hours"的错误,原因是 ...
- 关于Fragment 不响应onActivityResult的情况分析 (
大家都知道,可以通过使用 startActivityForResult() 和 onActivityResult() 方法来传递或接收参数. 但你是否遭遇过onActivityResult()不执行或 ...
- Python基础(11)--面向对象1
面向对象设计与面向对象编程的关系 面向对象设计(OOD)不会特别要求面向对象编程语言.事实上,OOD 可以由纯结构化语言来实现,比如 C,但如果想要构造具备对象性质和特点的数据类型,就需要在程序上作更 ...