LeetCode(3):无重复字符的最大子串
本内容是LeetCode第三道题目:无重复字符的最大子串
# -*- coding: utf-8 -*-
"""
Created on Sun Mar 10 20:14:53 2019 @author: Administrator
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.
""" class Solution:
def lengthOfLongestSubstring(self, s: str) -> int:
str_dict = {} #python中的字典类似于hashmap一样
start = 0 #定义第一次出现该元素在字典中的位置
maxLength = 0 #子串的最大的长度
one_max = 0 #一次循环中子串最长的长度
for i in range(len(s)):
#只有当前的字符在字典中并且其位置在最大长子串的起始位置开始
if s[i] in str_dict and str_dict[s[i]] >= start:
start = str_dict[s[i]] + 1 #起始位置从当前出现的下一个位置
one_max = i - start +1 #本次最大的长度为从start到i之间的子串
str_dict[s[i]] = i #将其存入字典中
maxLength = max(one_max,maxLength) #求最大长度
'''
此处应该加上返回最长的子串
'''
return maxLength if __name__ == '__main__':
s1 = 'abcabcbb'
s2 = 'abcdcdabcdadcadcdadb'
solution = Solution()
answer1 = solution.lengthOfLongestSubstring(s1)
answer2 = solution.lengthOfLongestSubstring(s2) print('MaxLength of abcabcbb:',answer1)
print('MaxLength of abcdcdabcdadcadcdadb:',answer2)
LeetCode(3):无重复字符的最大子串的更多相关文章
- Leetcode(三)无重复字符的最长子串
3. 无重复字符的最长子串 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...
- 【LeetCode】无重复字符的最长子串【滑动窗口法】
给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...
- [LeetCode] 3. 无重复字符的最长子串
题目链接:(https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) 题目描述: 给定一个字符 ...
- 【leetcode 3. 无重复字符的最长子串】解题报告
思路:滑动窗口的思想 方法一:滑动窗口 int lengthOfLongestSubstring(string s) { /* 控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是 ...
- LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters
题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...
- Leetcode——3. 无重复字符的最长子串
难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一 ...
- 力扣Leetcode 3. 无重复字符的最长子串
无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...
- [LeetCode]3. 无重复字符的最长子串(滑动窗口)
题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...
- [LeetCode]3.无重复字符的最长子串(Java)
原题地址: longest-substring-without-repeating-characters/submissions 题目描述: 示例 1: 输入: s = "pwwkew&qu ...
随机推荐
- 用JavaMail通过QQ邮箱来发送邮件(第一篇博客,备忘)
1.先启用QQ邮箱里POP3/STMP服务:生成授权码 2.导入mail.jar包(不要用太古董的技术,你懂得) 3.注意要在代码里加上开启SSL加密的代码 4.直接上代码 import java.u ...
- android4.4之后的HttpUrlConnection的实现是基于okhttp
首先看HttpUrlConnection使用 URL url = new URL("http://www.baidu.com"); HttpURLConnection urlCon ...
- vue 学习笔记—Resource
1.首先是引入 或者用npm来安装 cnpm i vue-resource --save(推荐) 3.提供的api 关于请求写法: get(){ // get请求 this.$http.get( ...
- python基础-----类和实例
在python中,首字母大写的名称指的是类,这个类定义中括号的内容是空的. 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板而实例是根据类创建出来的一个个具体 ...
- poi读取Excel模板并修改模板内容与动态的增加行
有时候我们可能遇到相当复杂的excel,比如表头的合并等操作,一种简单的方式就是直接代码合并(浪费时间),另一种就是写好模板,动态的向模板中增加行和修改指定单元格数据. 1.一个简单的根据模板shee ...
- [python] 基础工具介绍好文推荐
Github上有个哥们写的,还不错,mark一下: https://github.com/lijin-THU/notes-python/blob/master/index.ipynb 相对全面的介绍了 ...
- 【上载虚拟机】XX是一个Workstations 12.X虚拟机,不受XX支持。请选择其他主机或将虚拟机的硬件版本更改为以下选项之一。
背景 由于搭建集群时,要使用Ubuntu系统,还有一点就是咱们使用的Ubuntu系统是定制的,但是它是一个虚拟机,需要通过VMware Workstations这款软件,把咱们定制好的Ubuntu系统 ...
- 2018 Multi-University Training Contest 1 杭电多校第一场
抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001 Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...
- valgrind简介以及在ARM上交叉编译运行【转】
转自:https://blog.csdn.net/dengcanjun6/article/details/54958359 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blo ...
- python3+selenium入门12-警告框处理
在WebDriver中要处理JS生成的alert.confirm以及prompt,需要使用到switch_to_alert()定位到alert/confirm/prompt,然后再使用text.acc ...