003 Longest Substring Without Repeating Characters 最长不重复子串
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 最长不重复子串的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters 最长不重复子串
只遍历一次字符串即可求出最长不重复子串的长度. int lengthOfLongestSubstring(string s) { vector<,-); //记录字符上一次出现的位置,ASCII ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
随机推荐
- POJ1088(记忆化搜索)
经典记忆化搜索题目.当 从每个点一次进行搜索时要采用 记忆化搜索 #include"cstdio" #include"algorithm" using name ...
- HDFS中hsync方法介绍
HDFS中hsync方法介绍 原创文章,转载请注明:博客园aprogramer 原文链接:HDFS中hsync方法介绍 1. 背景介绍 HDFS在写数据务必要保证数据的一致性与持久性,从HDFS最初的 ...
- JS---script的位置
都可以,但各有千秋.放在head中:统一管理,方便维护:但浏览器会首先加载js文件,如果js文件过大,会造成页面在加载js的时候“无反应”时间过长,影响用户体验.放在body中(或放在body后):浏 ...
- CSS是什么?W3C是什么?W3C盒子与IE盒子的区别?
(1)层叠样式(Cascading Style Sheets, CSS)是用来表现HTML或XML文本样式的语言. (2)W3C推荐规范(World Wide Web Consortium,W3C ...
- 使用showInputDialog显示输入框
------------------siwuxie095 工程名:TestJOptionPane 包名:com.siwuxie095.showd ...
- 使用python已知平均数求随机数
问题描述:产生40个数,范围是363-429之间,平均值为402 思路: 1 产生一个随机数 2 使用平均数减去随机数求出第二个数,生成20组 3 将排序打乱 # -*- coding: cp936 ...
- Umbraco 的template使用的默认Model问题
Umbraco中的Template默认都继承自 Umbraco.Web.Mvc.UmbracoTemplatePage @inherits Umbraco.Web.Mvc.UmbracoTemplat ...
- 7.27实习培训日志-Oracle SQL(三)
Oracle SQL(三) 视图 特性 简单视图 复杂视图 关联的表数量 1个 1个或多个 查询中包含函数 否 是 查询中包含分组数据 否 是 允许对视图进行DML操作 是 否 CREATE [OR ...
- hdu1058
#include<iostream> #include<string> using namespace std; int const N = 4; int map[N] = { ...
- MySQL 杂项
关于MySQL mysql连接时的--prompt 和 --delimiter 参数是用来设置什么的? 设置提示符和分隔符 mysql查看创建数据库时的字符集命令? SHOW CREATE DATAB ...