[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, ...
随机推荐
- 流输入练习——寻找Sb.VI codevs 3096
题目描述 Description 已知某开放授权人员名叫Serb,由于经常修改各种数据,因此开发人员们都喊他SB.现在他和许多人一起过飞机安检,排成了一长队列,请问SB.是否在队列中. 输入描述 In ...
- Jlink仿真器下载程序时出现Invalid ROM table!
原因:仿真器时钟设置不对,应该将时间改低一点.
- [转载]Git常用命令
转载自: Git常用命令 Git配置 git config --global user.name "robbin" git config --global user.email & ...
- mysql和mysqli的区别
看书.看视频的时候一直没有搞懂mysqli和mysql到底有什么区别.于是今晚“谷歌”一番,整理一下.需要的朋友可以参考下. 一: PHP-MySQL 是 PHP 操作 MySQL 数据库最原始的 ...
- Erlang中的图形化检测工具(4)
这儿例举出若干个用于检视运行时系统的图形化工具,这些工具可以很好地帮助我们增进对系统的理解.借助这些工具,我们可以很好地以图形化方式观察进程.应用和监督层级. (1) Appmon.Appmon 是用 ...
- Python中range的用法
函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但 ...
- asp编程中获取上下两个月第一天和最后一天的代码
经常在asp编程遇到要获取上个月第一天和最后一天的日期,获取下个月第一天和最后一天的日期.这里总结了一下,将这些asp代码全部列出来了,以便以后遇到的时候使用. 上个月第一天:<%=dat ...
- NSString、NSData、char* 类型之间的转换-备
1. NSString转化为UNICODE String: (NSString*)fname = @“Test”; char fnameStr[10]; memcpy(fnameStr, [fname ...
- Unity3D中使用KiiCloud总结一
Kii Cloud简介 Kii Cloud提供一系列的服务,来帮助你为你的App获取用户,留住用户,以及创造利润.它为移动App提供了用户管理以及快捷安全可扩展的数据存储服务. 现在你可以在短短几分钟 ...
- php读取文件的各种方法
博客根据http://www.ibm.com/developerworks/cn/opensource/os-php-readfiles个人总结 获取文件全部内容 以下归类是按平时我们通常的使用方法总 ...