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软件安装和卸载

    用root身份执行如下命令: dpkg -l |grep "^rc"|awk '{print $2}' |xargs aptitude -y purge dpkg是Debian P ...

  2. Poj 1125 Stockbroker Grapevine(Floyd算法求结点对的最短路径问题)

    一.Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a ...

  3. Excel特殊格式的列

    1.根据前两列显示天时分格式,算出所需时间列的内容=DAY(O2-N2)&"天"&HOUR(O2-N2)&"小时"&MINUTE ...

  4. 【转】 Pro Android学习笔记(七二):HTTP服务(6):HttpURLConnection

    目录(?)[-] Http Get的使用方式 基础小例子 Cookie的使用 重定向 HTTP POST的小例子 基础小例子 文章转载只能用于非商业性质,且不能带有虚拟货币.积分.注册等附加条件,转载 ...

  5. Date---String is 合法的date 方法---

    package com.etc.jichu; import java.text.SimpleDateFormat; public class IsDate { public static boolea ...

  6. css菜鸟学习之block,inline和inline-block概念和区别

    block,inline和inline-block概念和区别   总体概念 block和inline这两个概念是简略的说法,完整确切的说应该是 block-level elements (块级元素) ...

  7. C#自定义控件 绘制框

    上几张测试的 效果 虽然全是用.net 的绘图库画的,但是手动双缓冲,不会闪烁,感觉还不错,源码开放了,喜欢的拿去扩展吧; 用于撤销的存放图像的数据结构我设置为10个,怕是内存崩了,我看mspaint ...

  8. python 基础 列表 小例子

    存主机ip到列表 host_list=[] netip='192.168.1' for hostip in range(1,254): ip = netip +str(hostip) host_lis ...

  9. 学习Web前端的好网站推荐

    说明:将其他技术技术网址也搜藏到下面,与君共同进步 1.Jquery专题 http://kb.cnblogs.com/zt/jquery/ 2.Entity Framwork专题 http://kb. ...

  10. (Python编程)Pickle对象

    Programming Python, 3rd Edition 翻译 最新版本见:http://wiki.woodpecker.org.cn/moin/PP3eD 19.4. Pickled Obje ...