Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", 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.
Solution
Approach 1 :
用start, end 标记当前判别的子序列的起始与终止index。
1. 若当前序列s[start: end + 1] 没有重复字符,更新Max, 并 end + 1
2. 若当前新加入的s[end]与之前的子串subs中某元素重复,则定位subs中重复元素的index, 更新start为该index + 1,并更新subs.
依此规则,每一次end后移(+1),则进行一次判断并更新。
最终Max即为所求最长无重复子串长度。
Notice:
subs = list(s[start : end + 1])
这里若直接subs = s[start : end + 1],则subs为str类型,
需要转为list类型,即将str按字母元素分隔开。
eg. “abc” -> 'a','b','c'
因为subs中即为无重复子串,则无需用set,
可直接通过判断新加入的s[end]是否在subs中,来判断是否有重复字符
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
""" if not s: return 0
start, end, Max = 0, 0, 0
subs = []
while end < len(s):
if s[end] not in subs:
subs.append(s[end])
else:
start += subs.index(s[end]) + 1
subs = list(s[start : end + 1])
Max = end - start + 1 if end - start + 1 > Max else Max
end += 1
return Max
Beats:35.65%
Runtime: 124ms
Approach 2:
基本思路与上面一致
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
""" if not s:
return 0
longestlength = 1
substr = ""
for item in s:
if item not in substr:
substr += item
else:
if len(substr) > longestlength:
longestlength = len(substr) #将重复字符加入substr
substr += item
# 应该从substr中第一次出现重复字符的下一个字符开始继续判断
substr = substr[substr.index(item) + 1:] if len(substr) > longestlength:
longestlength = len(substr)
return longestlength
Beats: 44.09%
Runtime: 108ms
Approach 3: 用dict来存储判断重复元素
这里复制了leetcode上最快的答案。
解法中用到了
d: dict
i: 字母元素 d[i]: 更新后的字母元素在s中的index
每当遍历新的s元素,更新该元素在d中的映射值 (新元素:添加;已有元素:更新)
这样可以直接定位到其index, 而不需要每次通过s.index()查找
index表示当前遍历到的s中元素的ind
start表示当前子串的起始ind
count表示当前子串的长度
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
""" d = {}
maxlength = 0
count = 0
start = 0
for index, i in enumerate(s):
if i in d and d[i] >= start:
count = index - d[i]
start = d[i]
else:
count+=1
d[i] = index
if count>maxlength:
maxlength = count return maxlength
Beats: 99.93%
Runtime: 68ms
Leetcode 3. Longest Substring Without Repeating Characters (Medium)的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)
3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 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][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- Android学习笔记_34_自定义窗口标题
1.建好项目之后在它的layout文件夹下创建一个title.xml文件,作为自定义窗口标题的文件. <?xml version="1.0" encoding="u ...
- ADO.NET 之 Entity Framework 基础
Entity Framework(EF)是使用直接映射到应用程序中业务对象的对象模型于关系数据库进行交互.它没有将数据视为行和列的集合,而是将其视为强类型对象(成为实体)的集合. 术语:LinQ to ...
- 火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥
火狐 SSL 收到了一个弱临时 Diffie-Hellman 密钥 最近在用FireFox 调试时使用Https,连接 https网址 时发生错误. 在服务器密钥交换握手信息中 SSL 收到了一个 ...
- window与MAC,多台机器ssh免密码登录同一台机器执行某个脚本,
参考:https://zhidao.baidu.com/question/586579720.html A B C三台机器上以当前用户运行如下命令生成本主机的公钥和私钥文件: 1 ssh-keygen ...
- 工作流,WEB框架,UI组件网络收集整理
工作流,WEB框架,UI组件网络收集整理 在博客园上逛了好多年,随手收录了一些工作流,WEB开发框架,UI组件,现在整理一下与大家分享. 由于个人能力与精力有限,望各位园友在评论中补充,我将全部整理到 ...
- 云监控自定义HTTP状态码说明
您在使用站点监控时,返回的6XX状态码均为云监控自定义HTTP状态码,具体含义如下表所示: 状态码 含义 备注 610 HTTP连接超时 监测点探测您的网站时出现连接超 ...
- maven-认识
1.认识maven maven是强大的项目构建工具,也是依赖管理工具 使用maven前提是安装JDK maven非常重要配置文件:setting.xml 3.maven工程 maven工程的约束: 主 ...
- hadoop生态搭建(3节点)-04.hadoop配置
如果之前没有安装jdk和zookeeper,安装了的请直接跳过 # https://www.oracle.com/technetwork/java/javase/downloads/java-arch ...
- JDK8新垃圾回收机制--G1垃圾回收机制
G1全称是Garbage First Garbage Collector,使用G1的目的是简化性能优化的复杂性.例如,G1的主要输入参数是初始化和最大Java堆大小.最大GC中断时间. G1 GC由Y ...
- tarnado源码解析系列一
目录 tarnado tarnado源码安装 tarnado测试程序 application类的解析 一. tarnado简介 最近在学习Python,无意间接触到的tarnado,感觉tarnado ...