题目如下:

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. Zabbix3.4-RHEL 7.4 X64 YUM联网安装

    OS准备 关闭selinux vi /etc/selinux/config setenforce 0 开启防火墙80端口访问 firewall-cmd --permanent --add-rich-r ...

  2. commons-lang3之StringUtils

    字符串是一种在开发中经常使用到的数据类型,对字符串的处理也变得非常重要,字符串本身有一些方法,但都没有对null做处理,而且有时可能还需要做一些额外处理才能满足我们的需求,比如,要判断某个字符串中是否 ...

  3. mange

    from flask import Flask app = Flask(__name__) manager = Manager(app) 1. 重写Command class ShellCommand ...

  4. JSP页面、EL表达式

    JSP页面: jsp 是一种动态页面,html 页面和 jsp页面最大的区别是:html 是一种静态页面,在 html 中只 能定义 css.js 等,在 jsp 中除了可以定义 css.js 之外还 ...

  5. Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用(转)

    原文地址:https://www.cnblogs.com/fashflying/p/6908028.html 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对 ...

  6. 清除 x-code 缓存

    https://www.jianshu.com/p/5673d8333544 之前由于经费不足,购置的128的mac,现在发现一不注意盘就满了,悔之晚矣...a).清除 x-code CoreSimu ...

  7. MySQL——合并查询结果

    利用  UNION 关键字,可以给出多条  SELECT  语句,并将它们的结果组合成一个结果集.合并时,两个表对应的列数和数据类型必须相同.SELECT 语句之间使用  UNION  或  UNIO ...

  8. Git学习指北

    learnGitBranching:一个可视化学习 git 的网站 learngitbranching.js.org,虽然项目有些悠久,如果学习 git 的话可以来玩下

  9. html 通用导航 a链接跳转时给当前导航添加选中颜色

    学习前端的同学或许会遇到这个问题 做一个基本的小站有几个导航的,如下图 无论有几个页面,这里的导航的样式都是一样,唯一不同的就是进入哪个页面时当前有个选中的样式 一般这样通用的导航在开发的时候都会封装 ...

  10. Python 学习笔记 - 不断更新!

    Python 学习笔记 太久不写python,已经忘记以前学习的时候遇到了那些坑坑洼洼的地方了,开个帖子来记录一下,以供日后查阅. 摘要:一些报错:为啥Python没有自增 ++ 和自减 --: 0x ...