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 ...
随机推荐
- iOS最笨的办法实现无限轮播图(网络加载)
iOS最笨的办法实现无限轮播图(网络加载) 简单的做了一下: 使用方法: 把 请求返回的 图片地址(字符串类型)放进数组中就行 可以使用SDWebImage(我就是用的这个)等..需要自己导入并引用, ...
- WCF基础之会话、实例和并发
这篇笔记是一些概念性的东西. 会话,借用百科上的描述就是一个客户与服务器之间的不中断的请求响应序列.wcf的会话模式是通过服务契约的SessionModel进行设置的,其值为枚举,分别为:Allowe ...
- CMD命令获取电脑所有连接过的WiFi密码
cmd中输入命令:for /f "skip=9 tokens=1,2 delims=:" %i in ('netsh wlan show profiles') do @echo ...
- GCD多线程在swift中的变化
1.异步线程加载主线程刷新 DispatchQueue.global().async { // TODO:执行异步线程网络请求 DispatchQueue.main.async(execute: { ...
- iOS 运行时详解
注:本篇文章转自:http://www.jianshu.com/p/adf0d566c887 一.运行时简介 Objective-C语言是一门动态语言,它将很多静态语言在编译和链接时期做的事放到了运行 ...
- zookeepeer ID生成器 (一)
目录 写在前面 1.1. ZK 的分布式命名服务 1.1.1. 分布式 ID 生成器的类型 UUID方案 1.1.2. ZK生成分布式ID 写在最后 疯狂创客圈 亿级流量 高并发IM 实战 系列 疯狂 ...
- Django 事务
Django事务 事务是通过将一组相关操作组合为一个,要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠.事务具有4个特性:原子性.一致性.隔离性.持久性. 默认情况下,在Djang ...
- Iptalbes练习题(一)
实验环境: KVM 虚拟机 centos6.7 test1:192.168.124.87 test2:192.168.124.94 场景一: 要求:1.对所有地址开放本机的tcp(80.22.10- ...
- 读paper:image caption with global-local attention…
最近的图片caption真的越来越火了,CVPR ICCV ECCV AAAI很多顶级会议都有此类的文章,今天我来讲一篇发表在AAAI的文章,因为我看了大量的论文,最近感觉AAAI越来越水了.所以这篇 ...
- 第10条:尽量用enumerate取代range
核心要点: (1)enumerate函数提供了一种精简的写法,可以在遍历迭代器时获知每个元素的索引. (2)尽量用enumerate来改写那种将range与下标访问相结合的序列遍历代码. (3)可以给 ...