本内容是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):无重复字符的最大子串的更多相关文章

  1. Leetcode(三)无重复字符的最长子串

    3. 无重复字符的最长子串 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...

  2. 【LeetCode】无重复字符的最长子串【滑动窗口法】

    给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc&quo ...

  3. [LeetCode] 3. 无重复字符的最长子串

    题目链接:(https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/) 题目描述: 给定一个字符 ...

  4. 【leetcode 3. 无重复字符的最长子串】解题报告

    思路:滑动窗口的思想 方法一:滑动窗口 int lengthOfLongestSubstring(string s) { /* 控制一个滑动窗口,窗口内的字符都是不重复的,通过set可以做到判断字符是 ...

  5. LeetCode 3: 无重复字符的最长子串 Longest Substring Without Repeating Characters

    题目: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. Given a string, find the length of the longest substring withou ...

  6. Leetcode——3. 无重复字符的最长子串

    难度: 中等 题目 Given a string, find the length of the longest substring without repeating characters. 给定一 ...

  7. 力扣Leetcode 3. 无重复字符的最长子串

    无重复字符的最长子串 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串 ...

  8. [LeetCode]3. 无重复字符的最长子串(滑动窗口)

    题目 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 "abc ...

  9. [LeetCode]3.无重复字符的最长子串(Java)

    原题地址: longest-substring-without-repeating-characters/submissions 题目描述: 示例 1: 输入: s = "pwwkew&qu ...

随机推荐

  1. JQuery属性选择

    css: JQuery基本选择器: 解释 层叠选择器:

  2. Web三层-UI/BLL/DAL/MODEL

    2013传智播客视频\视频\2013-05-28-EF\视频 创建4个程序集,添加引用,model添加映射, P01UI表现层--BLL+MODELP02BLL业务层--DAL+MODELP03DAL ...

  3. DeepLearning.ai-Week3-Autonomous driving-Car detection

    1 - Import Packages import argparse import os import matplotlib.pyplot as plt from matplotlib.pyplot ...

  4. python编程 之 PyMysql包接口,python中如何使用数据库

    1,环境介绍 要求:使用数据库TESTDB.EMPLOYMENT EMPLOYEE表字段为 FIRST_NAME, LAST_NAME, AGE, SEX 和 INCOME. 2,基本用法: impo ...

  5. awk基本用法

    1  简介 awk实质是一种编程语言,基本作用在于查找和替换. 2  基本用法 有文本名称为:awk.txt 内容为: john.wang male 30 021-111111 lucy.yang f ...

  6. android listView多层嵌套listView显示不全问题

    最近在做项目,需要用到listVIew多层嵌套listVIew的需求,先发现已下两个处理办法比较好用 第一种: public class ListViewNesting extends ListVie ...

  7. gradle记录

    set-env.bat set "JAVA_HOME=G:\jdk" set "CLASSPATH=%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\ ...

  8. 使用Office Online Server在线预览Office

    微软官方文档介绍 ⒈介绍 Office Online Server是 Office Web Apps Server 的升级版本,安装环境必须为两台Windows Server 2012 R2 或 Wi ...

  9. Wasserstein CNN: Learning Invariant Features for NIR-VIS Face Recognition

    承接上上篇博客,在其基础上,加入了Wasserstein distance和correlation prior .其他相关工作.网络细节(maxout operator).训练方式和数据处理等基本和前 ...

  10. HAProxy详解(一):HAProxy介绍【转】

    一.高性能负载均衡软件HAProxy介绍: 随着互联网业务的迅猛发展,大型电商平台和门户网站对系统的可用性和可靠性要求越来越高,高可用集群.负载均衡集群成为一种热门的系统架构解决方案.在众多的负载均衡 ...