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 ...
随机推荐
- Error generating Swagger server (Python Flask) from Swagger editor
1down votefavorite http://stackoverflow.com/questions/36416679/error-generating-swagger-server-pyt ...
- 创建WCF服务寄宿到IIS
一.WCF简介: Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台. 整合了原有的win ...
- C语言范例学习03-中
栈和队列 这两者都是重要的数据结构,都是线性结构.它们在日后的软件开发中有着重大作用.后面会有实例讲解. 两者区别和联系,其实总结起来就一句.栈,后进先出:队列,先进先出. 可以将栈与队列的存储空间比 ...
- Natural language style method declaration and usages in programming languages
More descriptive way to declare and use a method in programming languages At present, in most progra ...
- 不是语言之争--Go vs Erlang
因为 云巴 系统对高并发.低延迟的需求,我们对各个语言.平台做了很多的调研比较工作.这自然就包括致力于开发高并发应用的 Go 和 Erlang. 并发 Go 对高并发的支持通过 goroutine 实 ...
- JavaScript基础13——js的string对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 关于一个js连续赋值问题之我见(词略穷,见谅)
前几天在搜索面试题时发现了这么一段代码,执行完后感觉完全不与所想的一样 var a = { n : 1 }; var b = a; a.x = a = {n : 2}; console.log(a.x ...
- Sharepoint学习笔记—习题系列--70-573习题解析 -(Q22-Q24)
Question22You create a Feature.You need to remove the link to the Site Content Type page from the Si ...
- C编程常见问题总结
本文是C编程中一些常见错误的总结,有些是显而易见的,有些则是不容易发现 本文地址:http://www.cnblogs.com/archimedes/p/get-screwed-c.html,转载请注 ...
- 【原】自定义UINavigationItem的两种方法以及相应的隐藏方法
第一种: UIImage *searchimage=[UIImage imageNamed:@"search.png"]; UIBarButtonItem *barbtn=[[[U ...