【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
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(最长无重复的字串)的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
随机推荐
- 关于ie6出现的问题的原因归结
关于ie6出现的问题主要可以归结为以下几种情况把. 当然还存在各种原因,bug的情况也还有各种各样,我只是小结一下我自己经常遇到,比较有代表性的问题.会持续的更新. 1.浏览器本身存在的缺陷 比如: ...
- I - A/B
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). Input 数据的第一行是一个T,表示有T组数据. 每组数据有 ...
- Android定时执行和停止某任务
一.定义全局变量 int runCount = 0;// 全局变量,用于判断是否是第一次执行 Handler handlerCount = new Handler(); 二.创建Runnable Ru ...
- bootstrap 弹出框实现点击后打开离开后关闭
$("#PersonName").popover({ trigger: 'manual', placement: 'bottom', //title: '<div style ...
- F#周报2018年第48期
新闻 F#2018年圣诞日历 Mac上的Visual Studio 2017新版本7.7 Rider 2018.3将引入远程调试功能 Visual Studio 2017新版本15.9.3 视频及幻灯 ...
- 对iphone手机IMU的陀螺仪、加速度计、图像的时间戳做对齐处理
https://blog.csdn.net/chishuideyu/article/details/77479758 加速度计和陀螺仪的时间戳一致 ros 的message filters可以做时间同 ...
- python 给字符串加颜色
msg = '\033[41;1m字符串内容\033[0m' print(msg) # \033[41;1m起始位置 改变41数值就是改变其他颜色,.033[0m 结束位置
- Cnblog Markdown编辑器
第一次使用Cnblog Markdown编辑器 1.在博客园使用 Markdown 与 LATEX[1] 1.1.开启MarkDown编辑器 选择 Markdown 撰写博文:在博客园管理-选项中,将 ...
- [No000016A]CSS常用三种选择器
1.HTML Tag p{color:red;} 2.id #myid{color:red;} 3.class .myclass{color:red;} CSS常用文本样式属性 color font- ...
- [No0000161]IDEA初步接触
安装 参考https://blog.csdn.net/qq_35246620/article/details/61191375 安装过程全程默认(路径和快捷方式自定义,不需要下载jre): 启动后全程 ...