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: ...
随机推荐
- Linux系统上的popen()库函数
popen可以是系统命令,也可以是自己写的程序a.out. 假如a.out就是打印 “hello world“ 在代码中,想获取什么,都可以通过popen获取. 比如获取ls的信息, 比如获取自己写的 ...
- Django Model 数据表
Django Model 定义语法 版本:1.7主要来源:https://docs.djangoproject.com/en/1.7/topics/db/models/ 简单用法 from djang ...
- MyBatis子查询
一.父查询BaseChildResultMap: <?xml version="1.0" encoding="UTF-8" ?> <!DOCT ...
- 题解 【luogu P1541 NOIp提高组2010 乌龟棋】
题目链接 题解 题意: 有一些格子,每个格子有一定分数. 给你四种卡片,每次可以使用卡片来前进1或2或3或4个格子并拾取格子上的分数 每张卡片有数量限制.求最大分数. 分析 设\(dp[i]\)为第前 ...
- HDU 4529 状压dp
郑厂长系列故事——N骑士问题 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)To ...
- Android核心类源码分析
Handler流程1.首先Looper.prepare()在本线程中保存一个Looper实例,然后该实例中保存一个MessageQueue对象:因为Looper.prepare()在一个线程中只能调用 ...
- bzoj 2705: [SDOI2012]Longge的问题——欧拉定理
Description Longge的数学成绩非常好,并且他非常乐于挑战高难度的数学问题.现在问题来了:给定一个整数N,你需要求出∑gcd(i, N)(1<=i <=N). Input 一 ...
- bzoj 1030 ac自动机
比较容易看出来先建立ac自动机,然后在自动机上做DP,设w[0..1][i][j]为当前不包括/包括字典中的字符串,当前在自动机中走到第i个节点,完成的文本的长度为j的方案数,那么比较容易的转移w[i ...
- python 实现二叉树相关算法
一.构建与遍历二叉树 基本性质 1)在二叉树的第i层上最多有2i-1 个节点 .(i>=1)2)二叉树中如果深度为k,那么最多有2k-1个节点.(k>=1)3)在完全二叉树中,具有n个节点 ...
- centos6.5升级Linux内核步骤
centos6.5升级Linux内核步骤 http://www.jianshu.com/p/c75f00182b4c 使用的操作系统是是centos6.5,按照官方的推荐的配置,把linux内核升级到 ...