LeetCode 3 : Longest Substring Without Repeating Characters ---- 不重复的子串
2016-05-18 14:21
411 查看
原题链接: https://leetcode.com/problems/longest-substring-without-repeating-characters/
一:原题内容
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
Given
Given
subsequence and not a substring.
二:AC代码
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
ret=0
left=0
d={}
for index,value in enumerate(s):
if value in d and d[value]>=left:
left=d[value]+1
d[value]=index
ret=max(ret,index-left+1)
return ret
返回LeetCode 题解目录
一:原题内容
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 asubstring,
"pwke"is a
subsequence and not a substring.
二:AC代码
class Solution { public: int lengthOfLongestSubstring(string s) { int flag[128]={0}; int ans=0;//the answer int j=0; for(int i=0;i<s.size();i++) { if(flag[s[i]]>0) { j=max(j,flag[s[i]]); } flag[s[i]]=i+1; ans=max(ans,i-j+1); } return ans; } };
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
ret=0
left=0
d={}
for index,value in enumerate(s):
if value in d and d[value]>=left:
left=d[value]+1
d[value]=index
ret=max(ret,index-left+1)
return ret
返回LeetCode 题解目录
相关文章推荐
- maven项目打jar,war包方法
- 代码覆盖率工具---Emma
- 条形码支持字符范围及长度限制
- 【云计算】CloudFoundry参考资料
- vsftpd配置虚拟用户
- Python执行系统命令的方法 os.system(),os.popen(),commands
- boundingRectWithSize 计算高度 5s和plus高度不同
- iOS开发内存优化之自动检测内存泄露,检查是否有循环引用,检查内存为何如此大,Block循环引用的检查
- Flume+kafka+spark streaming+scala(sbt编译) 例子演示
- CMake使用总结
- 按钮点击多次出现多个相同页面问题
- Effective cpp 读书笔记5
- 多线程等待/通知机制
- 2016 - PayPal商家集成常见技术问题 - 官方整理
- Minimum Path Sum
- ashx文件获取$.ajax()方法发送的数据
- 批量查询ip地址归属地
- java oracle blob 读写操作
- Hive基础(2): 数据类型、数据库、表、字段、交互式查询的基本操作
- Android 设置背景边框