刷题之路第三题--Longest Substring Without Repeating Characters
问题简介:求给定字符串中最长的字符不重复的字符串的长度
问题详解:
给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度
注:答案必须是子字符串,不是子序列
是连续的字符不重复的字符串,不是所有不重复字符
举例:
1.
输入: “abcabcbb”
输出: 3
解释: 结果是 “abc”, 长度是 3
2.
输入: “bbbbb”
输出: 1
解释: 结果是 “b”,长度是 1
3.
输入: “pwwkew”
输出: 3
解释: 结果是 “wke”,长度是 3
JAVA 实现方法一:笨方法遍历(第一次愚蠢实现不推荐)


官方实现一 : Brute Force
简介:
逐个检查所有子字符串,看它是否没有重复的字符。
算法:
写一个方法 boolean allUnique(String substring),如果子字符串中的字符都是唯一的,则返回true,否则返回false.我们可以遍历给定字符串s的所有可能的子字符串并调用函数allUnique(),如果结果是true,那么我们更新子字符串的最大长度.
现在让我们填补缺少的部分:

复杂度分析:
时间复杂度 : 两层O(n3):main()中两层遍历,方法中还有一层遍历.
空间复杂度 : O(min(n,m)):取决于字符串长度
官方实现二 : Sliding Window
滑动窗口是数组/字符串问题中常用的抽象概念。窗口是数组/字符串中的一系列元素,通常由开始和结束索引定义,即[i,j].
使用HashSet将字符存储在当前窗口[i,j]中(最初j = i)然后我们将索引jjj向右滑动,如果它不在HashSet中,我们进一步滑动j,这样做直到 s [j] 已经在HashSet中.此时,我们发现没有重复字符的子字符串的最大大小以索引i开头,为所有i执行此操作,会得到答案

复杂度分析;
时间复杂度 : O(n):一层循环
空间复杂度: O(min(m,n))
官方实现三 : Sliding Window Optimized
定义字符到其索引的映射,而不是使用一个集来判断字符是否存在。,然后我们可以在找到重复的字符时立即跳过字符.
原因是,如果s [j] 在[i,j] 的范围内具有索引j的重复,我们不会需要一点一点地增加i,我们可以跳过[i,j] 范围内的所有元素,并让i直接为j+ 1

还可以假设ASCII 128
以前的实现都没有对字符串s的字符集进行假设。
如果我们知道charset相当小,我们可以用整数数组替换Map作为直接访问表。
常用的表有:
int[26] for Letters ‘a’ - ‘z’ or ‘A’ - ‘Z’
int[128] for ASCII
int[256] for Extended ASCII

复杂度分析:
时间复杂度 : O(n).
空间复杂度(HashMap) : O(min(m,n)).
空间复杂度(Table): O(m).
小提示:
String的三种方法 indexOf(),lastIndexOf(),subString()

刷题之路第三题--Longest Substring Without Repeating Characters的更多相关文章
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- 刷题3. Longest Substring Without Repeating Characters
一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【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》
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
随机推荐
- npm总是报错:unable to verify the first certificate
今天npm install总是报错:unable to verify the first certificate(无法验证第一证书),查了一下发现 As of February 27, 2014, n ...
- 【SQL】SQL中on条件与where条件的区别
#前言 数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1.on条件是在生成临时表时 ...
- 【C#】C#获取文件夹下的所有文件
#基础知识 1.获得当前运行程序的路径 string rootPath = Directory.GetCurrentDirectory(); 2.获得该文件夹下的文件,返回类型为FileInfo st ...
- Linux记录-JMX监控Tomcat上传到falcon
1.登录测试服务器xxxxxx xxxxxx su root输入xxxx 2.先修改Tomcat的启动脚本,(linux下为catalina.sh),添加以下内容: CATALINA_OPTS=&qu ...
- Hbase记录-zookeeper部署
#官网下载二进制包解压到/usr/app下,配置/etc/profile: export ZOOKEEPER_HOME=/usr/app/zookeeper export PATH=$PATH:$ZO ...
- ruby-----render讲解
Ruby rails页面跳转代码如下: 1.render(:text => string) 2.render(:inline => string, [:type => "r ...
- Git(介绍和安装)
Git 是什么 Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的分布式版本控制系统. 与常用的版本控制工具 CVS, Subversion 等不同,它 ...
- Leetcode 136 Single Number 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number/ 题目描述Given an array of integers, every element appea ...
- mysql表基本查询
第一节 -- or # 单行注释/***多行注释*/ -- c创建数据库examCREATE DATABASE exam; USE exam; /*创建部门表*/CREATE TABLE dept( ...
- 查看swap占用情况
查看swap被占用的情况 #!/bin/bash # Get current swap usage for all running processes # Erik Ljungstrom 27/05/ ...