Python 解leetcode:3. Longest Substring Without Repeating Characters
题目描述:求一个字符串的不含重复字符的最长连续子串的长度;
思路:
- 使用一个哈希表保存字符出现的位置;
- 使用left和right分别表示子串的最左和最右字符的下标;
- 遍历字符串,如果当前字符在哈希表中并且当前字符在哈希中的位置大于left,表明left和right之间存在和right索引上相同的字符,此时把left置为当前值在哈希表中的值;
- 把当前索引值+1,表示是第几个字符,求出当前最大长度;
- 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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- HTML基础要点归纳
一.开发环境 常用的HTML编辑器有Sublime Text.Hbuild.Dreamweare.以及vs code.pycharm等都可以.我目前在用的就是Sublime text3和Hbuild两 ...
- element-ui遮罩层el-dialog的使用
template <el-button type="text" @click="dialogVisible = true">点击打开 Dialog& ...
- 浅谈 es6 箭头函数, reduce函数介绍
今天来谈一下箭头函数, es6的新特性 首先我们来看下箭头函数长什么样子, let result = (param1, param2) => param1+param2; 上述代码 按照以前书写 ...
- spring事物不回滚的问题
学习spring分布式事务的时候,自己整了个demo,写test测试的时候发先事物没有回滚.此问题和分布式事务没关系.1.在service层引入@Transaction注解 使用 throw new ...
- 通过.zip安装eclipse插件
参考地址https://stackoverflow.com/questions/5482554/how-to-install-plugin-for-eclipse-from-zip
- 关于安卓端 点击button时出现橙色边框
一开始我以为是安卓的原因,后来经过测试发现不是,出现这个情况应该button的outline属性生效了,但是我已经写了outline 为none,后来发现,需要写上:foucs{ outline:0 ...
- git提交异常 fatal: LF would be replaced by..
git提交代码时,一直报出“fatal: LF would be replaced by CRLF in (文件名)”的异常,导致代码提交不到远程仓储.其实是,不同系统对换行符的解释不同导致的. 方法 ...
- (一)OpenCV-Python学习—基础知识
opencv是一个强大的图像处理和计算机视觉库,实现了很多实用算法,值得学习和深究下. 1.opencv包安装 · 这里直接安装opencv-python包(非官方): pip install ope ...
- LeetCode 被围绕的区域
给定一个二维的矩阵,包含 'X' 和 'O'(字母 O). 找到所有被 'X' 围绕的区域,并将这些区域里所有的 'O' 用 'X' 填充. 示例: X X X X X O O X X X O X X ...
- Splinter自动登录
默认用foxfire浏览器,如果用chrome请到官网下载 chromedriver驱动,解压后放到python目录scripts下 然后添加环境变量,在Path下添加chromedrvier的路径. ...