[LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
题目要求一个最长的无重复的子串(子串要求连续),我们可以想到使用set来操作,set可以保证内部元素全部是无重复的,这比我们使用暴力方法去遍历判断重复要优化很多。简单来说就是遍历一次字符串,用i和j来标记子串的开始和结束。每次遇到不同的就把元素加入到set中,同时子串长度+1,遇到相同的就从当前子串的开头开始删除,一直删除到重复元素的位置,比如说从i开始的第n个元素重复了,就删除掉从i到n的所有元素,这样新的不重复子串就是string[i+n+1,j+1],再和旧的长度比较出最大的,如此重复直到字符串末尾。
由于很像网络流量控制的滑动窗口协议,所以这种方法也叫滑动窗口
class Solution {
public int lengthOfLongestSubstring(String s) {
int n=s.length(),max=0;
int i=0,j=0;
HashSet<Character> set=new HashSet<Character>();
while(i<n&&j<n){
if(!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
max=Math.max(max,j-i);
}else{
set.remove(s.charAt(i++));
}
}
return max;
}
}
还可以从空间角度进行优化
当我们知道该字符集比较小的时侯,我们可以用一个整数数组作为直接访问表来替换 Map。
常用的表如下所示:
int [26] 用于字母 ‘a’ - ‘z’或 ‘A’ - ‘Z’
int [128] 用于ASCII码
int [256] 用于扩展ASCII码
public class Solution {
public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
int[] index = new int[128]; // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
i = Math.max(index[s.charAt(j)], i);
ans = Math.max(ans, j - i + 1);
index[s.charAt(j)] = j + 1;
}
return ans;
}
}
python的话可以使用列表自带的切片来维护一个不重复集合set
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
max_number = 0
number = 0
test = ''
for i in s:
if i not in test:
test += i
number += 1
else:
if number >= max_number:
max_number = number
index = test.index(i)
test = test[(index+1):] + i
number = len(test)
if number > max_number:
max_number = number
return max_number
[LeetCode]3. Longest Substring Without Repeating Characters无重复字符的最长子串的更多相关文章
- 【LeetCode】Longest Substring Without Repeating Characters(无重复字符的最长子串)
这道题是LeetCode里的第3道题. 题目描述: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: ...
- leetcode 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
一.题目大意 https://leetcode.cn/problems/longest-substring-without-repeating-characters/ 给定一个字符串 s ,请你找出其 ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 3. Longest Substring Without Repeating Characters 无重复字符的最长子串
1. 原始题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &quo ...
- [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters无重复字符的最长子串
网址:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 显然采用sliding window滑 ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- 一些奇怪的Javascript用法
阅读AngularJS时,看到一些奇怪的Javascript用法.1.(function(){ a.work=function(){} })(a) 声明一个匿名函数并执行 2. ...
- tensorflow第一个例子
import tensorflow as tf import numpy as np # create data x_data = np.random.rand(100).astype(np.floa ...
- 项目接入apm后错误报警总结
此文已由作者张磊授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 前言: 后端服务一般都有监控措施,一般可以及时发现线上错误,但是很多项目的前端却没有线上报警服务,即使有错误, ...
- JsonParse类
using System.Data; using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; using Syst ...
- Trie[字典树] 数据结构及基本操作集
#include <iostream> #include <stdio.h> #include <cstring> #include <algorithm&g ...
- 初识XML
前言 前期敲机房的时候,总是写配置文件,后来到ASP.NET学习中也遇到了XML,可是什么是XML呢,它又是用来干什么的呢?请听我细细道来. 内容 定义 (eXtensible Markup Lang ...
- 51nod1064(Bash博弈)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1067 题意:中文题诶~ 思路:直接规律就好了... 代码: ...
- CF987B High School: Become Human 数学
题意翻译 题目大意 输入一个 xxx ,一个 yyy ,求是 xyx^yxy 大还是 yxy^xyx 大. (1≤x,y≤109)(1≤x,y≤10^9)(1≤x,y≤109) 输入输出格式 输入格式 ...
- 洛谷P5159 WD与矩阵
题目背景 WD整日沉浸在矩阵中,无法自拔-- 题目描述 WD特别喜欢矩阵,尤其是\(01\)矩阵. 一天,CX给了WD一个巨大的\(n\)行\(m\)列的\(01\)矩阵,WD发现这个矩阵每行.每列的 ...
- P1089津津的储蓄计划
题目描述 津津的零花钱一直都是自己管理.每个月的月初妈妈给津津300300元钱,津津会预算这个月的花销,并且总能做到实际花销和预算的相同. 为了让津津学习如何储蓄,妈妈提出,津津可以随时把整百的钱存在 ...