问题简介:求给定字符串中最长的字符不重复的字符串的长度

问题详解:

给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度

注:答案必须是子字符串,不是子序列

是连续的字符不重复的字符串,不是所有不重复字符

举例:

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的更多相关文章

  1. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  2. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  3. 刷题3. Longest Substring Without Repeating Characters

    一.题目 Longest Substring Without Repeating Characters,具体请自行搜索. 这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂. 但要做到bug ...

  4. leetcode第三题Longest Substring Without Repeating Characters java

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  5. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  6. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  7. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. Leetcode第三题《Longest Substring Without Repeating Characters》

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  9. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

    题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...

  10. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

随机推荐

  1. IO以及file的一些基本方法

    IO定义:将内存的数据 持久化到设备上 这个动作称为输出 写 Output操作 把硬盘上的数据读取到内存 这个动作称为输入 读 Input 把上面这两种操作称为IO操作 File类的静态成员变量:pa ...

  2. day-03(js)

    回顾: css: 层叠样式表 作用: 渲染页面 提供工作效率,将html和样式分离 和html的整合 方式1:内联样式表 通过标签的style属性 <xxx style="...&qu ...

  3. 神经网络4_BP神经网络

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  4. 图论分支-差分约束-SPFA系统

    据说差分约束有很多种,但是我学过的只有SPFA求差分: 我们知道,例如 A-B<=C,那么这就是一个差分约束. 比如说,著名的三角形差分约束,这个大家都是知道的,什么两边之差小于第三边啦,等等等 ...

  5. Centos 6\7下yum安装rstudio-server\shiny-server

    rstudio-server安装 #wget https://download2.rstudio.org/rstudio-server-rhel-1.1.463-x86_64.rpm #yum ins ...

  6. Mac下显示网页全屏快捷键

    control+command+F mac下谷歌浏览器全屏时隐藏头部:(隐藏标签页和地址栏) command+shift+B

  7. Dubbo管控台安装(zookeeper集群)

    Dubbo管控台可以对注册到zookeeper注册中心的服务或服务消费者进行管理,但管控台是否正常对Dubbo服务没有影响,管控台也不需要高可用,因此节点部署   环境:Centos6.6.IP:10 ...

  8. golang 缓冲区的终端输入

    bufio包实现了有缓冲的I/O.它包装一个io.Reader或io.Writer接口对象,os.stdin就是实现了这个接口 package main import ( "bufio&qu ...

  9. HDU 1025(最长上升子序列)

    题意是要在两条平行线间连点,要在线不交叉的前提下尽可能多的连线,问最多能连多少条线. 现假定题中所给的是 9 组点,分别是:1—3,2—8,3—5,4—9,5—2,6—4,7—6,8—7,9—1,如图 ...

  10. HTTP status constants

    HTTP status constants value = ngx.HTTP_CONTINUE (100) (first added in the v0.9.20 release) value = n ...