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: ...
随机推荐
- Educational Codeforces Round 6 B
B. Grandfather Dovlet’s calculator time limit per test 1 second memory limit per test 256 megabytes ...
- Foxmail安装和登录
Foxmail安装和登录... ============================== 下载Foxmail客户端:http://www.foxmail.com/ ================ ...
- [技巧篇]06.关于防止SQL注入的方式,不使用预处理
在一期,二期阶段,有一些同学,对于SQL语句总是使用字符串的拼接,这是一个比较坏的毛病,这样非常影响我们的程序的安全性,所以一般情况下我们都推荐预处理模式,针对这种模式希望不了解的同学去努力学习,下面 ...
- Markdown 代码块中再内嵌一个行内代码
在 jQuery 1.9 之前(不含1.9):如果传入一个空字符串. null 或 jQuery.parseJSON( jsonString ) ,该函数将返回,而不是抛出一个错误,即使它不是有效的 ...
- 模板复习【updating】
马上就要noi了……可能滚粗已经稳了……但是还是要复习模板啊 LCT: bzoj2049 1A 7min # include <stdio.h> # include <string. ...
- 基数排序——尚未补完的坑QAQ
基数排序复杂度是(n+b)logn/logb 我们找一个基数 每次处理一部分位 从低位到高位处理 t是出现次数 s是这个桶管辖的起点 然后就可以写了 不过我这里是指针版的 有点难看 #include& ...
- HDU 1087 Super Jumping! Jumping! Jumping! --- DP入门之最大上升子序列
题目链接 DP基础题 求的是上升子序列的最大和 而不是最长上升子序列LIS DP[i]表示以a[i]结尾所能得到的最大值 但是a[n-1]不一定是整个序列能得到的最大值 #include <bi ...
- 教你 Shiro 整合 SpringBoot,避开各种坑(山东数漫江湖)
依赖包 <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-sprin ...
- 转一篇sublime必备的一些插件
Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...
- mysql not null default / default
not null default 说明不能是NULL, 并设置默认值 default 设置默认值 , 但值也可能是NULL mysql> create table test (id int, n ...