题目如下:

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. WEB框架-Django框架学习(二)- 模型层

    今日份整理为模型层 1.ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库, ...

  2. 史上最全的Spring-Boot-Starter开发手册

    1.前面的话 我们都知道可以使用 SpringBoot 快速的开发基于 Spring 框架的项目.由于围绕 SpringBoot 存在很多开箱即用的 Starter 依赖,使得我们在开发业务代码时能够 ...

  3. git错集

    2018年12月20日22:26:01 fatal:not a git repository ( or any of the parent directories ) : .git 这个错误出现在首次 ...

  4. 手动安装Package Control

    手动下载一个package control的包:https://github.com/wbond/package_control 然后Download ZIP后,解压,将解压后的文件夹重命名为 Pac ...

  5. pytorch识别CIFAR10:训练ResNet-34(微调网络,准确率提升到85%)

    版权声明:本文为博主原创文章,欢迎转载,并请注明出处.联系方式:460356155@qq.com 在前一篇中的ResNet-34残差网络,经过训练准确率只达到80%. 这里对网络做点小修改,在最开始的 ...

  6. (七)jdk8学习心得之join方法

    七.join方法 1. 作用:将list或者数组按照连接符进行连接,返回一个字符串. 2. 使用方法 1) String.join(“连接符”,数组对象或者list对象) 2) 首先转换成stream ...

  7. codeforces 792A-D

    先刷前四题,剩下的有空补. 792A New Bus Route 题意:给出x 轴上的n 个点,问两个点之间的最短距离是多少,有多少个最短距离. 思路:排序后遍历. 代码: #include<s ...

  8. 安装vue-cli

    1.路径 https://nodejs.org/en/ cmd 创建项目 1.创建一个基于 webpack 模板的新项目 vue init webpack projectname(项目名) 2.项目名 ...

  9. 源码分析 ucosii/source 任务源码详细分析

    分析源码: 得先学会读文档, 函数前边的 note :是了解该程序员的思想的途径.不得不重视 代码前边的  Notes,了解思想后,然后在分析代码时看他是如何具体实现的. 1. ucosii/sour ...

  10. Gradle+IDEA使用说明

    Gradle+IDEA使用说明 导语: IDEA拥有大量的JAVA开发者拥护,相比于开源的eclipse,IDEA拥有更简洁直观的界面,拥有更强大的自动补全功能,号称能“一路敲回车完成编码”.如果把I ...