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

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.

 
解决思路

  对于这道题最简单的方法我们可以使用暴力破解法,尝试所有可能的搜索字串,然后得出最大的不重复字串,但是这种解法复杂度太高,遇到比较长的字符串时,可能直接TimeOut了,所以尝试有没有其他解法。
  另一种解法就是我们通过设置一个标志量来表示当前从哪一个位置的字串开始计算的和一个字典辅助空间来记录字符出现的位置,然后从头到尾遍历字串,先判断字符串是否存在字典中并且当前的下标需要大于等于标志量,满足的话取出最大的具体,然后设置当前具体,并且将标志量移动到字典中第一个重复出现的字符串的下一位。然后继续执行。
 
复杂度

  时间复杂度为O(n), 空间复杂度为O(n)(字典存储所耗费的空间较大,也估计为O())
 
图示步骤:

       
                                     
 
代码:

 class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if len(s) < :
return if len(s) == else
index, max_count = , 0 # 设置标志量和最大长不重复字段的数目
tem_dict, count = {}, 0 # 设置辅助空间字典和当前的技术器
for i, char in enumerate(s): # 冲头开始遍历
if char in tem_dict and tem_dict[char] >= index: # 如果当前字符在字典中并且字典中的下标大于等于标志量下标(标志量表示从哪一个字符开始计算的)
max_count =max(count,max_count) # 求出最大的数
count = i - tem_dict[char] # 算出第一个出现重复的字符串当第二次出现时的距离数。
index = tem_dict[char]+1                  # 将标志量设置到第一次出现重复字符串的下一个。
else:
count += 1 # 无重复字符出现,计数加1
tem_dict[char] = i # 记录当前字符串下标
return max(count, max_count) # 返回最大的距离数

【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

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

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  4. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

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

  5. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

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

  6. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

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

  7. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  8. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

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

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

随机推荐

  1. 1. Mysql数据库的安装

    1. Mysql数据库的安装 (1)打开Mysql安装软件,同意相关协议进入下一步安装,在选择安装类型中选择[自定义]进入下一步安装. (2)选择安装的组件信息. (3)服务器软件安装目录 (4)数据 ...

  2. DOM常用事件绑定方式与实例

    一.常用的事件 onclick 点击事件 模态框实例 <input type="button" id="b1" style="width:50p ...

  3. lamp环境配置

     一.配置虚拟域名   1.为了模拟DNS,在本地hosts文件中设置一下  2.模拟三个项目  3.在apache中配置虚拟主机 去到apache的sites-available目录里复制三次def ...

  4. redis(二)--用Redis作MySQL数据库缓存

    用Redis作MySQL数据库缓存,必须解决2个问题.首先,应该确定用何种数据结构存储来自mysql的数据:在确定数据结构之后,还要考虑用什么标识作为该数据结构的键. 直观上看,Mysql中的数据都是 ...

  5. day10 十 函数、形参和实参

    一.形参和实参 1.形参:在函数定义()中出现的参数形参就是拷贝实参的值,随着函数的调用才产生,随着函数调用结束而销毁 def fn(a, b, c): print(a) print(b) print ...

  6. NFS使用autofs自动挂载

    NFS自动挂载设置在/etc/fstab和/etc/rc.local可能挂载不成功,假如是服务端NFS宕机还可能导致客户端无法启动,可以使用autofs实现自动挂载 安装autofs yum -y i ...

  7. linux shutdown命令以及参数详解

    在说shutdown命令之前 先说一下sync命令: sync:将内存中尚未写入硬盘的数据写入硬盘 因为linux为了保证数据读写速度,把常用的数据放在内存中,不会立即写入硬盘,如果有不当关机,这些数 ...

  8. 设置shell脚本静默方式输入密码方法

    stty命令是一个终端处理工具.我们可以通过它来实现静默方式输入密码,脚本如下 #!/bin/sh echo –e “enter password:” stty –echo               ...

  9. ionic中数据进行操作后,需要直接显示改变后的数据,数据刷新

    数据分页是通过使用下拉加载,查询sqlite本地数据的数据 <ion-refresher (ionRefresh)="doTest($event)"> <ion- ...

  10. el表达式获取对象属性值 返回值类型

    实现 数字页码时 遇到的一个问题. 后端servlet 在request.setAttribute("page",page); page 为pagebean的实例对象,pagebe ...