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

  • 思路:

  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. luoguP3366 【模板】最小生成树 x

    P3366 [模板]最小生成树 2.4K通过 6.3K提交 题目提供者HansBug 标签 云端↑ 生成树 难度 普及- 时空限制 1s / 128MB 题目描述 如题,给出一个无向图,求出最小生成树 ...

  2. Selenium中使用Cookies绕过登录

    在使用selenium测试后台时常常每个流程都需要走登录流程,这样自然比较浪费时间.如果遇到登录需要输入验证码等情况,就可能出师未捷身先死. 在Web应用中,登录状态通常是通过Cookie中对应的se ...

  3. MySQL数据分析-(13)表操作补充:索引

    大家好,我是jacky朱元禄,很高兴继续跟大家学习MySQL数据分析实战,今天跟大家分享的主题是表补充之索引: (一)前面课程的小节以及本节课程的逻辑梳理 在正式分享主题之前,jacky先跟大家捋顺一 ...

  4. nginx 部署php

    一:nginx安装: yum install nginx 安装完成即可,在/usr/sbin/目录下是nginx命令所在目录,在/etc/nginx/目录下是nginx所有的配置文件,用于配置ngin ...

  5. cesium billboard跨域问题1

    群里小伙伴问道使用billboard加载图片时出现跨域问题,一般认为在服务器端设置 Access-Control-Allow-Origin: * 例如用tomcat发布图片服务,可以这样设置:http ...

  6. phpstorm+xdebug安装配置

    这个问题也困惑了我好久 烦死了 今天看了qing师傅的博客 跟着安装 运行环境: phpStorm 2018 PHP 5.45 nts VC9 Xdebug 2.4.1 0x01 PHP安装xdebu ...

  7. ubuntu 16.04 脚本开机自启动

    1.首先编写一个shell脚本文件,例如python_self_start.sh (nohup & 指定后台运行) #!/bin/bash nohup python3 /home/senset ...

  8. flask 中扩展 flask-login

  9. 笔记四(Competitor Analysis Test小结)

    1.关机后启动电脑,测试BIOS的POST time 2.进入睡眠模式后,按任意键,通过Windows logs查看bios的init时间 3.进入BIOS setup的快捷键,一般为F2 4.进入B ...

  10. 字符串匹配 - hash

    之前有写过一篇hash表,不过那是非常久远的时候了,应该是大一刚学一个学期的时候的成果,后来也就不那样写了,后来从xiaoxin那里学习了hash的写法,比较容易用也比较方便多hash,就这样. 分别 ...