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. debian上安装codeblocks

    1.查看linux的版本uname -a 2.在官网上下载稳定版的codeblocks(www.codeblocks.org) 3.解压codeblocks后,进入到文件夹中用root身份执行dpkg ...

  2. 修改initrd.img里ko文件的一个小tips

    在经历以下步骤解开initrd.img文件之后: 若file initrd.img 指示initrd.img为gzip文件,则2: mv initrd.img initrd.gz gunzip -d ...

  3. 【jQuery】CheckBox使用attr全选无法正确显示

    今天编写JS脚本时,遇到如下的问题. 下面是源代码: <script src="../Scripts/jquery-2.1.3.js"></script> ...

  4. 在python3.6下 发明一个类似python3.7 dataclass数据类,不用在 __init__中self.xx

    虽然我用3.6,但我在2.7转3.6时候,把3.3 3.4 3.5 3.6的变化都看了一次,虽然已经忘了哪些变化.同时也关注3.7 3.8的变化,3.7中就有1个数据类印象深刻,因为之前在定义这种类时 ...

  5. C#设计模式(8)——桥接模式

    一.概念 桥接模式即将抽象部分与实现部分脱耦,使它们可以独立变化. 二.模型 三.代码实现 // 客户端调用 // 类似Web应用程序 class Client { static void Main( ...

  6. 如何在niosII中添加i2c外设_winday_新浪博客

    如何在niosII中添加i2c外设_winday_新浪博客 如何在niosII中添加i2c外设 winday 摘要:本文说明了如何在niosII添加第三方i2c外设,以供参考. 由于本人使用的Alte ...

  7. Ajax效果--个人收藏

    $.ajax({ url: "../../../Tools/WeChatMenu.ashx?action=get_menu", type: "post", da ...

  8. 8、非root权限下安装perl以及perl模块

    转载:http://www.cnblogs.com/nkwy2012/p/6418669.html 转载自http://www.zilhua.com 在本博客中,所有的软件安装都在服务器上,且无roo ...

  9. iOS开发,使用CocoaSSDP查找设备时按关键字过滤Device

    关于CocoaSSDP的资料有很多,这里就不介绍了. 希望寻找的目标设备,在header中设置了自定义的keyword,虽然通过外围代码也能达到相同目的,但是直接修改CocoaSSDP源码更简便. 导 ...

  10. idea中java项目删除一个module

    在idea中删除一个module,需要两步: 1. 使用Remove Module命令,快捷键是Delete 2. 经过第一步后,module图标上的小方块,消失,编程一个普通文件夹,这时使用Dele ...