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. mui---自定义页面打开的方向

    在使用MUI做APP的时候,会考虑对页面的打开方向做规定,MUI也给我们提供了很多种页面的打开方式. 具体参考: http://ask.dcloud.net.cn/question/174 MUI做A ...

  2. TOP100summit:【分享实录-猫眼电影】业务纵横捭阖背后的技术拆分与融合

    王洋:猫眼电影商品业务线技术负责人.技术专家.主导了猫眼商品供应链和交易体系从0到1的建设,并在猫眼与美团拆分.与点评电影业务融合过程中,从技术层面保障了商品业务的平稳切换,同时也是美团点评<领 ...

  3. .NET Core开发日志——结构化日志

    在.NET生态圈中,最早被广泛使用的日志库可能是派生自Java世界里的Apache log4net.而其后来者,莫过于NLog.Nlog与log4net相比,有一项较显著的优势,它支持结构化日志. 结 ...

  4. LeetCode 155 - 最小栈 - [数组模拟栈]

    题目链接:https://leetcode-cn.com/problems/min-stack/description/ 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的 ...

  5. [No0000137]字符编码详解

    摘要 本文主要介绍了字符编码的基础知识,以及常见的字符编码类型,比如ASCII,Unicode,UTF-8,ISO 8859等,以及各种编码之间的关系,同时专门解释了中文字符相关的编码标准,包括GB2 ...

  6. 【紫书】uva133 The Dole Queue 参数偷懒技巧

    题意:约瑟夫问题,从两头双向删人.N个人逆时针1~N,从1开始逆时针每数k个人出列,同时从n开始顺时针每数m个人出列.若数到同一个人,则只有一个人出列.输出每次出列的人,用逗号可开每次的数据. 题解: ...

  7. GIS软件相关安装(持续更新)

    软件安装是GIS专业的必修课,总会忘记步骤,在此汇总 1.oracle ①无法登录 管理员登录 sqlplus sys/密码 as sysdba https://www.linuxidc.com/li ...

  8. jQuery 报错,对象不支持tolowercase属性或方法

    泪流满面.<input>里id和name都不能是nodeName,否则跟jquery.js冲突 JQuery 实践问题 - toLowerCase 错误 在应用JQuery+easyui开 ...

  9. 预备作业2 :学习基础和C语言基础调查

    剑网三毒经pk心得: 看完标题的你真的没有进错,这里是博客园. 想到写这篇文章的原因一部分是自己的确没啥技能比超过90%以上的人还好,还有一部分是受到了作业提示的指引...... 如果你有类似的技能获 ...

  10. spring学习(01)之IOC

    spring学习(01)之IOC IOC:控制反转——Spring通过一种称作控制反转(IOC)的技术促进了低耦合.当应用了IOC,一个对象依赖的其它对象会通过被动的方式传递进来,而不是这个对象自己创 ...