【leetcode】Longest Substring Without Repeating Characters
题目描述:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
解题思路:
暴力方法不谈。学到了一种O(n)的方法,思想也是两个指针(我自己开始的AC代码考虑的是不匹配的回退值,不仅麻烦而且通用性还不够好)。首先我们用left和right分别表示满足题目的序列的两端下标,那么我们可以想到,首先在该值不重复的时候不断移动right指针,当遇到重复时我们则要比较是否满足最长,另外要移动left找到最佳的重新开始长度,这个长度就是使得新加入的right不是重复的。
class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
left =0
right = 0
res = 0
lis = []
l = len(s)
while right < l:
if s[right] in lis:
if res < right - left:
res = right - left
while s[left] != s[right]:
lis.remove(s[left])
left += 1
left += 1
else:
lis.append(s[right])
right += 1
if res < right - left:
res = right - left
return res
【leetcode】Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- 【Leetcode】【Medium】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode3】Longest Substring Without Repeating Characters★★
题目描述: 解题思路: 借用网上大神的思想:the basic idea is, keep a hashmap which stores the characters in string as key ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
随机推荐
- canvas.drawBitmap()频繁调用导致应用崩溃问题
因为opengl不熟,要在opengl上面贴文字 时间紧所以用到一个折中的办法 文字转bitmap 因为文字较多,对话形式 还要分行,分段,逻辑处理的比较复杂 运行中会有闪退发生,且不可 ...
- Django基础,Day6 - 单元测试tests
在django项目app目录下,有个tests.py,我们通常可以直接在这文件中写我们的单元测试代码. test for a model 根据前面章节的操作步骤下来,在Question Model中有 ...
- Hive : UDFArgumentTypeException Exactly one argument is expected.
Hive执行时Failed.. 分段执行发现排除一些聚合函数或者内置函数后可以正常执行.. 因为Hive-Sql语句比较长..有很多的case when then 排除后发现是聚合函数的用法问题.. ...
- Tomcat性能优化
1.关闭AJP协议 注释以下配置 <!-- <Connector port="8009" protocol="AJP/1.3" redirectPo ...
- java中Map,List与Set的区别(转)
Set,List,Map的区别 java集合的主要分为三种类型: Set(集) List(列表) Map(映射) 要深入理解集合首先要了解下我们熟悉的数组: 数组是大小固定的,并且同一个数组只能存放类 ...
- redis-window 集群配置
参考文章: 1.http://www.cnblogs.com/zr520/p/5057141.html (主从配置) 2.http://www.cnblogs.com/lori/p/5825691.h ...
- Solved: “Cannot execute a program. The command being executed was \roslyn\csc.exe”
When you publish your ASP.NET project to a hosting account such as GoDaddy, you may run into the iss ...
- mybase 用户教程
一.安装.卸载 1.安装 在Mac OS X环境下,可通过打开下载的.dmg文件,再把myBase图标拖到应用程序文件夹即可安装.然后通过双击程序图标运行程序 2.卸载 对于Mac OS X,把myB ...
- [Algorithm] 机器学习算法常用指标总结
考虑一个二分问题,即将实例分成正类(positive)或负类(negative).对一个二分问题来说,会出现四种情况.如果一个实例是正类并且也被 预测成正类,即为真正类(True positive), ...
- CentOS系统IPTables防火墙中FTP规则设置
时间 2016-04-21 10:32:15 虫虫开源 原文 http://www.sijitao.net/2403.html 主题 iptablesFTP防火墙 在设置ftp通过iptables ...