题目如下:

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

Example 1:

Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
https://leetcode.com/problems/longest-substring-without-repeating-characters/ 我学习的优秀代码:
int lengthOfLongestSubstring(string s) {
vector<int> dict(, -);
int maxLen = , start = -;
for (int i = ; i != s.length(); i++) {
if (dict[s[i]] > start)
start = dict[s[i]];
dict[s[i]] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}

来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/discuss/1737/C%2B%2B-code-in-9-lines.

个人加英文注释版:

class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> dict(,-);//256 is the amount of ASCII and its expanding. use -1 to initialize,dict stores each letter's index of their last position
int length=,start=-;//initialize
for(int i=;i<s.length();i++){// i is the index
if(dict[s[i]]>start)// it means whether this letter is already contained in the choosed string
start=dict[s[i]];// if true, the index of start should be reset to the index of the repeated word
dict[s[i]]=i;//update the index of their last position
length=max(length,i-start);//i and start both point at the same letter, so the real length is one letter less,i-start-1+1=i-start
}
return length;//return the largest length
}
};

解析如下{

相关知识点{

ASCII码与拓展ASCII码{

ASCII 码使用指定的7 位或8 位二进制数组合来表示128种字符。另外,后128个称为扩展ASCII码。许多基于x86的系统都支持使用扩展(或“高”)ASCII。扩展ASCII 码允许将每个字符的第8 位用于确定附加的128 个特殊符号字符、外来语字母和图形符号。所以共计256个};

C++ max与min函数的使用{

#include<algorithm>//引用头文件

min(a,b)或者max(a,b}会返回两个数中的较小数或者较大数,通常只用于两个数的大小比较};

};

内容解析{

这个算法最巧妙的地方是使用了哈希表,由于ASCII码的数量很小,只有256个,并且对应规则很明确(指的是字符和整数的对应规则),所以直接开了一个长度为256的数组做哈希表,用来保存这个字符上一次出现时的下标。由于哈希表的使用,查表的时间复杂度变成了O1级,使得速度的提升非常明显!这就是哈希表的力量hhhh

在if判断正确时,i和start指向的都是同样的字符,所选取的长度应该是要减掉一个的,但是由于尾减去头的结果会比字符个数少一。所以+1和-1相抵。

其他内容解析见这篇博文:https://www.cnblogs.com/ariel-dreamland/p/8668286.html}

};

Leetcode经典试题:Longest Substring Without Repeating Characters解析的更多相关文章

  1. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  2. 【LeetCode OJ】Longest Substring Without Repeating Characters

    题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...

  3. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  4. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

  5. 【LeetCode】3. Longest Substring Without Repeating Characters

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

  6. leetcode题解 3. Longest Substring Without Repeating Characters

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

  7. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

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

  8. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

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

随机推荐

  1. DIY手机锂电池万能充

    今天翻出来一个诺基亚的旧手机,试了一下,无法开机,应该了电池亏电了.可惜手头没有充电器,无法给手机充电. 活人岂能让尿憋死?回想了一下以前用过的手机万能充的样式(这里暴露年龄了) 根据家中现成的材料, ...

  2. ORM初探(一)

    Object Relational Mapping(ORM): 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象 ...

  3. Linux学习历程——Centos 7 账户管理命令(用户组篇)groupadd groupmod groupdel

    一.命令介绍 groupadd:创建用户组 groupmod:修改用户组属性 groupdel:删除用户组 ---------------------------------------------- ...

  4. ILRuntime官方Demo笔记

    调用/执行 热更中的方法 调用热更代码中方法,写在AppDomain中,记录一下主要几个方法: AppDomain.LoadAssembly 加载热更dll 执行热更代码的方法,有两种方式: appd ...

  5. 删除Widows 启动项中的信息

    1.打开任务管理器切换到启动Tab,在需要删除的项目上点击右键,点击打开文件所在位置,这样就找到了启动项所在磁盘位置,可以根据需要决定是否删除. 2.从注册表中删除在启动中的注册信息. regedit ...

  6. HTML基础-------HTML标签(1)

    HTML标签(1) h系列(容器级双标签) h系列标签分为六个等级(h1,h2,h3,h4,h5,h6) 语义:给文本添加一个标题 标题重要程度逐级递减,一个页面只能有一个h1级的标签,并且大多数时候 ...

  7. HashMap源码分析(一)

    前言:相信不管在生产过程中还是面试过程中,HashMap出现的几率都非常的大,因此有必要对其源码进行分析,但要注意的是jdk1.8对HashMap进行了大量的优化,因此笔者会根据不同版本对HashMa ...

  8. 数据结构学习之字符串匹配算法(BF||KMP)

    数据结构学习之字符串匹配算法(BF||KMP) 0x1 实验目的 ​ 通过实验深入了解字符串常用的匹配算法(BF暴力匹配.KMP.优化KMP算法)思想. 0x2 实验要求 ​ 编写出BF暴力匹配.KM ...

  9. OSI七层模型的每一层都有哪些协议

    TCP/IP: 数据链路层:ARP,RARP 网络层: IP,ICMP,IGMP 传输层:TCP ,UDP,UGP 应用层:Telnet,FTP,SMTP,SNMP. OSI: 物理层:EIA/TIA ...

  10. ##Django中Application labels aren't unique解决方法##

    pip更新了所有插件,发现了按平常编码遇到些问题,记录下. Django错误 django.core.exceptions.ImproperlyConfigured: Application labe ...