【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 ...
随机推荐
- 【APP设计利器】Sketch 41 Mac中文破解版(含汉化插件)
Sketch是一款拥有美观界面和强大功能适用于所有设计师的专业矢量绘图工具.它旨在为美术设计师创造出一款更优秀的作品,不是复制品,而是提升品.Sketch简约的设计是基于无限的规模和层次的绘图空间,免 ...
- 初识exception
一.exception的分类 根据此exception(异常)是否可以打断正在执行的指令,可以将exception分为 asynchronous exception 和 synchronous exc ...
- ORA-01722:无效数字
今天写查询语句,关联多张表,出现了这个错误. 结果发现时字段的数据类型不一致. select * from table_a a,table_b b where to_char(a.project_id ...
- 第四章 --- 关于Javascript 设计模式 之 迭代器模式
今天我先写 两个常用的迭代器的 例子.(同学们先自行体会这二种迭代器的优缺点) 需求:比较两个数组是否相等 tips: 当数组的下标不为数字的时候,默认为 该键值对 为 对象. 然后迭代器的原理基本来 ...
- JS 函数的柯里化与反柯里化
===================================== 函数的柯里化与反柯里化 ===================================== [这是一篇比较久之前的总 ...
- controller_name classify constantize model_name
控制器 class CourseSurveysController < ResourcesBaseController end controller_name # "course_su ...
- 《jQuery判断radio、checkbox、select 是否选中和设置选中问题以及获取选中值》总结
<form> <input type="radio" name="gender" id="man" value=" ...
- V4L2框架分析学习一
转载于http://www.techbulo.com/1193.html 1.概述 Video4Linux2是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口.凡 ...
- input输入时光标位置靠上问题解决
在css中如果我们定义了input高度在输入时会发现光标位置靠上了不在居中了,在Chrome浏览器中,当设置了line-height时,input无文字,光标高度与line-height一致:inpu ...
- [转载]PV操作简单理解
原文链接:http://blog.csdn.net/liushuijinger/article/details/7586656 进程通常分为就绪.运行和阻塞三个工作状态.三种状态在某些条件下可以转 ...