[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 ...
随机推荐
- jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第一话):初次启动jenkins,输入给定密码后登录失败问题解决
Jenkins是一个持续集成平台,它能够从git等源码管理服务器拉取代码.打包并发布到tomcat等中间件,只要配置好相关插件,就可以做到项目的自动化构建.部署,不论是对开发来说监控代码质量,还是对测 ...
- redis安装配置:inux系统为centOS 64位
下载Redis-4.0.6.tar.gz包 我下载的到自己的默认目录/root/software/下 1. 然后解压到这个目录下 /usr/local/src/ 解压命令: tar -xzf redi ...
- C#调用EXE
1.问题意义 据说界面程序开发,首选C#(像lebview之类的也很好) 但是,能不能用其他语言开发核心代码,只用C#做界面?毕竟每种语言都有自己擅长的领域. 2.exe程序 比如有个example. ...
- for...in和for...of循环的区别
使用for...in和for...of分别对Array,Set,Map做测试 var a=["A","B","C"]; var b=new ...
- Hisat2 bowtie2比对结果解读(Hisat2 Alignment summary)
RNA-seq数据的比对结果怎么解读?网上有很多人问,这里做一个大致的总结. Hisat2和bowtie2比对后产生的Alignment summary的格式是一样的,如下: Alignment su ...
- 安卓中使用OkHttp发送数据请求的两种方式(同、异步的GET、POST) 示例-- Android基础
1.首先看一下最终效果的截图,看看是不是你想要的,这个年代大家都很忙,开门见山很重要! 简要说下,点击不同按钮可以实现通过不同的方式发送OkHttp请求,并返回数据,这里请求的是网页,所以返回的都是些 ...
- PHP单例模式 demo
<?php class single{ static public $db; private function __construct(){ }; static function getinst ...
- HTML页面的三种弹框方式
1.弹出警告框,带确定按钮:alert 2.弹出,选择框 有确认和取消按钮 confirm 3. 弹出,输入框 : prompt
- ansible管理windows (发送文件)
https://github.com/ansible/ansible/raw/devel/examples/scripts/ConfigureRemotingForAnsible.ps1 环境: 服务 ...
- Git-解释“Swap file .MERGE_MSG.swp already exists”的问题
当合并代码时非正常保存退出遇到的问题. 博客原文: https://blog.csdn.net/qq_32452623/article/details/78395832