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 ...
随机推荐
- Flink - [08] 状态一致性
题记部分 一.什么是状态一致性 有状态的流处理,内部每个算子任务都可以有自己的状态.对于流处理器内部来说,所谓的状态一致性,其实就是我们所说的计算结果要保证准确.一条数据也不应该丢失,也不应该重复 ...
- dx12学习之旅-
记录一下,第一篇博客2024年7月26日下午. 计划在毕业后从事游戏开发的工作,现在在学习龙书dx12,平时会写一些对龙书内容上的一些理解.在读完全书之后,会考虑进行一次龙书相关的总结,不过这应该要很 ...
- C# 搭建一个 基于ISqlSugarClient 三层架构框架 涉及数据库仓储 然后中间又有业务逻辑层 案例
要在C#中搭建基于ISqlSugarClient的三层架构框架,你需要定义数据访问层(DAL).业务逻辑层(BLL)和表现层(UI).下面是一个完整的例子,涉及数据库仓储.业务逻辑层,以及依赖注入.这 ...
- MySQL 8.0 语法记录
SQL又杂又烦,记不住,网上搜到的语句还未必正确.这里做一个Record 基本操作 数据库操作 数据表操作 create index [索引名] on [表名]([列名]); /* 以选定列为索引信息 ...
- Qt QDateEdit下拉日历的样式设计
文章目录 QDateEdit样式设计 QDateEdit QCalendarWidget QDateEdit样式设计 最近做了一个用到QDateEdit的项目,涉及到对这个控件进行设计的方面,对于 ...
- Windows 提权-弱服务_1
本文通过 Google 翻译 Weak Service File Permissions – Windows Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭 ...
- Detected non-NVML platform: could not load NVML: libnvidia-ml.so.1: cannot open shared object
前言 在 kubernetes 中配置 https://github.com/NVIDIA/k8s-device-plugin 时, 报错:Detected non-NVML platform: co ...
- docker pause 命令使用
暂停正在运行的镜像容器 用途是在启动的容器的过程又的容器启动快了 有的还没有就绪 调试过程使用 a3: 正在运行的镜像容器简称 暂停: docker pause a3 解除暂停: docker unp ...
- macos设置docker可以ping容器
macos设置docker可以ping容器 项目连接不上seata 今天在启动项目时候seata报错: io.seata.common.exception.FrameworkException: ca ...
- Delphi 数据库连接查询分析器
为了方便自己查询数据库信息,花了小时间写了小工具,添加SQL语法高亮显示功能