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. 也谈.Net中间语言——破解Delphi2CS行数和时间限制

    其实我一直在研究将Delphi版的传奇2源代码使用C#实现,不过由于我并没有学习过Delphi.就只能说先试着用一些工具转换代码. 后来我在网上找到了一款软件:Delphi2CS.这款软件比较强大,虽 ...

  2. Windows 10 Install rabbitmq-server-3.6.9

    rabbitmq下载 http://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-3.6.9.exe otp插件下载 ...

  3. db2 查杀死锁进程

    db2 查杀死锁进命令 db2 get snapshot for locks on (需要snapshot的访问权限) db2 list applications db2 "force ap ...

  4. NPOI 自定义单元格背景颜色 XSSFWorkbook - Excel

    x 网上找到了,HSSFWorkbook自定义颜色的例子(讲的还挺细致的),但是XSSFWorkbook确没找到...研究了一下,坑掉了一地... NPOI.XSSF.UserModel.XSSFWo ...

  5. [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)

    github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...

  6. maven添加settings.xml使用阿里云仓库

    在~/.m2/repository目录下新建settings.xml,内容如下: <mirrors> <!-- mirror | Specifies a repository mir ...

  7. Runtime Services

    Python Runtime Services — Python 3.7.2 documentation https://docs.python.org/3/library/python.html

  8. DACLs and ACEs

    [Windows]Windows的访问控制模型 - Zplutor - 博客园 https://www.cnblogs.com/zplutor/archive/2010/01/05/1639892.h ...

  9. wpf(第一章 基础知识)

    wpf第一章基础知识:通过vs2015创建wpf程序会在引用里面多出3个核心程序集PresentationCore.PresentationFramework.WindowsBase.并且会在解决方案 ...

  10. DevExpress前端组件库

    适用于为.Net环境(也有dephi版本,其他环境未知)下 WinForm\WebForm\ASP MVC的前端扩展组件,帮助生成高度windows office统一风格的界面. 在winform下, ...