• 题目描述:求一个字符串的不含重复字符的最长连续子串的长度;

  • 思路:

  1. 使用一个哈希表保存字符出现的位置;
  2. 使用left和right分别表示子串的最左和最右字符的下标;
  3. 遍历字符串,如果当前字符在哈希表中并且当前字符在哈希中的位置大于left,表明left和right之间存在和right索引上相同的字符,此时把left置为当前值在哈希表中的值;
  4. 把当前索引值+1,表示是第几个字符,求出当前最大长度;
  5. 3-4步重复,直到获得遍历完成;

感觉这是个动态规划题,暂时没用动态规划分析,后续再说。

class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
hashes = {}
left, right, length = 0, 0, len(s)
max_len = 0
while right < length:
if hashes.get(s[right]) and hashes[s[right]] >= left:
left = hashes[s[right]]
hashes[s[right]] = right + 1
max_len = max(max_len, right - left + 1)
right += 1
return max_len

Python 解leetcode:3. Longest Substring Without Repeating Characters的更多相关文章

  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 最长无重复子串

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

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

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

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

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

  5. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

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

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

  7. LeetCode之Longest Substring Without Repeating Characters

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

  8. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

    Description Given a string, find the length of the longest substring without repeating characters. E ...

  9. [Leetcode Week1]Longest Substring Without Repeating Characters

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

  10. [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] LCA

    https://www.luogu.org/problemnew/show/P4211 baoli #include <iostream> #include <cstdio> ...

  2. Qt ASSERT:"QMetaObjectPrivate::get(smeta)->revision>= 7"in file kernel\qobject.cpp,line 2646

    qt5.6.1所做的工程在运行时出现该问题:该问题说的是信号槽中 函数的参数不匹配. 在qt4.8.4 中QThread 中查到assitant中定义void QThread::finished () ...

  3. 2019.7.9 校内测试 T1挖地雷

    这一次是交流测试?边交流边测试(滑稽 挖地雷 这个题是一个递推问题. 首先我们看第一个格子,因为它只影响了它的上面和右上面这两个地方是否有雷. 我们可以分3种情况讨论: 1. 第一个格子的数字是2: ...

  4. PHP 之实现按日期进行分组、分页

    一.效果图 二.原始数据 array(6) { [0]=> array(8) { ["id"]=> string(1) "6" ["use ...

  5. 常见的RuntimeException

    一般面试中java Exception(runtimeException )是必会被问到的问题常见的异常列出四五种,是基本要求.更多的....需要注意积累了 常见的几种如下: NullPointerE ...

  6. jinja2-模版继承

    一 简要 简单的来说模板继承包含基本模板和子模板.其中基本模板里包含了你这个网站里的基本元素的基本骨架,但是里面有一些空的或者是不完善的块(block)需要用子模板来填充. 二 基本模版样例 这个模板 ...

  7. SOA(面向服务的架构)初识

    SOA是一种设计方法,其中包含多个服务,而服务之间通过配合最终会提供一系列功能.一个服务通常以独立的方式存在于操作系统中.服务之间通过网络调用(常见有http+xml.http+json等),而非进程 ...

  8. sqlserver 锁表进程及执行的SQL

    --1.查进程select request_session_id spid,OBJECT_NAME(resource_associated_entity_id) tableNamefrom sys.d ...

  9. IDEA 好用的插件

    IDEA 好用的插件 非自带的一些自用插件. Alibaba java Coding Guidelines 阿里出的java规范,支持eclipse和Idea,支持实时扫描,规范代码,养成良好习惯.推 ...

  10. Facebook币Libra学习-4.新的智能合约语言Move入门

    Move是一种新的编程语言,旨在为Libra Blockchain提供安全可编程的基础.Libra Blockchain中的帐户是任意数量的Move资源和Move模块的容器.提交给Libra Bloc ...