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

  • 思路:

  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] 被污染的河流

    https://www.luogu.org/problemnew/show/P3875 线段树扫描线求矩形面积并 扫描线的线段树有点奇怪,修改的标记不会下传,标记的意义是当前区间被完整地覆盖了多少次, ...

  2. 【线性代数】6-5:正定矩阵(Positive Definite Matrices)

    title: [线性代数]6-5:正定矩阵(Positive Definite Matrices) categories: Mathematic Linear Algebra keywords: Po ...

  3. Java锁优化

    Java锁优化 应用程序在并发环境下会产生很多问题,通常情况下,我们可以通过加锁来解决多线程对临界资源的访问问题.但是加锁往往会成为系统的瓶颈,因为加锁和释放锁会涉及到与操作系统的交互,会有很大的性能 ...

  4. windows 连接 Linux 云服务器

    1.在我们购买了 阿里云 或者 腾讯云后,如果选择使用的是 Linux 系统,在 windows 上要远程连接,需要用到的是 putty 这一个软件 putty 官网:https://www.putt ...

  5. SqlServer自动锁定sa解决代码

    ALTER LOGIN sa ENABLE ; GO ALTER LOGIN sa WITH PASSWORD = '' unlock, check_policy = off, check_expir ...

  6. linux下添加动态链接库路径、动态库加载等方法

    linux下添加动态链接库路径的方法 2017年01月20日 10:08:17 阅读数:5596   Linux共享库路径配置 Linux下找不到共享库文件的典型现象为明明已经安装某个软包(如libn ...

  7. linux设置脚本开机自启

    由于在centos7中/etc/rc.d/rc.local的权限被降低了,所以需要赋予其可执行权 chmod +x /etc/rc.d/rc.local 赋予脚本可执行权限假设/opt/script/ ...

  8. DriverManager

    1: 注册驱动 Class.forName("com.mysql.jdbc.Driver") ; static { try { java.sql.DriverManager.reg ...

  9. 新西兰程序员 ASP.NET网站中设置404自定义错误页面

    新西兰程序员 ASP.NET网站中设置404自定义错误页面 在用ASP.NET WebForm开发一个网站时,需要自定义404错误页面. 做法是这样的 在网站根目录下建立了一个404.html的错误页 ...

  10. kettle的用法

    一: 从一个数据库导入表的数据到另一个 数据库的表中(表数据同步) 1:在 主对象树-- DB连接 中新建 连接: 在选项中 设置字符集: 2: 在 核心对象中 先增加一个  表输入: 再增加一个 插 ...