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 WaterContainer 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——经典题的更多相关文章

  1. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  2. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  3. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  4. leetcode-【中等题】3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  5. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  6. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  7. Leetcode经典试题:Longest Substring Without Repeating Characters解析

    题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...

  8. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  9. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

随机推荐

  1. 几个与特殊字符处理有关的PHP函数(过滤html js 标签)

    函数名 释义 介绍 htmlspecialchars 将与.单双引号.大于和小于号化成HTML格式 &转成&"转成"' 转成'<转成<>转成> ...

  2. Codeforces Round #169 (Div. 2) A水 B C区间更新 D 思路

    A. Lunch Rush time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. stout代码分析之三:Option类

    为了安全表示NULL, stout实现了Option类.Option对象有两种状态: enum State { SOME, NONE, }; 其中SOME表示非空,NONE表示为空.可通过isSome ...

  4. bzoj 3246 [Ioi2013]Dreaming 贪心

    [Ioi2013]Dreaming Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 638  Solved: 241[Submit][Status][Di ...

  5. ExecuteNonQuery,ExecuteReader,ExecuteScalar 区别

    ExecuteNonQuery方法 :执行非查询SQL操作,包括增insert.删delete.改update ExcuteReader方法 :执行查询,返回DataReader,通过DataRead ...

  6. swiper 、css3制作移动端网站,折叠导航

    swiper .css3制作移动端网站,折叠导航 前几天公司要更新改版移动端的官网,由于网站本身没有多少内容,所以设计师就做成了整屏滑动的样子,起初我并没有看设计稿就一口答应了,拿到手后发现了几个问题 ...

  7. 让ie8、ie9支持媒体查询

    <!-- 让IE8/9支持媒体查询,从而兼容栅格 --> <!--[if lt IE 9]> <script src="https://cdn.staticfi ...

  8. 南阳ACM 题目22:素数求和问题

    素数求和问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 现在给你N个数(0<N<1000),现在要求你写出一个程序,找出这N个数中的所有素数,并求和. ...

  9. linux删除乱码

    一:前沿 好久没有记载东西了,回来之后一直忙着改东西,我走之前项目是什么样,回来后也差不多.郁闷啊,努力敲代码,但是要敲出思想来啊.先会做,然后深入思考 二:内容 (1)每次使用rz命令向服务器上传代 ...

  10. Eclipse Support UTF-8

    1.  Windows > Preferences > General > Content Types, set UTF-8 as the default encoding for ...