[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 ...
随机推荐
- HeadFIrst Ruby 第二章总结 methods and classes
HeadFIrst Ruby 第二章总结 methods and classes 前言 这一章讲了如何创建自己的 class,并且讲了在用 class 创建 object 的两个要素: instanc ...
- WCF开山篇__图片传输
WCF开山篇__图片传输 一. 简介 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序接口,可以翻译为Windows通讯接口,它是. ...
- 20171104xlVBA制作联合成绩条
Dim dGoal As Object Dim dCls As Object Sub 制作联合成绩条() Dim sht As Worksheet Dim HeadRng As Range Dim H ...
- hdu-3001 三进制状态压缩+dp
用dp来求最短路,虽然效率低,但是状态的概念方便解决最短路问题中的很多限制,也便于压缩以保存更多信息. 本题要求访问全图,且每个节点不能访问两次以上.所以用一个三进制数保存全图的访问状态(3^10,空 ...
- php中安装memcache
1.查看php的版本(我的是5.6) 2.打开phpinfo查看第四行 3.然后选择要安装的文件 4.复制到 ext中 复制后 5.打开配置文件php.ini 把 extension=php_memc ...
- tcpcopy真实流量压测工具
https://quentinxxz.iteye.com/blog/2249799 http://blog.chinaunix.net/uid-25057421-id-5576741.html htt ...
- python-django rest framework框架之dispatch方法源码分析
1.Django的 CBV 中在请求到来之后,都要执行dispatch方法,dispatch方法根据请求方式不同触发 get/post/put等方法 class APIView(View): def ...
- php 求余
一,异号 1.函数值符号规律(余数的符号) mod(负,正)=正 mod(正,负)=负 结论:两个整数求余时,其值的符号为除数的符号. 2.取值规律 先将两个整数看作是正数,再作除法运算 ①能整除时, ...
- 一个典型的多表参与连接的复杂SQL调优(SQL TUNING)引发的思考
今天在看崔华老师所著SQL优化一书时,看到他解决SQL性能问题的一个案例,崔华老师成功定位问题并进行了解决.这里,在崔华老师分析定位的基础上,做进一步分析和推理,以便大家一起研究探讨,下面简述该案例场 ...
- MySQL安装教程(mysql5.6_bundle)
1.下载软件包 下载链接:https://dev.mysql.com/downloads/mysql/5.6.html#downloads centos6--MySQL-5.6.35-1.el6.x8 ...