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

问题详解:

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

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

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

举例:

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. 线程(Thread)

    package cn.gouzao.demo3; public class ThreadDemo extends Thread{ public void run(){ for(int i=0;i< ...

  2. 25 个常用的 Linux iptables 规则

    # 1. 删除所有现有规则 iptables -F   # 2. 设置默认的 chain 策略 iptables -P INPUT DROP iptables -P FORWARD DROP ipta ...

  3. 简洁架构的思想,基于go实现

    https://manuel.kiessling.net/2012/09/28/applying-the-clean-architecture-to-go-applications/ 从 Clean- ...

  4. Java NIO系列教程(八)JDK AIO编程

    目录: Reactor(反应堆)和Proactor(前摄器) <I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor> <[转]第8章 前摄器(Proa ...

  5. NSGA-II入门C1

    NSGA-II入门C1 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献1 参考文献2 白话多目标 多目标中的目标是个瓦特? 多目标即是优化问题中的优化目标在3个及以上,一般这些优化的 ...

  6. JAVA核心技术I---JAVA基础知识(Jar文件导入导出)

    一:Jar初识 (一)定义 同c++中的DLL一样 jar文件,一种扩展名为jar的文件,是Java所特有的一种文件格式,用于可执行程序文件的传播. jar文件实际上是一组class文件的压缩包 (二 ...

  7. python 小数据池 is和 == 编码解码

    ########################总结######################### 今日主要内容 1. 小数据池, id() 小数据池针对的是: int, str, bool 在p ...

  8. HDU 1052(田忌赛马 贪心)

    题意是田忌赛马的背景,双方各有n匹马,下面两行分别是田忌和齐王每匹马的速度,要求输出田忌最大的净胜场数*每场的赌金200. 开始的时候想对双方的马匹速度排序,然后比较最快的马,能胜则胜,否则用最慢的马 ...

  9. js监听键盘事件

    用JS监听键盘按下事件(keydown event)   1.监听全局键盘按下事件,例如监听全局回车事件 1 $(document).keydown(function(event){ 2 if(eve ...

  10. 解析/proc/net/dev

    https://stackoverflow.com/questions/1052589/how-can-i-parse-the-output-of-proc-net-dev-into-keyvalue ...