Longest Substring Without Repeating Characters---LeetCode进阶路③
- 题目描述
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.
- 思路分析
输出最大无重复字符的子字符串长度,很常规的数据结构题目。
最粗暴的BF算法就可以解决,当然时间复杂度会是O(n^2)这样的闹心样子。
小陌想到的方法是,建立数组存储字符串,从头开始捋,遇到重复的字符则从下一个元素开始下一轮的子字符串寻找,即每个子字符串从相应起点开始到下一个有重复字符位置。
小陌特别想分享下被圈粉的大神思路 :用HashSet维护当前字符串,左右两端一起搞。
例如我们从左边先开始,只要没有出现重复字符就继续左移;一旦出现重复字符,就换一条路,从右边开始。中间所忽略的那些,它们是有重复或者更短,值得“忽略”。而在左右开弓的情况下,至多对元素访问一遍,时间复杂度仅为O(n)。
大神的思路阐述:“基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移动。同样是维护一个HashSet, 正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。”
- 源码附录:
1.小陌的方法

class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
int[] count = new int[256];
int start = 0;
int end = 1;
int longest = 1;
Arrays.fill(count,-1);
count[s.charAt(0)] = 0;
while(end < s.length()){
if(count[s.charAt(end)] >= start){
start = count[s.charAt(end)] + 1;
}
longest = Math.max(longest,end - start + 1);
count[s.charAt(end)] = end;
end ++;
}
return longest;
}
}
2.大神的方法 (大神地址https://blog.csdn.net/linhuanmars/article/details/19949159)
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
HashSet<Character> hs = new HashSet<Character>();
int start = 0;
int end = 0;
int longest = 0;
while(end < s.length()){
if(hs.contains(s.charAt(end))){
longest = Math.max(longest,end - start);
while(s.charAt(start) != s.charAt(end)){
hs.remove(s.charAt(start));
start ++;
}
start ++;
}
else{
hs.add(s.charAt(end));
}
end ++;
}
longest = Math.max(longest,end - start);
return longest;
}
}
Longest Substring Without Repeating Characters---LeetCode进阶路③的更多相关文章
- Longest Substring Without Repeating Characters leetcode java
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters -- LeetCode
原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...
- [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
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
随机推荐
- Joe主题 更换评论框样式,填写QQ自动获取昵称邮箱
前言: 由于为了美观感去除了画图模式,至于怎么加上画图模式会在文章最后给出详细教程. 同时也去除了填写网址选项,这个暂时无添加教程,关键没有合适的添加位子. 话不多说,直接开始教程! 下载文件包,解压 ...
- 【渗透测试】Vulnhub DarkHole
渗透环境 攻击机: IP: 192.168.216.129(Kali) 靶机: IP:192.168.216.130 靶机下载地址:https://www.vulnhub.com/entr ...
- Python基础笔记-while、字符串格式化、运算符、基础概念与数据类型
前言 !!!注意:本系列所写的文章全部是学习笔记,来自于观看视频的笔记记录,防止丢失.观看的视频笔记来自于:哔哩哔哩武沛齐老师的视频:2022 Python的web开发(完整版) 入门全套教程,零基础 ...
- 代码托管平台对比分析:Gitee与GitLab
一.Gitee:本土化服务的深度实践者 Gitee凭借对中国开发者需求的精准洞察,提供了多项针对性优化功能,尤其适合国内团队: 高速稳定的访问体验 服务器均部署于国内,代码拉取.推送及CI/CD流程的 ...
- Selenium KPI接口 屏幕截图
屏幕截图功能常用的有两种: savescreenshot()及 getscreenshotasfile(). 使用格式 self.driver.save_screenshot('baidu.png') ...
- Creo 4.0二次开发工具框架搭建
一.新建MFC DLL工程 二.配置项目属性 附加依赖项中输入:netapi32.lib;psapi.lib;mpr.lib;wsock32.lib;protk_dll_NU.lib;protk_d ...
- 使用MCP C# SDK开发MCP Server + Client
大家好,我是Edison. 近日被MCP刷屏了,刚好看到张队发了一篇文章提到MCP的官方C# SDK发布了预览版,于是手痒痒尝了一下鲜,写了一个DEMO分享给大家. MCP是什么鬼? MCP,全称是& ...
- 牛客小白月赛104 C-小红打怪
小红打怪 答案有单调性,使用二分答案来做 但是当时没有想到用二分,而是卡在怎么处理这三种攻击了. 可以把进行x回合的攻击,分为先进行x回合的全体打击,再进行x回合的范围打击,最后验证剩余血量够不够x回 ...
- CentOS 7 部署 GLPI 系统及集成方案
一.系统环境准备 1. 安装必要依赖 # 更新系统sudo yum update -y # 安装EPEL仓库sudo yum install -y epel-release # 安装必要组件sudo ...
- 『Plotly实战指南』--面积图绘制与应用
在数据可视化领域,面积图是一种强大而直观的工具,它通过填充线条与坐标轴之间的区域来量化数据大小, 从而帮助我们清晰地展示数据的总量.趋势变化以及不同类别之间的对比. 无论是分析随时间变化的累积量,还是 ...