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

问题详解:

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

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

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

举例:

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. Vue(基础六)_vue-router

    一.前言 本文主要涉及: 1.传统方式路由的实现                                     2.使用vue-router                         ...

  2. [USACO07NOV] Milking Time

    题目链接 动态规划转化成 DAG 然后拓扑求解的思路 虽然很简单不过感觉这个新思路会很有用! 如果两个事件互不影响并且有先后关系,就可以连一条有向边,跑最长路可以得到最后的最优解 实际上这还是个背包… ...

  3. python对象初始化

    当python对象被创建以后,需要将对象进行初始化.Python有一个构造函数和一个初始化函数: 1.构造函数__new__,只接受一个参数,即类本身(它会在对象被构造之前调用,所以这里也就没有sel ...

  4. okhttp添加自定义cookie

        package cn.x.request; import java.util.ArrayList; import java.util.HashMap; import java.util.Lis ...

  5. python金融反欺诈-项目实战

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  6. CentOS 安装最新版本 Git

    查看默认 yum 源的 git版本 # 安装 yum install -y git # 查看版本 git version # git version 1.8.3.1 参看官网,CentOS 安装新版本 ...

  7. 【转】C语言中的符号优先级

    转自: http://blog.csdn.net/huangblog/article/details/8271791 虽然在日常使用中,添加括号来明确规定运算符优先级是一种常识,但毕竟学校考试就喜欢考 ...

  8. Linux系统中errno对应的中文意思 errno.h

    /usr/include/asm/errno.h #define EPERM 1 /* Operation not permitted */操作不允许 #define ENOENT 2 /* No s ...

  9. HDU 1026(迷宫 BFS+打印)

    题意是要穿过一个迷宫并且将每一步打印出来. 用宽搜的方法找到路径,在 vis 中存一下方向,只是这题被看到的一种不太对的运算符重载坑了很久...... 代码如下: #include <bits/ ...

  10. stat/lstat函数使用

    1. 进程虚拟地址空间 2. stat函数 获取文件信息 #include <sys/types.h> #include <sys/stat.h> #include <u ...