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)的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)

    3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...

  3. 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]

    题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...

  4. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  5. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  6. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  7. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  8. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

  9. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

随机推荐

  1. Tomcat+Oracle配置连接池的例子

    我这有一个Tomcat+Oracle连接池的例子,放上来和大家分享一下. Tomcat +Oracle  连接池配置   Author: Kenneth.Leaf@GalaxySoft Date: / ...

  2. mysql自连接

    自连接: -- 求7369员工编号,姓名,经理编号和经理姓名 select e1.empno,e1.ename,e2.empno,e2.ename from emp e1,emp e2 where e ...

  3. python-time、datetimme模块

    time模块 1.time.time():返回当前时间的时间戳. 打印时间戳: >>> import time >>> time.time() 1530329387 ...

  4. Question20180106 Java环境变量的配置及为什么要配置环境变量

    Question 1  Java环境变量的配置及为什么要配置环境变量 Q1.1为什么要配置环境变量 在学习JAVA的过程中,涉及到多个环境变量(environment variable)的概念,如PA ...

  5. 你不知道的javaScript笔记(5)

    原生函数 常用的原生函数 String() Number() Boolean() Array() Object() Function() RegExp() Date() Error() Symbol( ...

  6. 移动设备HTML5页面布局

    在HTML5标准添加的新元素中,用于常见页面结 构的包括header footer footer nav aside aside article section hgroup . 下面简单介绍一下这个 ...

  7. 新疆大学ACM-ICPC程序设计竞赛五月月赛(同步赛)-B-杨老师游戏

    题目链接:杨老师游戏 题目分析:将9个数字分成3块,分块枚举,话句话说,9个数字的所有排列组合,如果满足N=a*b-c就是一个答案,暴力枚举Orz. 代码如下:  #include<iostre ...

  8. SPOJ PRIME1 - Prime Generator(线性筛)

    Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...

  9. 解决ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)【亲测有效】

    文件转自:https://blog.csdn.net/hua1011161696/article/details/80666025 问题:(MySQL 5.6社区版windows版) 忘记密码或其他一 ...

  10. mysql更新返回值问题(更新内容跟之前内容一样,返回0)

    mysql更新返回值问题 问: 有一界面要更新个人信息,有几十个text标签需要填写假设有一用户从用户列表点修改进入了修改页面,但又没有修改什么,马上点击保存这时,因为text标签非常多,不能够一一判 ...