3_Longest Substring Without Repeating Characters -- LeetCode
C++解法一:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int m[] = {},left = , res = ;
for(int index = ; index < s.size(); ++index)
{
if(m[s[index]] == || m[s[index]] < left)
{
res = max(res,index-left+);
}else{
left = m[s[index]];
}
m[s[index]] = index+;
}
return res;
}
};
算法解读:
1) 这里使用哈希的思想,把256个可能的字符都考虑在内,字符的ASCⅡ编码作为数组下标进行映射。如m[a]等价于m[97]。
2) res其实是result的缩写,表示返回的最大无重复子串的长度,left表示当前正在判断的子串的起始位置。
3) 进行一个for循环,当满足(m[s[i]] == 0 || m[s[i]] < left)条件时候,计算子串长度,并且与res比较最长并取之。
这里的m[s[i]]表示第i个字符上一次出现的位置,如果从来没有出现过即m[s[i]]==0, 则表示无重复;如果m[s[i]] < left
则表示上次出现的位置在当前判断的字串起始位置的左边,不重复。
否则,(m[s[i]] != 0 && m[s[i]] >= left),重复。此时要更新left的值为m[s[i]],即不left更新为当前字符与上一次重复
的字符位置的下一位 。这里的i+1 是因为i是从0开始的下标,而我们需要的是从1开始的序号。
4) i偏移都每一个字符都要几下当前的位置,即m[s[i]] = i + 1 。
C++解法二
复制代码
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> m(, -);
int res = , left = -;
for (int i = ; i < s.size(); ++i) {
left = max(left, m[s[i]]);
m[s[i]] = i;
res = max(res, i - left);
}
return res;
}
};
算法分析:
1)与算法一思想一样,只是进行了精简。
2)每次循环,将left更新 max(left, m[s[i]]),如果s[i]在left的右边且
3_Longest Substring Without Repeating Characters -- LeetCode的更多相关文章
- LeetCode 3_Longest Substring Without Repeating Characters
LeetCode 3_Longest Substring Without Repeating Characters 题目描写叙述: Given a string, find the length of ...
- 3-Longest Substring Without Repeating Characters @LeetCode
3-Longest Substring Without Repeating Characters @LeetCode 题目 题目中得到的信息有: 一段字符串找出不重复子串的最大长度,只需要长度信息. ...
- LeetCode: 3_Longest Substring Without Repeating Characters | 求没有重复字符的最长子串的长度 | Medium
题目: Given a . For . 解题思路: 这个题让找一个字符串中具有不重复单词的最长子串的长度,如:ababc,子串为abc,长度为3.有这么几个方法: 方法一: 依赖字符串本身的一些特有函 ...
- Longest Substring Without Repeating Characters leetcode java
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters -- LeetCode
原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
随机推荐
- Part 3:视图和模板--Django从入门到精通系列教程
该系列教程系个人原创,并完整发布在个人官网刘江的博客和教程 所有转载本文者,需在顶部显著位置注明原作者及www.liujiangblog.com官网地址. Python及Django学习QQ群:453 ...
- bootstrap select2 使用简单介绍
1. 基本属性配置: $("#select2-id").select2({ templateResult : formatState, // 列表带图片 templateSelec ...
- Golang时间格式化
PHP中格式化时间很方便,只需要一个函数就搞定: date("Y-m-d H:i:s") 而在Golang中,用的是"2006-01-02 15:04:05"这 ...
- xBIM 使用Linq 来优化查询
目录 xBIM 应用与学习 (一) xBIM 应用与学习 (二) xBIM 基本的模型操作 xBIM 日志操作 XBIM 3D 墙壁案例 xBIM 格式之间转换 xBIM 使用Linq 来优化查询 x ...
- dubbo中Listener的实现
这里继续dubbo的源码旅程,在过程中学习它的设计和技巧,看优秀的代码,我想对我们日程编码必然有帮助的.而那些开源的代码正是千锤百炼的东西,希望和各位共勉. 拿ProtocolListenerWrap ...
- 【转】TCP/IP和SOCKET的区别
要写网络程序就必须用Socket,这是程序员都知道的.而且,面试的时候,我们也会问对方会不会Socket编程?一般来说,很多人都会说,Socket编程基本就是listen,accept以及send,w ...
- 使用xUnit为.net core程序进行单元测试(4)
第1部分: http://www.cnblogs.com/cgzl/p/8283610.html 第2部分: http://www.cnblogs.com/cgzl/p/8287588.html 第3 ...
- 运行所选代码生成器时出错:“预期具有协定名称 "NuGet.VisualStudio.IVsPackageInstallerServices" 的1导出 ——VS2015错误记录
在编写ASP.NET MVC控制器后,右键添加视图时,VS2015报出错误: 运行所选代码生成器时出错:“预期具有协定名称 "NuGet.VisualStudio.IVsPackageIns ...
- Django搭建博客网站(二)
Django搭建自己的博客网站(二) 这里主要讲构建系统数据库Model. Django搭建博客网站(一) model 目前就只提供一个文章model和一个文章分类标签model,在post/mode ...
- 关于js 全选 反选
prop 对于HTML元素本身就带有的固有属性,在处理时,使用prop方法. attr 对于HTML元素我们自己自定义的DOM属性,在处理时,使用attr方法. $("#selectAll ...