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. 【luogu P3865 ST表】 模板

    跟忠诚是一样滴,不过是把min改成max就AC了.模板题. #include <cstdio> #include <algorithm> using namespace std ...

  2. C#结构体和字节数组的转换函数

    在通信过程中,一般我们都会操作到字节数组.特别是希望在不同语言编程进行操作的时候. 虽然C#提供了序列化的支持,不用字节数组也行.但操作字节数组肯定会碰到.   一般都会采用结构来表示字节数组.但结构 ...

  3. SpringBoot非官方教程 | 第二十二篇: 创建含有多module的springboot工程

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springboot/2017/07/11/springbot22-modules/ 本文出自方志朋的博客 这篇文 ...

  4. NEC 框架规范 animation

    /* animation *//* 淡入 */.a-fadein{-webkit-animation-name:fadein;-moz-animation-name:fadein;-ms-animat ...

  5. ES6 语法学习总结

    第一节:什么是ES6?   ES6是什么?跟JavaScript有什么关系? JavaScrip由三部分组成:分别是ECMAScript,BOM和DOM. 1)由此看出,ECMAScript是Java ...

  6. Form表单之复选框checkbox操作

    input复选(checkbox): <label>input复选1组:</label> <input type="checkbox" name=&q ...

  7. C++最接近整数的浮点运算

    Function return ceil 不小于给定值的最接近整数值 floor 不大于给定值的最接近整数 trunc (C++11) 绝对值不大于给定值的最接近整数 round(C++11) 最接近 ...

  8. ABAP术语-Accounting Document

    Accounting Document 原文:http://www.cnblogs.com/qiangsheng/archive/2007/12/12/991731.html Accounting d ...

  9. LVS NAT,DR,TUN三种负载原理

    负载均衡简单介绍 用通俗的话来说负载均衡,就是通过不同的调度机制将用户的请求分派到后端不同的服务器.缓解服务器的请求压力,实现负载均衡的方案有多种,下面简单说说了解的几种方式: DNS 负载:利用DN ...

  10. 快速玩转linux(1)

    快速上手Linux玩转典型应用 mark 大牛都会使用Linux, Linux命令是行业要求. 商业服务器基本都是linux 开源软件都先支持Linux(只支持) 大数据分析.机器学习首选Linux ...