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 ...
随机推荐
- SpreadJS 中应用 KnockoutJS 技术
SpreadJS 支持 Knockout (KO)技术, KnockoutJS 是一个使用 MVVM 模式的 JavaScript 库,允许双向数据绑定,使数据和UI界面进行实时的交互更新.关于KO的 ...
- java之StringBuilder类详解
StringBuilder 非线程安全的可变字符序列 .该类被设计用作StringBuffer的一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍).如果可能,建议优先采用该类,因为在 ...
- unity3d常用属性汇总
unity常用的是C#语言.而C#语言有Attribute属性.特别强大.所以unity开发的时候.可以在变量加Attribute属性来达到开发人员想要的效果 RequireComponent:约束组 ...
- fibonacci数列从a到b的个数
Description 我们定义斐波那契数列如下: f1=1 f2=2 f(n)=f(n-1)+f(n-2)(n>=3) 现在,给定两个数a和b,计算有多少个斐波那契数列中的数在a和b之间( ...
- NYOJ:题目860 又见01背包
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=860 方法一:不用滚动数组(方法二为用滚动数组,为方法一的简化) 动态规划分析:最少要拿总 ...
- [python拾遗]列表
python列表拾遗 1.列表可以修改,使用 ‘+’ 将一个新列表附加在原列表的尾部: >>> a = [1,'a'] >>> b = a + [2,'b'] &g ...
- [vim] vim入门
1. 概述 工欲善其事 必先利其器.vim是非常好用的文本编辑器,可以将它看作是vi的进阶.绝大多数Unix系统都会内置vi编辑器,vi是文本编辑器,vim是程序编辑器.相比vi,它可以根据文件的类型 ...
- android onNewIntent
在Android应用程序开发的时候,从一个Activity启动另一个Activity并传递一些数据到新的Activity上非常简单,但是当您需要让后台运行的Activity回到前台并传递一些数据可能就 ...
- Java中Scanner类和BufferReader类之间的区别
java.util.Scanner类是一个简单的文本扫描类,它可以解析基本数据类型和字符串.它本质上是使用正则表达式去读取不同的数据类型. Java.io.BufferedReader类为了能够高效的 ...
- 帝吧出征FB:这李毅吧的“爆吧”文化是如何形成的
声明:本文不对爆吧行为及其涉及的事件进行是非判断,只探讨帝吧文化本身,欢迎拍砖.更正和补充. 一.“帝吧FB出征”事件梳理 继上次全网集体骂 “薯片”事件后,昨日(1月20日)晚7点,又发生了一次互联 ...