滑动窗口解决最小子串问题 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;}
转载于:https://www.cnblogs.com/zheng123/p/9713411.html
滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters相关推荐
- LeetCode3:Longest Substring Without Repeating Characters
原题链接:传送门 Given a string, find the length of the longest substring without repeating characters. For ...
- leetcode3. Longest Substring Without Repeating Characters
解法一: 1.维护三个变量: max用于记录当前最大值, next表示当前维护的正确子串, pre表示之前出现的重复字符再上一次出现的最靠前位置 A1:当前重复字符当前位置 A2:当前重复字符上一次位 ...
- leetcode算法—无重复字符的最长子串 Longest Substring Without Repeating Characters
关注微信公众号:CodingTechWork,一起学习进步. 题目 Longest Substring Without Repeating Characters: Given a string, fi ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)
Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...
- Leetcode 3:Longest Substring Without Repeating Characters(最长不重复子串)
Description Given a string, find the length of the longest substring without repeating characters. 给 ...
- 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)三部曲之一:解题思路
笔者在完成LeetCode第三题(Longest Substring Without Repeating Characters)时,经历了设计.实现.优化三个阶段,于是通过这个三部曲系列,将当初的整个 ...
最新文章
- nagios 使用MSN 发报警消息
- php mysql 读取中文数据的函数_php读取mysql中文数据出现乱码的解决方法
- 脑细胞膜等效神经网路12分类实例
- 我的世界拿java开服务器_我的世界如何开服务器
- vba 跳到下一个循环_VBA野知识分享:从一个数组中取部分值生成新数组,不使用循环的思路...
- 51单片机雾化片自动扫频程序_单片机简介
- linux awk 日志分析,Linux Awk使用案例总结 nginx日志统计
- 混口饭吃,谈不上喜欢
- 技术谈 | SDN 和 NFV 之间的爱与恨
- centos7 常用命令参考
- bzoj 4522: [Cqoi2016]密钥破解
- 不是区块链的特征_《区块链的特征》阅读练习及答案
- 寿光农商行计算机机房,寿光农村商业银行:新系统上线前奏——培训
- Typora简易教程
- 如何在Mac上打开和使用AirPlay,以便在更大的显示器上进行屏幕镜像?
- 树莓派系列二:openCV之头像添加国旗
- 推荐一些网络安全的网站和论坛
- 微信动态表情保存到手机相册
- myeclipse浅灰色主题/配色
- 使用HTML创建表单