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 解题报告的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. 【LeetCode】Longest Substring Without Repeating Characters 解题报告

    [题意] Given a string, find the length of the longest substring without repeating characters. For exam ...

  3. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. Leetcode:Longest Substring Without Repeating Characters 解题报告

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  5. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  6. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  7. 【leetcode】Longest Substring Without Repeating Characters

    题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

  8. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. Java [leetcode 3] Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...

随机推荐

  1. sql多行转一行,以逗号隔开

    --SELECT ff= stuff((select ','+cast(WorkOrderNo as varchar)-- FROM dbo.TB_WorkOrder c -- where tpl.P ...

  2. question about import google file

    1. question descibe; once, one css file(app.css) of my web project has sentence like this: @import u ...

  3. 搭建spring的开发环境

    在eclipse中新建web项目,集成spring开发环境,把集成spring的过程描述如下, 1.从spring官网下载spring的jar包,我这里是spring4.1,下载的文件中包含了源码及文 ...

  4. Hibernate框架之注解的配置

    在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ...

  5. 【JavaEE】SSH+Spring Security+Spring oauth2整合及example

    现在加最后一样,就是oauth2,现在很多网站都有对应的移动版本,那么移动端访问服务端的服务怎么控制权限,我知道的主要是两种方法,第一是模拟浏览器,访问服务的时候会生成session,之后在移动端缓存 ...

  6. GitHub上我收藏Java及Android的项目Demo

    接触编程不久但浏览频率最高的还是GitHub毕竟它真的是程序员必不可少的新世界. (2016/9/23更新) 静态更新,很强势你值得拥有 更新资源不需要重新安装APK (2016.9.10更新) ht ...

  7. “连不上 ArcGIS License Manager ”的一点常用诊断方法

    在 ArcGIS Desktop 的问题库中,有一类不算做核心技术问题,但却可能会位列“最常见的问题”之一.简言之一句话,”许可服务器连不上怎么办?!“ 下面就来演绎下问题的诊断过程. 本文仅适用于客 ...

  8. SarePoint Powershell Add user to Group

    $FromGroupnames = "001总经理","010101管理本部" $ToGroupname = "test" $SPWeb = ...

  9. Oracle EBS在编码方式为AL32UTF8时的注意事项

    现如今的EBS系统中,为了推进国际化的进程,以及系统向全球化的扩展,在Oracle数据库的编码方式上渐渐从支持中国本土简体中文的ZHS16GBK转向了更趋于国际化的AL32UTF8编码方式.但随之而来 ...

  10. SQL 查询学生缺考情况

    有三个表,如果学生缺考,那么在成绩表中就不存在这个学生的这门课程成绩的记录,写一段SQL语句,检索出每个学生缺考的科目.A 学生表(student)字段1 学生号(s_id)字段2 学生名(s_nam ...