本内容是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. 【blog】Hibernate5如何设置SQLite的方言(待更新...)

    参考链接 Hibernate3.Hibernate4.Hibernate5 hibernate5连接sqlite (目前参考的是这个方法)

  2. tomcat源码之connector启动过程

    connector源码部分 构造函数 生命周期启动 启动endPoint 启动accepter 线程执行方法 SocketProcessor启动

  3. Ajax——从服务器获取各种文件

    ajax.js内容 function ajax(url,fnWin,fnFaild){ //1.创建ajax对象 var xhr = window.XMLHttpRequest ? new XMLHt ...

  4. Flask里面的cookie的基本操作

    #cookie相关操作,依赖于make_response #调用cookie依赖request模块 from flask import Flask,make_response,request #建立对 ...

  5. 论文笔记系列-Speeding Up Automatic Hyperparameter Optimization of Deep Neural Networks by Extrapolation of Learning Curves

    I. 背景介绍 1. 学习曲线(Learning Curve) 我们都知道在手工调试模型的参数的时候,我们并不会每次都等到模型迭代完后再修改超参数,而是待模型训练了一定的epoch次数后,通过观察学习 ...

  6. 遗传算法selection总结-[Fitness, Tournament, Rank Selection]

    假设个体(individual)用\(h_i\)表示,该个体的适应度(fitness)为\(Fitness(h_i)\),被选择的概率为\(P(h_i)\). 另外假设种群(population)的个 ...

  7. Jenkins远程构建

    首先在Jenkins上配置Job: 按照图片形式,仅需往对应的url发送请求即可 Shell如下: import json import urllib.parse,http.client def po ...

  8. 利用crash 分析软死锁问题【转】

    转自:https://blog.csdn.net/divlee130/article/details/47806551 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...

  9. EMOS之邮件服务器

    作者:邓聪聪 EMOS一键自动安装镜像,邮件服务器配置

  10. 利用jsoncpp将json字符串转换为Vector

    在API测试过程中经常会遇到传入参数为复杂类型,一般情况下在python下,习惯用字典来表示复杂类型.但是c++对字符串的处理是比较弱智的,一般c++里边会用vector来存储复杂类型,那么就存在转换 ...