Longest Substring Without Repeating Characters——经典题
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.
历遍字符串,当当前字符出现过的时候,子串开始位置+1,否则更新locs数组中的hash值为当前位置。
其实就是用两个指针,判断出字符串中所有重复出现的字符的长度,选出最大值即可,复杂度O(n) + O(n) = O(n),而不是Ο(n2)
这道题纠结了我好久啊!!!!!
(其实这道题和Trapping Rain Water和Container With Most Water这两道题有异曲同工之妙)
看了这篇博客:http://www.cnblogs.com/dollarzhaole/p/3155712.html
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int locs[];//保存字符上一次出现的位置
memset(locs, -, sizeof(locs));
int idx = -, max = ;//idx为当前子串的开始位置-1
for (int i = ; i < s.size(); i++)
{
if (locs[s[i]] > idx)//如果当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
idx = locs[s[i]];
}
if (i - idx > max)
{
max = i - idx;
}
locs[s[i]] = i;
}
return max;
}
}
Longest Substring Without Repeating Characters——经典题的更多相关文章
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- leetcode-【中等题】3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
随机推荐
- 非代码抽取的dex加固脱壳
常见的非代码抽取的dex加固,可以通过修改或者hook源码中的dex解析函数拿到目标dex完成脱壳.该dex解析函数为DexFile* dexFileParse(const u1* data, siz ...
- android脱壳之DexExtractor原理分析[zhuan]
http://www.cnblogs.com/jiaoxiake/p/6818786.html内容如下 导语: 上一篇我们分析android脱壳使用对dvmDexFileOpenPartial下断点的 ...
- 【ST表】【模板】ST表
Definition ST表是一种用于处理静态RMQ问题(无修改区间最值问题)的最快数据结构,书写方便使用简单效率便捷.其中其预处理复杂度为O(nlogn),查询复杂度为O(1).总时间复杂度为O(n ...
- Javascript让你的网页标题飘动起来
如果你有使用WebQQ,你也许会发现,每次收到信息的时候,就会看到title提示哪个网友或群来信息,然后网页的title就会开始飘动,挺人性化,蛮有意思的.而这个效果其实也不难,这里实现了这个效果,演 ...
- Foxmail安装和登录
Foxmail安装和登录... ============================== 下载Foxmail客户端:http://www.foxmail.com/ ================ ...
- Weblogic 9.2 启动时报错 javax.xml.namespace.QName
启动Weblogic 时会报错.javax.xml.namespace.QName; local class incompatible: stream classdesc serialVersionU ...
- 确保web安全的https、确认访问用户身份的认证(第七章、第八章)
第七章 确保web安全的https 1.http的缺点: (1)通信使用明文,内容可能会被窃听 (2)不验证通信方的身份,因此有可能遭遇伪装 (3)无法证明报文的完整性,因此有可能已遭篡改. 2.通信 ...
- 2015/8/28 Python基础(2):对象
Python用对象模型来存储数据.构造任何类型的值都是一个对象.Python对象都有是三个特性:身份,类型和值 身份是每个对象的唯一身份标识.任何对象都可以用内建函数id()来得到身份.如: > ...
- 深入HBase架构解析(一)
前记 公司内部使用的是MapR版本的Hadoop生态系统,因而从MapR的官网看到了这篇文文章:An In-Depth Look at the HBase Architecture,原本想翻译全文,然 ...
- RabbitMQ与Spring集成
RabbitMQ服务端安装: https://blog.csdn.net/hzw19920329/article/details/53156015 与Spring集成 https://www.cnbl ...