[Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口
【题目】
Given a string, find the length of the longest substring without repeating characters.
【举例】
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is"abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is"b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is"wke", with the length of 3.
Note that the answer must be a substring,"pwke"is a subsequence and not a substring.
【思路】
滑动窗口Sliding Window 【代码】
public class Solution {
public int lengthOfLongestSubstring(String s) {
int len=s.length();
int ans=0;int j=0;
Map<Character,Integer> map=new HashMap<>();
for(int i=0;i<len;i++){
if(map.containsKey(s.charAt(i))){
j=Math.max(j,map.get(s.charAt(i)));
}
ans=Math.max(ans,i-j+1);
map.put(s.charAt(i),i+1);
}
return ans;
}
}
[Leetcode 3] 最长不重复子串 Longest substring without repeating 滑动窗口的更多相关文章
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- 最长无重复字符的子串 · Longest Substring Without Repeating Characters
[抄题]: 给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 3. 对于, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
随机推荐
- google浏览器如何导出书签
首先打开浏览器点右侧的自定义及控制Google chrome. 点击书签-书签管理器 打开书签管理器界面中· 点击书签管理器的整理 最下面的将书签导出到html文件.. 弹出另存为对话 ...
- MyBatis配置文件中的常用配置
一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的conf.xml文件中,如下: <?xml version="1 ...
- Java从内存流中读取byte数组
Java中通过servlet接收二进制数据,然后将二进制数据流读取为byte数组.开始使用:byte[] bs = new byte[request.getContentLength()];reque ...
- python三级菜单实例(傻瓜版和进阶版)
程序: python三级菜单 要求: : 1.打印省.市.县三级菜单 2.可返回上一级 3.可随时退出程序 方案一:傻瓜版(其实傻瓜版考察的主要是思路!思路清楚了,那才不是傻瓜!O(∩_∩)O哈哈~) ...
- spring ----> 搭建spring+springmvc+mybatis出现的几个问题
环境: idea ce 2018.1+maven3.5.3+mysql8.0.11+jdk1.8 spring4.3.7+spring mvc4.3.7+mybatis3.4.1+tomcat7.0. ...
- every day a practice —— morning(6)
"Nearly one in five job ads for China's 2018 national civil service called for 'men only' or 'm ...
- EntityFramework的安装
关于EntityFramework在vs2012无法引用的问题 这段时间学习MVC,发现一个问题,我公司的电脑可以直接引用EntityFrameWork这个命名空间,但我家里面的电脑就不能直接引用,刚 ...
- hdoj1043
8数码问题有解:除0外逆序数%2相等.16数码有解:除0外,如果0的行数相差奇数个,逆序也差奇数个,vice versa.
- p1218 Superprime Rib
深搜,添加数字后如果仍为质数,继续递归. #include <iostream> #include <cstdio> #include <cmath> #inclu ...
- vue中使用transition标签底部导航闪烁问题
<transition :name="transitionName" :duration="{ enter: 500, leave: 0 }" > ...