Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下:
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解析的更多相关文章
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- mysql export mysqldump version mismatch upgrade or downgrade your local MySQL client programs
I use MySQL Community Edition and I solved this problem today. goto https://dev.mysql.com/downloads/ ...
- Raneto中文搜索支持
背景 因业务部门需要在线软件使用说明文档,但我们资源不足,故我想找一个开源的知识库,发现 Raneto不错,决定使用. 官方文档相当清晰,部署完成,发布一些文章,启动项目,交由业务同事测试使用,于是我 ...
- hadoop wordcout测试
hadoop wordcout测试 安装好hadoop 环境后,启动HDFS等服务:输密码 1004 start-all.sh 查看启动情况 1006 jps 1007 cd ~ 切换到用户默认目录 ...
- kerberos环境下spark消费kafka写入到Hbase
一.准备环境: 创建Kafka Topic和HBase表 1. 在kerberos环境下创建Kafka Topic 1.1 因为kafka默认使用的协议为PLAINTEXT,在kerberos环境下需 ...
- 关键字-this
1.当成员变量和局部变量重名时,在方法中使用this时,this指向的是该方法所在类的成员变量. package gc.test.java.cs1; public class User{ public ...
- Vue组件之全局组件与局部组件
1全局注册实例 <div id="app"> <com-btn></com-btn> <com-btn></com-btn&g ...
- 安装Linux虚拟系统
VMWare创建虚拟机与Linux系统的安装 准备工作:VMWare虚拟机,Linux系统镜像 创建好虚拟机之后就可以进入Bios(Basic input ouput system)界面设置安装引导顺 ...
- FineUIPro/Mvc/Core v5.4.0即将发布(Core基础版,新功能列表)!
FineUIPro/Mvc/Core v5.4.0 即将于 2019-03-04 发布,目前官网示例已更新,先睹为快:http://pro.fineui.com/http://mvc.fineui.c ...
- 挥舞的手臂(mixly+二次开发)
从vb6到vb.net,一路c#, java, python, nn, c,对技术的切换早已经没有害怕的感觉了,一直有的是技术的热情和我所认为的技术信仰. 扯完,开始正文. 看看效果图: 使用的是Ar ...
- 前序遍历构造已知二叉树(Java)
public BiNode createBiTree() { Scanner input = new Scanner(System.in); int k = input.nextInt(); if(k ...