leetcode-03-Longest Substring Without Repeating Characters-python
来源:互联网 发布:淘宝手机卡 编辑:程序博客网 时间:2024/06/17 14:28
找出一串字符中最常不重复的字符个数。
o(n*n)的方法
class Solution(object): def lengthOfLongestSubstring(self,s): b=[] if len(s)==0: return 0 else: for index,i in enumerate(s): a=[] while( s[index] not in a ): a.append(s[index]) if index<len(s)-1: index+=1 else: break b.append(len(a)) return max(b)
o(n)的方法。
class Solution(object): def lengthOfLongestSubstring(self,s): res = 0 left = 0 d = {} for index,i in enumerate(s): if i in d and d[i]>=left: left=d[i]+1 d[i]=index res=max(res,index-left+1) return res """ :type s: str :rtype: int """
遍历字符串一遍,left指向不重复字符串的左边界。d{}中保存的是出现的不重复的字符。如果遍历的下一个字符在d{}中且出现在left右边,则left右移一位,计算这时该字符索引值index-left+1表示到该字符时最大长度。每次取上一次max和这时的index-left+1的max。
0 0
- Leetcode -- Python -- Longest Substring Without Repeating Characters
- 【Leetcode】【python】Longest Substring Without Repeating Characters
- leetcode-03-Longest Substring Without Repeating Characters-python
- LeetCode 03: Longest Substring Without Repeating Characters
- [LeetCode]3 Longest Substring Without Repeating Characters (C++,Python实现)
- LeetCode 3. Longest Substring Without Repeating Characters (C++/python)
- 3.Longest Substring Without Repeating Characters Leetcode Python
- [Leetcode]3. Longest Substring Without Repeating Characters @python
- Leetcode题解(Python): 3.Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters Leetcode Python Java
- Python leetcode #3 Longest Substring Without Repeating Characters
- [leetcode: Python]3. Longest Substring Without Repeating Characters
- Python刷题日记:LeetCode-Longest Substring Without Repeating Characters
- LeetCode 3. Longest Substring Without Repeating Characters题解python
- 【leetcode】3. Longest Substring Without Repeating Characters(Python & C++)
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- 《操作系统》实验大纲
- 《操作系统》——实验课教学进度表——12计科12班——张同光
- -bash: pod: command not found
- API级别是什么
- 《操作系统》——实验课教学进度表——13专升本班——张同光
- leetcode-03-Longest Substring Without Repeating Characters-python
- 模块(第1章)实验——编译问题(没有规则可以创建目标“modules”)
- linux 启动时,sendmail 长时间等待
- 以编译ipk包方式修改openwrt web界面。
- [Unity热更新]tolua# & LuaFramework(二):打包工具
- 中断线 申请 外设——PCI中断路由机制
- java多线程-Lock接口(将多线程共享资源锁定)
- 数据中心(IDC)的灾难恢复目标以及虚拟机迁移帮助灾难恢复
- 红帽发布首个RHEL 7测试版本