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. Android学习笔记_54_自定义 Widget (Toast)

    1.Toast控件: 通过查看源代码,发现Toast里面实现的原理是通过服务Context.LAYOUT_INFLATER_SERVICE获取一个LayoutInflater布局管理器,从而获取一个V ...

  2. Unity让带有Rigidbody组件的游戏对象停止运动

    Rigidbody rigidbody = transform.GetComponent<Rigidbody>(); rigidbody.velocity = Vector3.zero; ...

  3. LeetCode13.罗马数字转整数 JavaScript

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并 ...

  4. Go转json数组

    Go转json数组 最近因需要要调用gitlab的API,其中有一个是根据私有token获取Repositories列表 由于返回结果是一个json数组,单纯使用json.Unmarshal没法实现, ...

  5. idea常用技巧

    1.如何设置,使IntelliJ IDEA智能提示忽略大小写 打开设置(CTRL+ALT+S)搜索editor,找到“Code Completion”->点击Case sensitive com ...

  6. 原生js的常见封装

    )); } ;;;;]){                 ];                 ] = ;;;,)     ,)     ,)     ,)         ,)         , ...

  7. js解决异步的方法汇总

    参考:https://www.cnblogs.com/zuobaiquan01/p/8477322.html 一.callback回调函数 回调是一个函数被作为一个参数传递到另一个函数里,在那个函数执 ...

  8. js实现前端的搜索历史记录

    最近在对接前台页面(WEB端)时,产品要求需记录下客户的搜索记录,我们是前后台完全分离的项目,根本不能保存的session域中,没办法,虽然作为后台开发,遇到需求就自己研究了一通,先看一下最终效果图, ...

  9. C语言实例解析精粹学习笔记——34(用“结构”统计学生成绩)

    实例34: 设学生信息包括学号.姓名和五门功课的成绩,要求编写输入输出学生信息的函数.在输入学生信息后,以学生成绩的总分从高到低顺序输出学生信息. 思路: 程序引入一个结构数组依次存储输入的学生信息, ...

  10. 贪心算法之Huffman

    Huffman编码,权重越大,离根节点越大.所以就是不断的选取两个最小的树,然后组成一颗新树,加入集合,然后去除已选的两棵树.不断的循环,直到最后的树的集合只剩下一棵,则构建完成,最后输出Huffma ...