[Leetcode] Longest Substring Without Repeating Characters (C++)
题目:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
Tag:
String; Hash Table; Two Pointers
体会:
这道题对我来说挺难,看了很多答案,还是不太会。。。现在也只是知道了个大概意思。
大致的意思就是,首先我们要有一个Map, 其中<Key, Value>=<char, char在string s 中最后一次出现的位置>。然后开始逐个检查string中的char。
假设string s = "cxyzabdecb", 现在我们的substring是"abd",那么有两种情况我们可以直接增加当前substring的长度,一个是现在的char是第一次出现,(比如出现了e,那么就可以变成abde);另一个是虽然这个char在先前出现过,但是并没有在当前substring中出现过,比如abde后面的那个c, 判断条件是 map[s[i]] < i - curLen,意思就是前面那个c出现在abdec这个范围之外。综上,两种情况的共同特点都是char要在当前的substring中没有出现过。
除了上面两种情况,那么就是char在现在的substring中出现过了,那我们的substring就只能保留后来的这个部分了,即从上一次这个char后面的那一位到当前位置。例子:abdec现在往后检查遇到了b,为了能把b加进来,就只能保留dec,变成decb了。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// map<char, last index that char appears in the string
map<char, int> indexes;
// length of current substring
int curLen = ;
// length of max substring length so far
int maxLen = ;
int n = s.length();
for (int i = ; i < n; i++) {
char ch = s[i];
// if this char appears the first time or
// is not part of the current substring
if ((indexes.count(s[i]) == ) ||
(curLen < i - indexes[s[i]])) {
curLen++;
} else {
curLen = (i - indexes[s[i]]);
}
// update
indexes[s[i]] = i;
maxLen = (curLen > maxLen) ? curLen : maxLen;
}
return maxLen;
}
};
[Leetcode] Longest Substring Without Repeating Characters (C++)的更多相关文章
- [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] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森.键盘声音有点大,担心会吵到舍友.今年要当个可爱的技术宅呀~ 题目:Given a string, find the length of the longest substring w ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- 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 (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- weblogic启动问题
昨天测试环境上网银系统突然出现启动weblogic控制台出错问题,执行startWebLogic.sh脚本后tail到nohup文件时没有反应,nohup.out文件一直没有反应.对于此问题同事想re ...
- Gora官方范例
参考官方文档:http://gora.apache.org/current/tutorial.html 项目代码见:https://code.csdn.net/jediael_lu/mygoradem ...
- struts.custom.i18n.resources国际化详解(一)
每种框价都会有国际化的支持,struts2的国际化大致上分为页面的国际化,Action的国际化以及xml的国际化 首先在struts.properties文件中加入以下内容:struts.custom ...
- Dede修改文章默认标题长度,让标题全显示
第一步首先你要进入dedecms后台,系统——系统基本参数——其他选项——文档标题最大长度——在这修改为200或更大(其实200应该是足够了). 第二步进入phpmyadmin,点击dede_arch ...
- php的一些小笔记--时间函数
strtotime 返回UNIX时间戳 .这个函数可以用来计算前天,昨天,后天,明天 例如明天:date('Y-m-d H:is',strtotime('+1 day')) day>1是复数 ...
- AngularJS中的控制器示例_2
<!doctype html> <html ng-app="myApp"> <head> <script src="C:\\Us ...
- AOP技术基础
1.引言 2.AOP技术基础 3.Java平台AOP技术研究 4..Net平台AOP技术研究 2.1 AOP技术起源 AOP技术的诞生并不算晚,早在1990年开始,来自Xerox Palo Alto ...
- (转)fastcgi协议的简单实现
FastCgi不仅可以用于webserver与PHP的交互,也可用于任何两个应用之间的交互,PHPer用的比较多的应该就是用于两个子系统之间的交互. 比如A系统和B系统分部独立的部署在两台机器上,其之 ...
- jquery中table里面的tr里的input添加一行,并且第一列autoincrement
实现添加一行并且第一列由A0开始autoincrement,代码如下(在文件的同一个文件夹下添加一个jquery.js文件): <!DOCTYPE html PUBLIC "-//W3 ...
- (转)linux下fork的运行机制
转载http://www.cnblogs.com/leoo2sk/archive/2009/12/11/talk-about-fork-in-linux.html 给出如下C程序,在linux下使用g ...