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 ...
随机推荐
- 机器学习实战 - 读书笔记(11) - 使用Apriori算法进行关联分析
前言 最近在看Peter Harrington写的"机器学习实战",这是我的学习心得,这次是第11章 - 使用Apriori算法进行关联分析. 基本概念 关联分析(associat ...
- Web Service(一) 基础学习
1 基础的Web Service平台是XML+HTTP. 2 Web Service平台的元素包括:SOAP(Simple Object Access Protocol)简单对象访问协议: UDDI( ...
- C++之多态的一个例子
[例12.1] 先建立一个Point(点)类,包含数据成员x,y(坐标点).以它为基类,派生出一个Circle(圆)类,增加数据成员r(半径),再以Circle类为直接基类,派生出一个Cylinder ...
- arcgis安装msi安装包提示"在未标记为正在运行时,调用了RunScript”解决办法
安装msi安装包提示"在未标记为正在运行时,调用了RunScript”解决办法 windows/temp目录相关权限不对,右击temp文件夹,选择管理员获取所有权限.
- Mybatis学习记录(三)----理解SqlMapConfig.xml文件
SqlMapConfig.xml mybatis的全局配置文件SqlMapConfig.xml,配置内容如下: properties(属性) settings(全局配置参数) typeAliases( ...
- 在SharePoint中无代码开发InfoPath应用: 一个测试Web Service的工具
这是这个系列的第一篇,介绍一个小工具,主要是用在Web Service测试的. 因为为了用一点高级的东西,就免不了和web service打交道. 你可以使用按照KB819267来修改web.conf ...
- 转:HTTP 1.1与HTTP 1.0的比较
原文地址:http://blog.csdn.net/elifefly/article/details/3964766 HTTP 1.1与HTTP 1.0的比较 一个WEB站点每天可能要接收到上百万的用 ...
- 2015年第6本(英文第5本):Harry Potter 1 哈利波特与魔法石
书名: Harry Potter 1 – Harry Potter and the Sorcerer’s Stone 作者:J.K. Rowling 单词数:7.8万 不重复单词数:6000(我怎么感 ...
- centos如何安装软件
背景 之前用的linux操作系统移植都是ubuntu,没有用过redhat版本的linux,最近开始想学习redhan版本的linux,就从centos开始.在安装完centos以后,第一个碰到的问题 ...
- 上传Android代码到Jcenter(解决了字符映射的问题)
请先阅读:http://blog.saymagic.cn/2015/02/16/release-library-to-jcenter.html 最外面的build.gradle // Top-leve ...