用滑动窗口的思想来做。用一个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. 删除路径下的文件以及子路径。(范例“E:/www/”)

    /// <summary> /// 删除路径下的文件以及子路径. /// </summary> /// <param name="dir">目标 ...

  2. 使用CSDN CODE来存放OPENSTACK位于GITHUB上的源代码

    use CSDN CODE to pull openstack codes 2014-11-20 Author:Hyphen 问题 直接从GITHUB上获代替码,常常是没保障,特别是用DEVSTACK ...

  3. js的new到底干了啥 -

    javascript通过new操作符构建一个对象的步骤 <Javascript高级程序设计>的解释: 创建一个对象 将构造函数的作用域赋给新对象(把新对象作为构造函数的调用上下文,也就是t ...

  4. mybatis相关

    1 namespace dao中使用namespace+id一起来完成对mapper中sql statement的调用. 2 关于resultMap和parameterType parameterTy ...

  5. HealthKit详解

    1. 导入HealthKit框架 #import <HealthKit/HealthKit.h> 2. 判断设备是否支持HealthKit HealthKit是iOS8加入的API Hea ...

  6. What I learned from competing against a ConvNet on ImageNet

    http://karpathy.github.io/2014/09/02/what-i-learned-from-competing-against-a-convnet-on-imagenet/

  7. c#的const可以用于引用类型吗

    答案是可以的.不过用const修饰的类实例只能是null. class A{ public int a=0; } class B{ const A constA=null; const object ...

  8. C# Interactive Shell

    C# Pad 有点像VisualStudio中的ImmediateWindow,程序运行中的一些变量都保存着,可以直接从命令行访问,方便执行一些code来进行测试或debug. 上图中右边每一个小时钟 ...

  9. Android 开发之深入理解安卓调试桥各种错误解决办法

    摘要: Android开发调试项目使用到安卓调试桥工具,Android Debug Bridge(ADB)位于sdk路径platform-tools文件夹,使用Android Studio或Eclip ...

  10. eclispse + tomcat 启动是不加载项目的解决办法

    有一个java spring的项目一直好好的,突然一天不能启动了.eclipse的console没有报任何错误,相关的server配置也没有问题,经过一翻折腾顺便还把eclipse从indigo升级到 ...