Leetcode 解题 Longest Substring without repeating charcater python
原题:
Given a string, find the length of the longest substring without repeating character
For example, the Longest substring without repeating letters for "abcabcbb" is "abc", with the length is 3
思路:参考blog.csdn.net/hcbbt/article/details/43966513
开一个数组记录当前字符最近出现的位置,遍历,更新左边界,用它计算最大值。
代码:
class Solution:
def lenthOfLongestSubstring(self, s):
res = 0
left = 0
d = {} for i , ch in enumerate(s):
if ch in d and d[ch] >= left: left = d[ch] + 1
d[ch] = i
res = max(res, i -left + 1)
return res
Leetcode 解题 Longest Substring without repeating charcater python的更多相关文章
- 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 解题报告
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】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- [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 ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Leetcode3:Longest Substring Without Repeating Characters@Python
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 找出数组前N大的数
这个题也是个比较有名的面试题.当然有很多变种. 题目意思基本是:从一个数据量很大的数组里找前N大的元素.不允许排序. 这个题有两个比较好的思路: 思路一:用快速排序的思想,是思想,不是要排序; 思路二 ...
- node.js在windows下的学习笔记(2)---简单熟悉一些命令
1.打开如下的安装 2.输入node -v,显示node的版本号 3.输入node --help.显示帮助命令 4.在命令行中输入node,按下回车键,当出现>符号的时候即进入了node的REP ...
- 标准I/O库之二进制I/O
如果进行二进制I/O操作,那么我们更愿意一次读或写整个结构.因此,提供了下列两个函数以执行二进制I/O操作. #include <stdio.h> size_t fread( void * ...
- phpcms 源码分析三:common.inc.php
这次是逆雪寒分析common.inc.php的数据库部分: <?php // 包含数据库操作类,下章详说 require PHPCMS_ROOT.'/include/'.$db_file.'.c ...
- android ViewPager滑动事件讲解
首先ViewPager在处理滑动事件的时候要用到OnPageChangeListener OnPageChangeListener这个接口需要实现三个方法:(onPageScrollStateChan ...
- 观察者模式Demo
using System; using System.Collections.Generic; namespace Demo { #region 抽象层,定义了观察者模式 /// &l ...
- 解决在mybatis中使用CTE进行oracle查询数据类型为long的字段出现流关闭问题
今天把notice表中的content字段改为long字段后,含有该字段的使用CTE的查询语句报错了.提示一下错误 ### Cause: java.sql.SQLException: 流已被关闭 ; ...
- Clean Code 笔记 (一):命名
一.命名规范1.名副其实,通过名称我们知道它为什么存在,它做什么事,该怎么用2.避免误导,避免使用与本意相悖的词,提防使用不同之处较小的名称3.有意义的区分,去掉多余的废话比如:a,the.缺少明确约 ...
- 第九篇:web之前端之web上传文件的方式
前端之web上传文件的方式 前端之web上传文件的方式 本节内容 web上传文件方式介绍 form上传文件 原生js实现ajax上传文件 jquery实现ajax上传文件 form+iframe构 ...
- Objective-c中autorelease的释放时机
如果你使用过MRR,autorelease这个关键字应该是太熟悉了,每次在我们生成一个新的对象返回时,都需要向这个对象发送autorelease消息,目的是为了延时释放创建的对象.那到底是在什么时候, ...