[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 ...
随机推荐
- 获取解码字符串指定位置的数值 Decoded String at Index
2018-10-04 12:53:06 问题描述: 问题求解: 首先本题给出了问题的规模,从Note中我们可以看到解码后的字符串长度甚至可以达到2^63的长度,这个长度已经远远超过整型数的范围,因此如 ...
- Oracle:如何创建一个只有查看权限的用户
因为工作中测试环境和开发环境是分开的,所以开发有时处理bug时需要连接测试数据库,这样出现一个问题是有些开发会为了验证某些问题任意改动数据库的表和字段,对测试库造成污染.为了能够让开发连接测试环境,同 ...
- 验证码之SimpleCaptcha (一)
在captcha中,两个比较著名的框架验证码有Jcaptcha和simpleCaptcha,Jcaptcha太庞大了,所以我选择了简单的SimpleCaptcha simpleCaptch ...
- Google云平台使用方法 | Hail | GWAS | 分布式回归 | LASSO
参考: Hail Hail - Tutorial windows也可以安装:Spark在Windows下的环境搭建 spark-2.2.0-bin-hadoop2.7 - Hail依赖的平台,并行处 ...
- KM算法 带权二分匹配 O(n^3)
#include<cstdio> #include<cstdlib> #include<cstring> #include<string> #inclu ...
- SP10707 COT2 - Count on a tree II (树上莫队)
大概学了下树上莫队, 其实就是在欧拉序上跑莫队, 特判lca即可. #include <iostream> #include <algorithm> #include < ...
- 【转】XP系统远程桌面连接2012R2提示:远程计算机需要网络级别身份验证,而您的计算机不支持该验证
一.背景 因对方客户的服务器是内网的,需要操作更新服务器的数据库表信息,因此远程对方客户办公司的电脑远程服务器:但是在远程桌面连接出现问题. 二.错误问题 错误问题:“远程计算机需要网络级别身份验证, ...
- python-flask-script定制manage命令
安装: pip3 install flask-script #!/usr/bin/env python # -*- coding:utf-8 -*- from flask_script import ...
- python-day73--django-用户验证
一.auth模块 from django.contrib import auth django.contrib.auth中提供了许多方法,这里主要介绍其中的三个: 1 .authenticate() ...
- java关键字总结
static: 用来修饰成员变量和成员方法,也可以形成静态static代码块,可以形成静态内部类,也可以用于静态导包. 1.静态方法中不能用this和super关键字,不能直接访问所属类的实例变量和实 ...