Longest Substring Without Repeating Characters---LeetCode进阶路③
- 题目描述
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.
- 思路分析
输出最大无重复字符的子字符串长度,很常规的数据结构题目。
最粗暴的BF算法就可以解决,当然时间复杂度会是O(n^2)这样的闹心样子。
小陌想到的方法是,建立数组存储字符串,从头开始捋,遇到重复的字符则从下一个元素开始下一轮的子字符串寻找,即每个子字符串从相应起点开始到下一个有重复字符位置。
小陌特别想分享下被圈粉的大神思路 :用HashSet维护当前字符串,左右两端一起搞。
例如我们从左边先开始,只要没有出现重复字符就继续左移;一旦出现重复字符,就换一条路,从右边开始。中间所忽略的那些,它们是有重复或者更短,值得“忽略”。而在左右开弓的情况下,至多对元素访问一遍,时间复杂度仅为O(n)。
大神的思路阐述:“基本思路是维护一个窗口,每次关注窗口中的字符串,在每次判断中,左窗口和右窗口选择其一向前移动。同样是维护一个HashSet, 正常情况下移动右窗口,如果没有出现重复则继续移动右窗口,如果发现重复字符,则说明当前窗口中的串已经不满足要求,继续移动有窗口不可能得到更好的结果,此时移动左窗口,直到不再有重复字符为止,中间跳过的这些串中不会有更好的结果,因为他们不是重复就是更短。”
- 源码附录:
1.小陌的方法

class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
int[] count = new int[256];
int start = 0;
int end = 1;
int longest = 1;
Arrays.fill(count,-1);
count[s.charAt(0)] = 0;
while(end < s.length()){
if(count[s.charAt(end)] >= start){
start = count[s.charAt(end)] + 1;
}
longest = Math.max(longest,end - start + 1);
count[s.charAt(end)] = end;
end ++;
}
return longest;
}
}
2.大神的方法 (大神地址https://blog.csdn.net/linhuanmars/article/details/19949159)
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s == null || s.length() == 0){
return 0;
}
HashSet<Character> hs = new HashSet<Character>();
int start = 0;
int end = 0;
int longest = 0;
while(end < s.length()){
if(hs.contains(s.charAt(end))){
longest = Math.max(longest,end - start);
while(s.charAt(start) != s.charAt(end)){
hs.remove(s.charAt(start));
start ++;
}
start ++;
}
else{
hs.add(s.charAt(end));
}
end ++;
}
longest = Math.max(longest,end - start);
return longest;
}
}
Longest Substring Without Repeating Characters---LeetCode进阶路③的更多相关文章
- Longest Substring Without Repeating Characters leetcode java
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters -- LeetCode
原题链接: http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ 这道题用的方法是在LeetC ...
- [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
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
随机推荐
- FastAPI路由:微服务架构下的路由艺术与工程实践 🌐
title: FastAPI路由专家课:微服务架构下的路由艺术与工程实践 date: 2025/3/4 updated: 2025/3/4 author: cmdragon excerpt: 用API ...
- 关于vue,npm,webpack,nod的一点心得
玩前端,换不到一颗米,纯粹是基于兴趣(我也希望能换到米,但真到那一天,说不定兴趣就没了,^_^),感觉玩这个能带来快乐,仅此而已. 最近,又来了点兴趣,读了读<vue实战>,自然免不了再次 ...
- go语言实现终端里的倒计时
最近在更新系统的时候发现pacman的命令行界面变了,我有很久没更新过设备上的Linux系统了,所以啥时候变的不好说.但这一变化成功勾起了我的好奇心.新版的更新进度界面如下: 新的更新进度界面能同时显 ...
- windows c++共享内存
#include <iostream> #include <Windows.h> #include <string> int main() { const wcha ...
- Web前端入门第4问:HTML、CSS、JavaScript 的作用分别是什么?
HTML.CSS.JavaScript 的核心作用 HTML:网页的骨架 功能:定义页面的内容结构(如按钮.表格.图片). 示例:<button>提交</button> 创建一 ...
- 注册全局图标ts写法
https://element-plus.gitee.io/zh-CN/component/icon.html#使用图标 仓库地址:https://github.com/Megasu/element- ...
- linux xxx is not in the sudoers file. This incident will be reported.
前言 linux 报错:xxx is not in the sudoers file. This incident will be reported. 这意味着用户 xxx 没有在 sudoers 文 ...
- NumPy学习9
今天学习了NumPy排序和搜索功能 17, NumPy排序和搜索功能 numpy_test9.py : import numpy as np ''' 17, NumPy排序和搜索功能 NumPy 提供 ...
- Joker 可视化开发平台全局方法使用指南
在 Joker 可视化开发平台中,全局方法是实现公共业务逻辑的有力工具,它能跨越组件和页面文件的界限,让开发者快速调用,显著提升开发效率.下面将详细介绍全局方法在平台中的使用方式. 一.全局方法的定义 ...
- 如何在linux中查看cpu信息、机器硬件型号
# cat /proc/cpuinfo | grep name | cut -f2 -d: | uniq -c 8 Intel(R) Xeon(R) CPU E5410 @ 2.33GHz (看到有8 ...