Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

详见:https://leetcode.com/problems/longest-substring-without-repeating-characters/description/

Java实现:

class Solution {
public int lengthOfLongestSubstring(String s) {
int[] hash=new int[256];
for(int i=0;i<256;++i){
hash[i]=-1;
}
int maxLen=0;
int start=-1;
for(int i=0;i<s.length();++i){
if(hash[s.charAt(i)]>start){
start=hash[s.charAt(i)];
}
hash[s.charAt(i)]=i;
maxLen=Math.max(maxLen,i-start);
}
return maxLen;
}
}

python实现:

方法一:

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
start,ans,d=0,0,{}
for i in range(len(s)):
c=s[i]
if c in d and d[c]>=start:
start=d[c]+1
else:
ans=max(ans,i-start+1)
d[c]=i return ans

方法二:

class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
ans, l, check = 0, 0, set()
for i in range(len(s)):
if s[i] in check:
while l<i and s[i] in check:
check.discard(s[l])
l+=1
check.add(s[i])
ans=max(ans,len(check)) return ans

003 Longest Substring Without Repeating Characters 最长不重复子串的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. LeetCode Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  3. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  6. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  7. Longest Substring Without Repeating Characters 最长不重复子串

    只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...

  8. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

随机推荐

  1. 动态webService

    using System; using System.Net; using System.IO; using System.CodeDom; using Microsoft.CSharp; using ...

  2. [转]Unity3D学习笔记(四)天空、光晕和迷雾

    原文地址:http://bbs.9ria.com/thread-186942-1-1.html 作者:江湖风云 六年前第一次接触<魔兽世界>的时候,被其绚丽的画面所折服,一个叫做贫瘠之地的 ...

  3. Webpack 基础使用

    使用webstorm编译: 1.新建一个工程(最基本的工程) 2.在webstorm的控制台,使用命令行     cnpm install webpack  -g  全局安装 3.安装完后,可以使用 ...

  4. svn、git等比较---总结

    免费的版本控制系统: CVS:集中式的版本控制系统,必须联网,速度慢,CVS作为最早的开源而且免费的集中式版本控制系统,直到现在还有不少人在用.由于CVS自身设计的问题,会造成提交文件不完整,版本库莫 ...

  5. ML 徒手系列 SVM

    在Lagrange乘子法中,介绍了如何构建及如何使用对偶函数,对目标问题进行求解. 这一章利用L乘子法对SVM进行推导. SVM 即支持向量机,是通过求解超平面进行分类的一种算法.所谓的支持向量,是在 ...

  6. shell入门-cut命令

    命令:cut 选项:-d:-f  指定第几段由“:(分割符)”分割的段 -c    指定第几个字符 说明:选取命令,选取一段数据中我们想要的,一般是针对每行来分析选取的 [root@wangshaoj ...

  7. Java 常见注解

    @Retention 1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略2.RetentionPolicy.CLASS —— ...

  8. 24、嵌合体序列Chimeras

    转载:http://www.cnblogs.com/xudongliang/p/6497465.html 嵌合体序列:由来自两条或者多条模板链的序列组成,示意图如下: 在PCR反应中,在延伸阶段,由于 ...

  9. Entity Framework Code-First(9.5):DataAnnotations - MaxLength Attribute

    DataAnnotations - MaxLength Attribute: MaxLength attribute can be applied to a string or array type ...

  10. JavaScript学习系列1 基础-变量

    1. 在一次面试中,被问到JavaScript中使用严格模式有什么区别, 这里的严格模式指的是 ECMAScript5严格模式 我们首先来看看普通模式,也就是非严格模式 a = 5; console. ...