leetcode 3 Longest Substring Without Repeating Characters(滑动窗口)
用滑动窗口的思想来做。用一个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(滑动窗口)的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
随机推荐
- Java中的使用了未经检查或不安全的操作(类前加:@SuppressWarnings("unchecked"))
Java中的使用了未经检查或不安全的操作 如此解决就可以了 类前面加@SuppressWarnings("unchecked") @SuppressWarnings("u ...
- 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 ...
- hihocoder1260,1261 (HASH经典题)
这两题比赛做的时候各种卡,太久没有写过这种类型的题目了.各种细节想不清楚. 赛后看下网上大部分题解的代码,发现大部分都是直接用TRIE+暴力直接搞的--!,随便找了份代码发现有些数据这种做法是超时的, ...
- poj2115[扩展欧几里德]
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22260 Accepted: 6125 Descr ...
- 九度OJ 1356:孩子们的游戏(圆圈中最后剩下的数) (约瑟夫环)
时间限制:10 秒 内存限制:32 兆 特殊判题:否 提交:1333 解决:483 题目描述: 每年六一儿童节,JOBDU都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此.HF作为JOBDU的资深 ...
- 洛谷 2679 [NOIP 2015] 子串
题目戳这里 一句话题意 给你两个字符串A,B从A中取出K个不重合子串(顺序与在A中顺序相同)组成B,问有多少种方案? Solution 话说重打还是出各种错误也是醉了 先看题目,因为答案与A串,B串和 ...
- ubuntun下安装Fiddler
对于分析网页或者写爬虫的时候经常需要用到抓包工具进行网页数据的抓包.在Windows下可以安装Fiddler来抓包.在ubuntun下不能直接安装Fiddler.需要先安装mono 1 首先安装mon ...
- eclipse js调试
问题: js经常会被浏览器给cache,不管怎么刷都是原来的. 解决: 暂时有一个方法,一刷新就好,下面的截图: 1)先打开 <开发者工具> 2)找到Sources 3)Page中找到你的 ...
- Yii2之事件处理
通过事件(Event)处理,可以在某个特定时刻执行指定的代码,可以解耦代码,同时也增加了可维护性,通常,事件在客户端软件中比较好理解,比如onClick,onFocus,当点击按钮,获取到焦点时执行指 ...
- PHP-内嵌式语言(转)(未看)
PHP,一个嵌套的缩写名称,是英文超级文本预处理语言(PHP:Hypertext Preprocessor)的缩写.PHP 是一种内嵌式的语言,PHP与微软的ASP颇有几分相似,都是一种在服务器端执行 ...