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

  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstring(string s) {
  4. unordered_map<char,int>mp;
  5. int ans=,now=;//now is the window's instant length
  6. int b=,e=;//the window's begin and end
  7. int len=s.length();
  8. for(int i=;i<len;i++){
  9. if(mp.count(s[i])==&&mp[s[i]]>=b&&mp[s[i]]<e){//in the window
  10. b=mp[s[i]]+;
  11. e=i+;
  12. now=e-b;
  13. if(ans<now){
  14. ans=now;
  15. }
  16. mp[s[i]]=i;
  17. }
  18. else{//not in the window
  19. mp[s[i]]=i;
  20. now++;
  21. e++;
  22. if(ans<now){
  23. ans=now;
  24. }
  25. }
  26. }
  27. return ans;
  28. }
  29. };

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

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

  1. class Solution {
  2. public:
  3. int lengthOfLongestSubstring(string s) {
  4. int hash[];
  5. memset(hash,-,sizeof(hash));
  6. int b=,ans=;
  7. int len=s.length();
  8. for(int i=;i<len;i++){
  9. b=max(b,hash[s[i]]+);
  10. hash[s[i]]=i;
  11. ans=max(ans,i-b+);
  12. }
  13. return ans;
  14. }
  15. };

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. Java中的使用了未经检查或不安全的操作(类前加:@SuppressWarnings("unchecked"))

    Java中的使用了未经检查或不安全的操作 如此解决就可以了 类前面加@SuppressWarnings("unchecked") @SuppressWarnings("u ...

  2. xcrun: error: unable to find utility "instruments", not a developer tool or in PATH

    xcrun: error: unable to find utility "instruments", not a developer tool or in PATH   用web ...

  3. hihocoder1260,1261 (HASH经典题)

    这两题比赛做的时候各种卡,太久没有写过这种类型的题目了.各种细节想不清楚. 赛后看下网上大部分题解的代码,发现大部分都是直接用TRIE+暴力直接搞的--!,随便找了份代码发现有些数据这种做法是超时的, ...

  4. poj2115[扩展欧几里德]

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22260   Accepted: 6125 Descr ...

  5. 九度OJ 1356:孩子们的游戏(圆圈中最后剩下的数) (约瑟夫环)

    时间限制:10 秒 内存限制:32 兆 特殊判题:否 提交:1333 解决:483 题目描述: 每年六一儿童节,JOBDU都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为JOBDU的资深 ...

  6. 洛谷 2679 [NOIP 2015] 子串

    题目戳这里 一句话题意 给你两个字符串A,B从A中取出K个不重合子串(顺序与在A中顺序相同)组成B,问有多少种方案? Solution 话说重打还是出各种错误也是醉了 先看题目,因为答案与A串,B串和 ...

  7. ubuntun下安装Fiddler

    对于分析网页或者写爬虫的时候经常需要用到抓包工具进行网页数据的抓包.在Windows下可以安装Fiddler来抓包.在ubuntun下不能直接安装Fiddler.需要先安装mono 1 首先安装mon ...

  8. eclipse js调试

    问题: js经常会被浏览器给cache,不管怎么刷都是原来的. 解决: 暂时有一个方法,一刷新就好,下面的截图: 1)先打开 <开发者工具> 2)找到Sources 3)Page中找到你的 ...

  9. Yii2之事件处理

    通过事件(Event)处理,可以在某个特定时刻执行指定的代码,可以解耦代码,同时也增加了可维护性,通常,事件在客户端软件中比较好理解,比如onClick,onFocus,当点击按钮,获取到焦点时执行指 ...

  10. PHP-内嵌式语言(转)(未看)

    PHP,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写.PHP 是一种内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行 ...