LeetCode 3. longest characters & 切片
Longest Substring Without Repeating Characters
找无重复的最长子串
第1次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
i=0
for k,c in enumerate(s):
# c在之前出现过多少次
#print(i)
#print(s[i:k+1],s[i:k+1].count(c),k-i)
if s[i:k+1].count(c) > 1 :
# 最长赋值
if (k-i)>maxLen:
maxLen = k-i
# 重新计算的切片起点
i=k
return maxLen
if __name__ == "__main__":
sl=[
'abcabcbb','bbbbb','pwwkew','','c','au'
];
for s in sl:
print(Solution().lengthOfLongestSubstring(s))
print("end-------------")
Wrong Answer:
Input:
"c"
Output:
0
Expected:
1
没有重复的时候忘了赋值了,for之后如果还未0就计算长度
第2次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
i=0
for k,c in enumerate(s):
# c在之前出现过多少次
#print(i)
print(s[i:k+1],s[i:k+1].count(c),k-i)
if s[i:k+1].count(c) > 1 :
# 最长赋值
if (k-i)>maxLen:
maxLen = k-i
# 重新计算的切片起点
i=k
if maxLen==0:
maxLen=len(s)
return maxLen
Wrong Answer:
Input:
"aab"
Output:
1
Expected:
2
想了一阵,发现我的逻辑有问题,for每次都应该计算长度,有重复则计算切片-1的,无重复则计算切片长度。看测试数据感觉没漏洞了,提交下:
第3次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
i=0
# 最后的切片
sp=[]
for k,c in enumerate(s):
# c在之前出现过多少次
#print(i)
sp=s[i:k+1]
print(sp,sp.count(c),k-i)
# 这里分两种,1中有重复则计算之前的长度,无重复则计算现在长度
if sp.count(c) > 1 :
# 最长赋值
if (k-i)>maxLen:
maxLen = k-i
# 重新计算的切片起点
i=k
else:
if len(sp)>maxLen:
maxLen = len(sp)
return maxLen
Wrong Answer:
Input:
"dvdf"
Output:
2
Expected:
3
再次推翻了之前的想法,不应该直接重复就再次计算的,应该重复了,从起点+1切片再次开始
第4次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
# 最后的字符
lastChar=None
# 在处理的切片
sp=[]
# 切片起点
i=0
# 当前下标
k=0
while k!=len(s[i:]):
c=s[i:][k]
sp=s[i:i+k+1]
#print(sp,c,sp.count(c),"sp:",len(sp),end=" len:")
# 这里分两种,1中有重复则计算之前的长度并且起点+1切片重新开始for。无重复则计算现在长度
if sp.count(c) > 1 :
findLen=len(sp)-1
# 重新计算的切片起点
i+=1
k=0
else:
findLen=len(sp)
k+=1
if findLen>maxLen:
maxLen=findLen
#print(maxLen)
return maxLen
Time Limit Exceeded:
Last executed input:
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~
......
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ abcdefghijklmnopqrstuvwxyzABCD"
稍微优化一下切片的计算次数
第5次提交
class Solution:
def lengthOfLongestSubstring(self,s):
"""
type s: str
rtype: int
"""
maxLen=0
# 最后的字符
lastChar=None
# 在处理的切片
sp=[]
# 切片起点
i=0
# 当前下标
k=0
lists=s[i:]
lens=len(lists)
while k!=lens:
c=lists[k]
sp=lists[:k+1]
#print(sp,c,sp.count(c),"sp:",len(sp),end=" len:")
# 这里分两种,1中有重复则计算之前的长度并且起点+1切片重新开始for。无重复则计算现在长度
if sp.count(c) > 1 :
findLen=len(sp)-1
# 重新计算的切片起点
i+=1
lists=s[i:]
lens=len(lists)
k=0
else:
findLen=len(sp)
k+=1
if findLen>maxLen:
maxLen=findLen
#print(maxLen)
return maxLen
总结:思考不够全面。
LeetCode 3. longest characters & 切片的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [leetcode]340. Longest Substring with At Most K Distinct Characters至多包含K种字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] 340. Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- 【LeetCode】Longest Word in Dictionary through Deleting 解题报告
[LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法
LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法)
Leetcode 5. Longest Palindromic Substring(最长回文子串, Manacher算法) Given a string s, find the longest pal ...
随机推荐
- Spring Cloud(Dalston.SR5)--Feign 声明式REST客户端
Spring Cloud 对 Feign 进行了封装,集成了 Ribbon 并结合 Eureka 可以实现客户端的负载均衡,Spring Cloud 实现的 Feign 客户端类名为 LoadBala ...
- LB+nginx+tomcat7集群模式下的https请求重定向(redirect)后变成http的解决方案
0. 环境信息 Linux:Linux i-8emt1zr1 2.6.32-573.el6.x86_64 #1 SMP Wed Jul 1 18:23:37 EDT 2015 x86_64 x86_6 ...
- golang语言并发与并行——goroutine和channel的详细理解(一) 转发自https://blog.csdn.net/skh2015java/article/details/60330785
如果不是我对真正并行的线程的追求,就不会认识到Go有多么的迷人. Go语言从语言层面上就支持了并发,这与其他语言大不一样,不像以前我们要用Thread库 来新建线程,还要用线程安全的队列库来共享数据. ...
- 写了一个hiero检查任务渲染结果的脚本
基本思路是写了一个时间判断函数(postSequence_check)来对比transcode任务提交时间和目标文件夹内文件的修改时间来确定渲染是否成功执行,然后通过Hiero提供的postSeque ...
- 开始使用Chronograf(官方说明)
地址:https://docs.influxdata.com/chronograf/v1.6/introduction/getting-started/ 开始使用Chronograf 在本页面 入门概 ...
- jenkins 邮件配置 二 ***
Jenkins 有两种邮件通知方式: 1.Jenkins自带的“E-mail Notification” 2.插件:Extended E-mail Notification,是可编辑的邮件配置方式. ...
- JAVA多线程之中断机制(stop()、interrupted()、isInterrupted())
一,介绍 本文记录JAVA多线程中的中断机制的一些知识点.主要是stop方法.interrupted()与isInterrupted()方法的区别,并从源代码的实现上进行简单分析. JAVA中有3种方 ...
- IDEA创建一个Mybatis逆向工程
Mybatis逆向工程简介: MyBatis Generator (MBG) 是一个Mybatis的代码生成器 MyBatis 和 iBATIS. 他可以生成Mybatis各个版本的代码,和iBATI ...
- ALGO-151_蓝桥杯_算法训练_6-2递归求二进制表示位数
记: 进制转换 AC代码: #include <stdio.h> #define K 2 int main(void) { ; scanf("%d",&n); ...
- STL进阶--相等 vs 等价 (Equality vs Equivalence)
相等性 vs 等价性 问题: 以下两个find的结果分别指向什么? class Lsb_less { public: bool operator()(int x, int y) { return (x ...