用滑动窗口的思想来做。用一个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. python之开篇---hello world!

    (1)前沿 (2)python 简介 (3)python hello world 实现 (4) -------------qq:1327706646 ------------------------- ...

  2. 如何通过Google访问外网

    修改host: https://laod.cn/hosts/2017-google-hosts.html google中文: https://www.google.com.hk/ 弄好前两项后,可以再 ...

  3. 在Linux中显示日历(cal)

    cal 2013    显示2013年整年日历 cal 7 2013  显示2013年 7 月 日历

  4. String、StringBuilder、 StringBuffer 深入分析 源代码解析

    java学习有一段时间了.但学习的东西都是框架等东西,java基础知识有点遗忘.所以重温一下java基础知识.写写文章里面有错的希望大家指正共同进步~~ 一.String 大家常常会说使用" ...

  5. 驱动程序分层分离概念_总线驱动设备模型_P

    分层概念: 驱动程序向上注册的原理: 比如:输入子程序一个input.c作为一层,下层为Dev.c和Dir.c,分别编写Dev.c和Dir.c向上Input.c注册:如图所示 分离概念: 分离概念主要 ...

  6. Java Enum 比较用 == 还是 eques

    我是把枚举当作常量来使用的,枚举中还有两个自己的属性,关注到这个地方的朋友对枚举已经有了认识,这里就不再编写枚举的demo了,这里我直接说结果吧,在枚举中使用==和equals比较效果是一样的,查看源 ...

  7. [Android]彻底去除Google AdMob广告

    应用中包含广告是能够理解的,但经常造成用户误点,或者广告切换时造成下载流量,就有点让人不舒服了. 以下就以Google AdMob广告为例,看怎样彻底去除他. 先分析一下Google AdMob的工作 ...

  8. js打开新窗口: window.open

    var iWidth = 800; var iHeight = 600; var iLeft = (window.screen.width - 10 - iWidth) / 2; //获得窗口的水平位 ...

  9. 【python】-- pymsql 外键

    pymsql 外键 本片是以上一篇pymsql操作MySQL的补充,主要演示pymysql的外键操作使用 一.一对一外键关联 1.示意图 2.一对一外键关联示例 2.1.创建表结构,插入数据 from ...

  10. 【python】-- web开发之jQuery

    jQuery jQuery 是一个 JavaScript 函数库,jQuery库包含以下特性(HTML 元素选取.HTML 元素操作.CSS 操作.HTML 事件函数.JavaScript 特效和动画 ...