3. Longest Substring Without Repeating Characters无重复字符的最长子串
网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/
显然采用sliding window滑动窗口法,辅助以哈希表,判断字符是否已使用
- 不断扩充 j,直到 s[j] 已经使用
- 此时,把 i 移动到 j 之前的 s[j] 字符前面,继续循环
class Solution {
public:
int lengthOfLongestSubstring(string s)
{
int i = , j;
int ans = ;
map<char, int> mp;
for(j = ; j<s.size(); j++)
{
if(mp.find(s[j]) == mp.end() || mp[s[j]] == )
{
mp[s[j]] = ;
}
else
{
ans = max(ans,(j-i));
while(s[i] != s[j])
{
mp[s[i]] = ;
i++;
}
i++;
}
}
ans = max(ans,(j-i));
return ans;
}
};
3. Longest Substring Without Repeating Characters无重复字符的最长子串的更多相关文章
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
1. 原始题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &quo ...
- leetcode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
一.题目大意 https://leetcode.cn/problems/longest-substring-without-repeating-characters/ 给定一个字符串 s ,请你找出其 ...
- [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
随机推荐
- Centos不能上外网解决
检查路由 # route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0 ...
- JAVA微信支付代码(WeChatPay.java 才是调用类)
微信官方文档:https://pay.weixin.qq.com/wiki/doc/api/index.html MD5Util.java package weixin; import java.se ...
- C++重载操作符自增自减
#include <iostream> using namespace std; class Test { friend ostream& operator<<(ost ...
- ArrayList 除重
看到一段简洁的 ArrayList 除重代码: protected final <T> List<T> removeDuplicates(List<T> list) ...
- mysql 事务锁超时时间 innodb_lock_wait_timeout
mysql 事务锁超时时间 innodb_lock_wait_timeout: # 查询全局等待事务锁超时时间 SHOW GLOBAL VARIABLES LIKE 'innodb_lock_wait ...
- pypi上传问题
pypi上传过程中报错403 windows 解决办法: 1.建一个新的记事本编辑内容 [distutils]index-servers = pypi [pypi]repository:https:/ ...
- JavaScript判断对象有没有定义
if ( typeof(callbackfun) != "undefined" ) { callbackfun(); }
- P3371 【模板】单源最短路径(弱化版)(Dijkstra算法)
题目描述 如题,给出一个有向图,请输出从某一点出发到所有点的最短路径长度. 输入输出格式 输入格式: 第一行包含三个整数N.M.S,分别表示点的个数.有向边的个数.出发点的编号. 接下来M行每行包含三 ...
- Missing library: xdoclet-1.2.1.jar.如何解决?
去这里下载xdoclet-bin-1.2.1.zip http://sourceforge.net/projects/xdoclet/files/xdoclet/1.2.1/ 解压出来,比如解压到C: ...
- angular7 Rxjs 异步请求
Promise 和 RxJS 处理异步对比 Promise 处理异步: let promise = new Promise(resolve => { setTimeout(() => { ...