滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters
问题描述:
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.
解决:
class Solution {
public int lengthOfLongestSubstring(String s) {
int left = 0,right = 0,res = 0;
HashSet<Character> m = new HashSet<>();
while (right<s.length()){
if (!m.contains(s.charAt(right))){
m.add(s.charAt(right++));
res = Math.max(res,m.size());
}else {
m.remove(s.charAt(left));
left++;
}
}
return res;
}
滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters的更多相关文章
- 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)
Question: Given a string, find the length of the longest substring without repeating characters. Exa ...
- LeetCode3 Longest Substring Without Repeating Characters
题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- Leetcode3:Longest Substring Without Repeating Characters@Python
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- [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 without repeating characters. Example 1: In ...
- [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 ...
- 19.Longest Substring Without Repeating Characters(长度最长的不重复子串)
Level: Medium 题目描述: Given a string, find the length of the longest substring without repeating cha ...
随机推荐
- Jenkins管理员密码忘记修改操作
一.Jenkins管理员密码忘记 当jenkins忘记了管理用户的密码时,只能通过修改配置文件并重启的方式初始化设置用户名及密码,操作如下: [root@localhost jenkins]# vim ...
- codeforces 1045I Palindrome Pairs 【stl+构造】
题目:戳这里 题意:给1e5个字符串,问有多少对字符串组合,满足最多只有一种字符有奇数个. 解题思路:每种情况用map存一下就行了.感觉这题自己的代码思路比较清晰,所以写个题解记录一下 附ac代码: ...
- oslab oranges 一个操作系统的实现 实验一
实验目的: 搭建基本实验环境,熟悉基本开发与调试工具 对应章节:第一.二章 实验内容: 1.认真阅读章节资料 2.在实验机上安装virtualbox,并安装ubuntu 3.安装ubuntu开发环境, ...
- Leetcode(206)-反转链表
反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路:反转链表很简 ...
- SPOJ PHRASES Relevant Phrases of Annihilation(后缀数组 + 二分)题解
题意: 给\(n\)个串,要你求出一个最长子串\(A\),\(A\)在每个字串至少都出现\(2\)次且不覆盖,问\(A\)最长长度是多少 思路: 后缀数组处理完之后,二分这个长度,可以\(O(n)\) ...
- Linux 驱动框架---驱动中的时间相关
内核中的时间 Linux 系统内核对于时间的管理依赖于硬件,硬件按一定的周期产生中断,周期由内核的一个配置值HZ决定在系统启动时会将定时器配置为HZ值指定的频率产生中断:同时内核和维护一个64位(X8 ...
- TypeScript Version 23 Design Patterns
TypeScript Version 23 Design Patterns TypeScript 设计模式 https://refactoring.guru/design-patterns/types ...
- free useful skills videos courses & tutorials
free useful skills videos courses & tutorials website video courses https://realpython.com/ http ...
- ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await
ES Next & Arrow function & Promise & Iterator & Generator yield & Async Await co ...
- egg.js 如何禁用 sensors data
egg.js 如何禁用 sensors data holy shit http://localhost:7001/product/create const json = {"id" ...