【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 ...
随机推荐
- (error) MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk
今天运行Redis时发生错误,错误信息如下: (error) MISCONF Redis is configured to save RDB snapshots, but is currently n ...
- 微软unity 注入mvc
首先获取开源unity ,引用, 新建UnityDependencyResolver 继承IDependencyResolver,代码如下: public class UnityDependencyR ...
- jQuery对数组操作
//对象数组 var trackObj1={ , "direcLine":"line31" }; var currentTrack=[]; currentTra ...
- Go收藏
Go项目收藏 电子书 1.Go Web 编程 2.Go入门指南(the-way-to-go_ZH_CN) 3.Go语言圣经(中文版) Go by Example 中文 一些Go英文电子书 High P ...
- php 路径的理解
当php文件用require方式包含了另外一个文件,这另外文件引用的图片是相对目录下的内容时,而该相对目录是指包含目录的文件的 -----index.php -----default 目录 --- ...
- maven实战(02)_坐标详解
(一) 何为mave坐标 maven的世界中拥有数量非常巨大的构件,也就是平时用的一些jar,war等文件. maven定义了这样一组规则: 世界上任何一个构件都可以使用Maven坐标唯一标志,ma ...
- Js计算当前日,当前周开始结束时间,当前月份,当前年份
<script type="text/javascript"> //日期加上天数后的新日期. function GetDateStr(AddDayCount) { va ...
- 二刷Cracking the Coding Interview(CC150第五版)
第18章---高度难题 1,-------另类加法.实现加法. 另类加法 参与人数:327时间限制:3秒空间限制:32768K 算法知识视频讲解 题目描述 请编写一个函数,将两个数字相加.不得使用+或 ...
- Unity 好坑的Save Scene
在编辑一个Untiy工程的时候,有很多的教程提到了 "Save Scene",也知道是干么用的.但是,后面打开工程的时候,工程界面是很多东西都不见了,又忘了有个Save Scene ...
- pullToRefreshListView的简单使用
1.加入library后直接布局 library下载地址:http://pan.baidu.com/s/1dFJu8pF <com.handmark.pulltorefresh.library. ...