用滑动窗口的思想来做。用一个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 selenium2示例 - 日志管理

    logger继承图 前言 在自动化测试实践过程中,必不可少的就是进行日志管理,方便调试和生产问题追踪,python提供了logging模块来进行日志的管理.下面我们就logging模块的学习和使用进行 ...

  2. Android异步处理三:Handler+Looper+MessageQueue深入详解

    在<Android异步处理一:使用Thread+Handler实现非UI线程更新UI界面>中,我们讲到使用Thread+Handler的方式来实现界面的更新,其实是在非UI线程发送消息到U ...

  3. ubuntu study

    1.statement a.Fist af all,I think myself be to study miv in instruction. b.again,I think myself be t ...

  4. web安全之SQL注入---第四章 如何进行SQL注入攻击

    第四章 如何进行SQL注入攻击1.数字注入2.字符串注入    '#    '--

  5. JAVA中sleep() 和 wait() 有什么差别?

    (网上的答案:sleep是线程类(Thread)的方法,导致此线程暂停运行指定时间,将运行机会给其它线程.可是监控状态依旧保持,到时后会自己主动恢复.调用sleep不会释放对象锁. wait是Obje ...

  6. java 中 集合类相关问题

    1,Java中Collection和Collections的差别 java.util.Collection 是一个集合接口.它提供了对集合对象进行基本操作的通用接口方法. Collection接口在J ...

  7. python 上传文件下载图片

    python 2.7 poster-0.8.1 requests-2.7.0 #coding:utf-8import urllibimport urllib2import sysimport time ...

  8. zookeeper snowflake 实战

    目录 写在前面 1.1.1. 集群节点的命名服务 1.1.2. snowflake 的ID算法改造 SnowFlake算法的优点: SnowFlake算法的缺点: 写在最后 疯狂创客圈 亿级流量 高并 ...

  9. 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例(转载)

     前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域 ...

  10. linux系统环境下搭建coreseek(+mmseg3) (good)

    1.下载并解压coreseek软件,操作命令如下: wget http://www.coreseek.cn/uploads/csft/3.2/coreseek-3.2.14.tar.gz 说明:文件下 ...