用滑动窗口的思想来做。用一个unordered_map来查询之前的char有没有在现在的窗口中。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
unordered_map<char,int>mp;
int ans=,now=;//now is the window's instant length
int b=,e=;//the window's begin and end
int len=s.length();
for(int i=;i<len;i++){
if(mp.count(s[i])==&&mp[s[i]]>=b&&mp[s[i]]<e){//in the window
b=mp[s[i]]+;
e=i+;
now=e-b;
if(ans<now){
ans=now;
}
mp[s[i]]=i;
}
else{//not in the window
mp[s[i]]=i;
now++;
e++;
if(ans<now){
ans=now;
}
}
}
return ans;
}
};

其实,还有更好的做法。就是hash表并没有必要用map来表示。用一个数组就可以了。因为ASCII码的字符最多就255个。所以开一个256大小的数组足够了。

这种方法的妙处在于不把字符当成字符本身去处理,而是处理它对应的ASCII码,这样就把map变成普通数组也可以了,思路是一样的,只不过map是以字符为下标,这里的普通数组是以它的ASCII码为下标。

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int hash[];
memset(hash,-,sizeof(hash));
int b=,ans=;
int len=s.length();
for(int i=;i<len;i++){
b=max(b,hash[s[i]]+);
hash[s[i]]=i;
ans=max(ans,i-b+);
}
return ans;
}
};

leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

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

  3. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  4. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  5. [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  7. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  8. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  9. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

随机推荐

  1. C#数组 多个集合和数组的操作(合并,去重,拆分,判断)

    http://www.cnblogs.com/liguanghui/archive/2011/11/09/2242309.html http://www.cnblogs.com/dreamszx/ar ...

  2. 二、Android应用的界面编程(六)ProgressBar及其子类[SeekBar、RatingBar]er

    通常用于向用户显示某个耗时操作完成的百分比.Android支持几种风格的进度条,通过style属性可以为ProgressBar指定风格.该属性支持如下几个属性值. # @android:style/W ...

  3. iOS绘图CGContextRef详解

    转自:http://blog.csdn.net/u014286994/article/details/51333118 /* CoreGraphics - CGContext.h */ /** Gra ...

  4. APNS推送原理详解

    推送是解决轮询所造成的流量消耗和电量消耗的一个比较好的解决方案,在Android上,虽然Google提供了GCM(之前为C2DM),但在国内基本等于没用,各大Android应用基本都自己架设推送Ser ...

  5. 打广告:B站广告

    https://www.bilibili.com/video/av52230444/ https://www.bilibili.com/video/av52230444/ https://www.bi ...

  6. Django 之 ORM 字段和字段参数

    ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 简单的说,ORM是通过使用描述 ...

  7. Android系统移植与调试之------->如何修改Android设备的默认休眠时间

    1.找到~/mx0831-0525/frameworks/base/packages/SettingsProvider/res/values/ defaults.xml文件 2.修改默认休眠时间 3. ...

  8. python-安装 pip

    https://pip.pypa.io/en/stable/installing/ wget https://bootstrap.pypa.io/get-pip.py python get-pip.p ...

  9. python3的时间日期处理

    1.python3日期和时间 Python 程序能用很多方式处理日期和时间,转换日期格式是一个常见的功能. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 时 ...

  10. 从硬盘设计思想到RAID改良之道

    监控硬盘的前生今世关于桌面硬盘.企业级近线硬盘(NL-SAS/SATA)和监控硬盘的差别,我们在前文中已经讲得很详细,这里再换一个角度来看看. "监控硬盘是希捷和西数为视频监控定制的,典型的 ...