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. day_6.21web框架编写

    web框架都是相同,只需把某些东西改写!就可以为自己所用! 直接实现改写,补充东西!然后,就是输入一个路径,然后执行方法!!!! 更改路由,然后调用方法就好!

  2. .NET Core HttpClient调用腾讯云对象存储Web API的"ERROR_CGI_PARAM_NO_SUCH_OP"问题

    开门见山地说一下问题的原因:调用 web api 时请求头中多了双引号,请求体中少了双引号. 腾讯云提供的对象存储(COS)C# SDK 是基于 .NET Framework 用 WebRequest ...

  3. webpack dev server 配置 启动项目报错Error: listen EADDRINUSE

    Error: listen EADDRINUSE 0.0.0.0:5601 它的意思是,端口5601被其他进程占用. 切换端口即可解决问题

  4. RabbitMQ 内存控制 硬盘控制

    RabbitMQ服务器在启动时以及abbitmqctl set_vm_memory_high_watermark fraction 执行时,会检查计算机的RAM总大小. 默认情况下下, 当 Rabbi ...

  5. arcengine实现右键菜单打开/关闭所有图层

    参考资料:  http://developer.51cto.com/art/201104/256774.htm 参照后自己做的: 关于右键菜单的几个有价值的网址: http://blog.csdn.n ...

  6. Exactly-Once 投递语义

    小结: 1.Exactly-Once 是指发送到消息系统的消息只能被消费端处理且仅处理一次,即使生产端重试消息发送导致某消息重复投递,该消息也在消费端也只被消费一次. 消息队列 RocketMQ &g ...

  7. ACM:油田(Oil Deposits,UVa 572)

    /* Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

  8. day 0314函数的进阶

    1.空间:内置空间,全局空间,局部空间. 内置空间:空间存放python解释器,为我们提供了方便的名字:input,print,str,list,tuple 三个空间的加载顺序: 内置空间>&g ...

  9. CS231n: Convolutional Neural Networks for Visual Recognition

    https://zhuanlan.zhihu.com/p/28522637 https://zhuanlan.zhihu.com/p/21930884 mark

  10. Java之旅_高级教程_URL处理

    摘自 :http://www.runoob.com/java/java-url-processing.html Java URL 处理 URL(Uniform Resource Locator)中文名 ...