题目:

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. Java 之 File类(文件操作)

    一.概述 java.io.File 类是文件和目录路径名册抽象表示,主要用于文件和目录的创建.查找和删除等操作. File类是一个与系统无关的类,任何的操作系统都可以使用这个类中的方法. 路径问题: ...

  2. C# - 配置动态更新

    生产中经常会遇到修改配置的情况,但是又需要重启应用程序,是不是有点小烦躁.... 下面了解下在不重启情况下,实现配置更新实时生效 public static void SetConfig(string ...

  3. springboot系列(八)springboot整合mybatis

    本篇介绍一下在springboot中整合mybatis ,使用mysql数据库,集成durid 连接池技术,全部代码是手动生成,没有使用代码生成器来构建代码. 一.创建数据库和表 二.在pom中添加依 ...

  4. Python requests.post嵌套多层json参数调用接口

    #coding:utf-8 import requests,json #第一行注解的#coding:utf-8表示可以支持中文,不然代码里面有中文会报错 url = "http://xxx& ...

  5. LVS实现健康性检查功能

    LVS高可用性 Director不可用,整个系统将不可用:SPoF Single Point of Failure 解决方案:高可用 keepalived heartbeat/corosync 某RS ...

  6. 个性化召回算法实践(三)——PersonalRank算法

    将用户行为表示为二分图模型.假设给用户\(u\)进行个性化推荐,要计算所有节点相对于用户\(u\)的相关度,则PersonalRank从用户\(u\)对应的节点开始游走,每到一个节点都以\(1-d\) ...

  7. [转]对于BIO/NIO/AIO,你还只停留在烧开水的水平吗

    原文:https://www.javazhiyin.com/40106.html https://coding.imooc.com/class/381.html ------------------- ...

  8. 配置ubuntu18.04

    关于Ubuntu18.04的配置: 首先在装好系统后需要配置VMware Tools,这个会在虚拟机退出全屏的时候给一个帮助链接,在帮助链接中有详细的教程. 教程链接:https://docs.vmw ...

  9. 大数据之路week06--day03(jdk8新特性 Lambda表达式)

    为什么使用Lambda表达式?(做为初学者接触这个新的语法,会很懵逼,说道理,我在接触到这一块的时候,语法规则我看到了也很懵逼,因为这个和逻辑的关系不是很大,但就是作为一种新的语法出现,一时间很难接受 ...

  10. vulkan load store and memoryless

    https://www.jendrikillner.com/article_database/ https://community.arm.com/developer/tools-software/g ...