Given a string, find the length of the longest substring without repeating characters.

Examples:

Given "abcabcbb", the answer is "abc", which the length is 3.

Given "bbbbb", the answer is "b", with the length of 1.

Given "pwwkew", 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

首先我解这个题的思路是依次找到一个字符串的所有字串,和已存的LongestSubstring比较长度,如大于,替换,直至找到所有的字串。

我的这个程序可能还有问题,提交后显示为Time Limit Exceeded(超时),不过我有时间会改下它,找到错误。

 class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
longestSubstring = ''
strLength = len(s)
longestSubstringLength = 0
for i in range(strLength):
subString = ''
str = s[i:strLength]
for j in range(len(str)):
if str[j] not in subString:
subString += str[j]
if j == len(str)-1:
substringLength = len(subString)
if substringLength>longestSubstringLength:
longestSubstringLength = substringLength
longestSubstring = subString
else:
substringLength = len(subString)
if substringLength>longestSubstringLength:
longestSubstringLength = substringLength
longestSubstring = subString
break
return longestSubstringLength

在网上我找到了另外的对于Python特别方便的解法:开一个字典记录字符串中的字符和它的索引,left用来记录当前字符最新出现的地方。

 class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
left = 0
d = {}
for i,ch in enumerate(s):
if ch in d and d[ch] >= left:
left = d[ch] + 1
d[ch] = i
res = max(res,i - left + 1)
return res

Leetcode3:Longest Substring Without Repeating Characters@Python的更多相关文章

  1. LeetCode3 Longest Substring Without Repeating Characters

    题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  2. 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)

    Question: Given a string, find the length of the longest substring without repeating characters. Exa ...

  3. 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters

    问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...

  4. leetcode Longest Substring Without Repeating Characters python

    class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtyp ...

  5. Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串

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

  6. LeetCode3:Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  7. [LeetCode][Python]Longest Substring Without Repeating Characters

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...

  8. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  9. [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

随机推荐

  1. 关于python如何简单跳出多层循环

    上述代码的逻辑是,在跳出子循环之前定义一个变量flag为Ture,第一层循环跳出之后,如果要跳出第二个循环,直接调用变量flag,可以直接跳出第二层循环.这里要注意的是缩进,不然会出错误. 如果是多层 ...

  2. 一般处理程序上传文件(html表单上传、aspx页面上传)

    html 表单上传文件        一般处理程序由于没有 apsx 页面的整个模型和控件的创建周期,而比较有效率.这里写一个用 html 表单进行文件上传的示例.        1. 表单元素选用 ...

  3. 性能测试工具 转自https://yq.aliyun.com/articles/35149?spm=5176.100239.blogcont35147.8.rsow6k

    摘要: 继续这个系列的学习,这一节重点介绍目前流行的性能测试工具以及如何选择适合项目的工具.在此之前,我已经对性能测试工具的原理与架构做了分析. http://www.cnblogs.com/fnng ...

  4. 显示intent和隐示intent有什么区别

    显式Intent定义:对于明确指出了目标组件名称的Intent,我们称之为显式Intent. 隐式Intent定义:对于没有明确指出目标组件名称的Intent,则称之为隐式Intent. 说明:And ...

  5. 为模版设计师而生的Twig(下)-Twig使用指南

    原文地址:http://my.oschina.net/veekit/blog/276800 12. 模板继承 Twig最强大的部分是模板继承.模板继承允许你建立一个基本的"骨架"模 ...

  6. [zz]Maya C++ API Programming Tips

    Maya C++ API Programming Tips source : http://wanochoi.com/?page_id=1588 How to handle the multiple ...

  7. NC营改增

    收票 select * from jzinv_receive  where vinvno='04888118' 1045select * from bd_corp where pk_corp='104 ...

  8. 安装lxml时gcc: internal compiler error: Killed (program cc1)的解决方法

    在安装lxml时出现如下错误 gcc: internal compiler error: Killed (program cc1) 通过查看dmesg发现下述错误信息[2517343.500178] ...

  9. 数据挖掘之KNN算法(C#实现)

    在十大经典数据挖掘算法中,KNN算法算得上是最为简单的一种.该算法是一种惰性学习法(lazy learner),与决策树.朴素贝叶斯这些急切学习法(eager learner)有所区别.惰性学习法仅仅 ...

  10. 使用VS2010在Coding.net上进行代码托管

    网上有VS2010和Github结合使用办法,但是Github在国内使用太慢,本文使用相同的配置方法稍作改动让VS2010代码托管在coding.net平台上.由于只是稍做记录让自己不会遗忘,所以叙述 ...