LeetCode 3:Longest Substring Without Repeating Charact
LeetCode 3:Longest Substring Without Repeating Charact
Given a string s, find the length of the longest substring without repeating characters.
说人话:
在一个字符串中寻找没有重复字母的最长子串。
举例1:
举例2:
举例3:
举例4:
[法1] 滑动窗口
思路
滑动窗口的思想是我们取 2 个索引 i
和 j
,这样就围成了一个窗口,窗口 s[i…j] 是没有重复字母的。
① 然后我们不断向右边扩展,直至字符串结束或者是遇到重复字符了。那么这个时候我们就暂时找到了一个符合条件的子串的,记下当前的子串长度。
② 然后我们从左边删除字符,直至字符串不包含即将从右边加入的字符的时候就停下。
这样我们就又可以执行 ① 来扩展子串了,这个循环 ① ② 直至结束。
算法
class Solution {public int lengthOfLongestSubstring(String s) {int result = 0;//子串为 s[left...right]int left = 0;int right = -1;while(left < s.length()){//不断往后边加入满足条件的字符while(right+1 < s.length() &&!s.substring(left,right+1).contains(s.substring(right+1,right+2))){ right++;}//跳出循环,记录当前最长的子串长度result = (result > (right-left+1))?result:(right-left+1);//右边已经到底了,那就说明已经找到最长的了if(right == s.length()-1){break;}//不能往后边加的时候,从左边删,删到可以往后边加为止while(left < s.length() &&s.substring(left,right+1).contains(s.substring(right+1,right+2))){left++;}}return result;}
}
提交结果
代码分析
- 时间复杂度:O(n):实际上我们的 left 和 right 只是遍历了一遍字符串,所以时间复杂度只是 O(n)
- 空间复杂度:O(1)
改进思路
为什么 O(n) 的时间复杂度在 Leetcode 提交后执行用时这么久呢?其实是因为我们在判断字符串中是否存在某字符
的时候用了 JDK 的 API:s.contains(substring)
。这个 API 在判断的时候每次都会遍历我们整个字符串,这是比较耗时的,也是我们可以改进的地方。
[法2] 优化判断是否重复的逻辑
思路
ASCII 总共就 256 个,我们可以创建一个临时数组来记录字符串中的字符在 ASCII 中出现的频率,以此来判断是否存在重复字符。
代码
class Solution {public int lengthOfLongestSubstring(String s) {//记录字符出现频率int[] freq = new int[256];int result = 0;//子串为 s[left...right]int left = 0;int right = -1;while(left < s.length()){//不断往后边加入满足条件的字符while(right+1 < s.length() && freq[s.charAt(right+1)] == 0){right++;freq[s.charAt(right)] ++;}//跳出循环,记录当前最长的子串长度result = (result > (right-left+1))?result:(right-left+1);//右边已经到底了,那就说明已经找到最长的了if(right == s.length()-1){break;}//不能往后边加的时候,从左边删,删到可以往后边加为止while(left < s.length() && freq[s.charAt(right+1)] > 0){freq[s.charAt(left)] --;left++;}}return result;}
}
提交结果
代码分析
- 时间复杂度:O(n),优化后我们的时间复杂度还是一样的,但是省去了底层遍历字符串,大大减少了执行同时。
- 空间复杂度:用了存储字符使用频率的临时数组,O(256) = O(1)
LeetCode 3:Longest Substring Without Repeating Charact相关推荐
- Leetcode 3:Longest Substring Without Repeating Characters(最长不重复子串)
Description Given a string, find the length of the longest substring without repeating characters. 给 ...
- LeetCode算法入门- Longest Substring Without Repeating Characters-day4
LeetCode算法入门- Longest Substring Without Repeating Characters-day4 Longest Substring Without Repeatin ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路
笔者在完成LeetCode第三题(Longest Substring Without Repeating Characters)时,经历了设计.实现.优化三个阶段,于是通过这个三部曲系列,将当初的整个 ...
- 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 without repeating characters. For exam ...
- LeetCode:3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- 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
LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...
最新文章
- Electron 开发环境下总是 crash
- qlabel文本改变信号_周明:NLP进步将如何改变搜索体验
- 使用Leopard Jdbc
- nginx 优化(突破十万并发)
- 2021浙江高考成绩排名查询,2021年浙江高考成绩排名查询,第一批分数线23日公布...
- php原生判断,JavaScript
- 【web前端期末大作业】html在线网上书店 基于html制作我的书屋(23页面)
- coreseek mysql.sock_coreseek配置
- 自制Linux开发板,换个核心再来一次
- 保利威视视频云平台 新版本(Version 1.1.0) 上线通知
- 毕业设计论文 基于遥感影像的道路材质信息提取方法研究
- vue+tsx+slot
- 修改linux系统iqn,linux iscsi initiator 安装配置
- C语言程序设计博客作业06
- 全球20家最具创新力的创业公司
- JQuary学习之路---初始JQuary
- 评价最高影片JAVAlibrary_视频 | 手游大神,动画导演,机圈新贵,极客怎么评价愤怒的小鸟2?...
- SpringBoot JAVA 动态设置定时任务执行时间
- 联想ThinkPad开机进不了系统的解决方式
- B类职称论文发表一般是什么价格
热门文章
- eclipse 64位 免安装_超详细:64位Linux下安装PS模拟器ePSxe
- 信息系统规划方法-战略目标集转化法(SST)
- Todd Lammle's CCNA IOS Commands Survival Guide
- gym 101908C Pizza Cutter (逆序对)
- 2022年:企业绩效管理蓝图
- R语言---Seewave包和tuneR在声音分析中的应用①关于声音及简单分析
- rabbit 消息丢失
- JVM -- JVM内存结构:程序计数器、虚拟机栈、本地方法栈、堆、方法区(二)
- 快速接入百度大脑身份证识别
- 二维码图片生成(带文字显示)