题目:

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.

  意思就是:给出一个字符串,找出不包含重复字母的最长字串,输出字符串的长度。

  之前我一直是暴力破解,最近看到一个有意思的思路,记录下来。

  先上代码:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> dict(, -);//ASCCI hash
int maxLen = , start = -;
for(int i = ; i < s.size(); ++i)
{
if(dict[s[i]] > start)
{
start = dict[s[i]];
}
dict[s[i]] = i;
maxLen = max(maxLen, i-start);
}
return maxLen;
}
int max(int a, int b)
{
return a > b ? a : b;
}
};

  看到这么短的代码,我也被其优雅所折服,我们来看下思路:

  * 对字符串进行一次遍历,用一个大小为256(ascii最多有256种字符)数组记录每个字母最后一次的下标。
  * 如果当前字符在之前出现过就覆盖原来的数组元素,并且更新start值。
  * 每次循环时检查当前下标i-开始下标start和一个记录当前最大串长度的max变量的关系,将max保持或更新。
  * 需要注意的是,我更新start和max是在检测到重复字母时进行的,而最后一个字符不一定和前边重复,所以循环外要附加一次更新max的操作。

Leetcode第三题《Longest Substring Without Repeating Characters》的更多相关文章

  1. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  2. leetcode第三题Longest Substring Without Repeating Characters java

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

  3. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

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

  4. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  5. 刷题之路第三题--Longest Substring Without Repeating Characters

    问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...

  6. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  7. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  8. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  9. LeetCode(3)Longest Substring Without Repeating Characters

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

  10. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

随机推荐

  1. U盘因格式化 NTFS 中断造成无法识别,生产平台同样无法识别的修复处理方案

    特征: 电脑设备管理器(win10):识别到大容量存储设备 电脑磁盘管理:识别可移动磁盘无媒体 ChipGenius(v4_19_0319):能识别到制造商,但识别不到芯片具体型号 U盘相关生产平台: ...

  2. js调用正则表达式

    //验证是否为正整数 function isPositiveInteger(s) { var re = /^[0-9]+$/; return re.test(s); } if (exchangeCou ...

  3. SSM - SpringBoot - SpringCloud

    SSM框架 Spring + Spring MVC + MyBatis:标准MVC模式 继 SSH (Struts+Spring+Hibernate)之后,主流的 Java EE企业级 Web应用程序 ...

  4. 《构建之法》第五次作业——Alpha项目测试

    博客开头 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign?page=6 这个作业要求在 ...

  5. python 爬虫相关含Scrapy框架

    1.从酷狗网站爬取 新歌首发的新歌名字.播放时长.链接等 from bs4 import BeautifulSoup as BS import requests import re import js ...

  6. jquery基础知识2

    1.js和jquery对象的转换 js==>jquery对象 $(js对象) jquery==>js jq对象[index] jq对象.get(index) <!DOCTYPE ht ...

  7. uWSGI ,WSGI和uwsgi的区别

    1.1.为方便理解,uWSGI ,WSGI和uwsgi在网站项目流程图中的功能如下: 1.2.网站项目结构图 2.uWSGI ,WSGI和uwsgi的区别 2.1 WSGI: WSGI,全称 Web ...

  8. DateTime函数

    一.初始化: DateTime dt = , , ); DateTime dt1 = DateTime.Now; DateTime dt2 = DateTime.Today; DateTime dt3 ...

  9. mysql中一个字段升序,另一个字段降序

    mySql中,升序为asc,降序为desc.例如: 升序:select   *  from  表名 order by  表中的字段 asc(mysql中默认是升序排列,可不写) 降序:select   ...

  10. ADB命令使用详解

    ADB是一个 客户端-服务器端 程序, 其中客户端是你用来操作的电脑, 服务器端是android设备. 1.连接android设置 adb connect 设备名 例如: adb connect 12 ...