Leetcode3:Longest Substring Without Repeating Characters@Python
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的更多相关文章
- LeetCode3 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)
Question: Given a string, find the length of the longest substring without repeating characters. Exa ...
- 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- leetcode Longest Substring Without Repeating Characters python
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtyp ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- C语言字符串操作总结大全(超详细)
本篇文章是对C语言字符串操作进行了详细的总结分析,需要的朋友参考下 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat( ...
- How to create/restore a slave using GTID replication in MySQL 5.6
MySQL 5.6 is GA! Now we have new things to play with and in my personal opinion the most interesting ...
- zeromq:c,c++,golang及nodejs使用
官网:www.zeromq.org 消息队列比较:http://www.cnblogs.com/charlesblc/p/6058799.html zeromq的一些观点:http://www.cnb ...
- C#对象克隆介绍
浅拷贝和深拷贝 有两种对象克隆的方法:浅拷贝和深拷贝.浅拷贝只是复制引用,而不会复制引用的对象.深拷贝会复制引用的对象. 因此,原始对象中的引用和浅拷贝对象中的同一个引用都指向同一个对象.而深拷贝的对 ...
- 转载:scala中:: , +:, :+, :::, ++的区别
原文链接:https://segmentfault.com/a/1190000005083578 初学Scala的人都会被Seq的各种操作符所confuse.下面简单列举一下各个Seq操作符的区别. ...
- delete file by bat
@echo off set logFile=AmazonDeleteFiles.log set Feeds="E:\AmazonProject\AmazonListing\AmazonLis ...
- [I2C]I2C架构分析
转自:http://blog.csdn.net/wangpengqi/article/details/17711165 1. I2C 概述 I2C是philips提出的外设总线. I2C只有两条线,一 ...
- OAF_开发系列16_实现OAF与XML Publisher整合
http://wenku.baidu.com/link?url=y2SFKHP5qqn4bl_iNeqLGjXsTvhyFuhkMraIbWZdTXbzcv0vTefrZFFBDWie0cAAKuTw ...
- oracle补丁升级
PSU的全称是Patch Set Update,Oracle对于其产品每个季度发行一次的补丁包,包含了bug的修复.Oracle选取被用户下载数量多,且被验证过具有较低风险的补丁放入到每个季度的PSU ...
- 检测Linux VPS是Xen、OpenVZ还是KVM真假方法
如果大家对自己购买和使用的VPS需要检测是否为真的Xen,我们可以用如下方法进行测试.比较专业的就是用virt-what脚本进行检测.检测Linux VPS是Xen.OpenVZ还是KVM真假方法方法 ...