# -*- coding: utf8 -*-
'''
__author__ = 'dabay.wang@gmail.com'
https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating letters for "abcabcbb" is "abc",
which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1. ===Comments by Dabay===
维护一个变量max_so_far来记录到目前为止最大的不重复字符串长度,同时维护一个字符串,这个字符串必须包含正在检查的字符。
因为这个字符串可能继续增长,当长度超过max_so_far时,更新max_so_far。
'''
class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
if len(s) <= 1:
return len(s)
max_so_far = 0
longest = ""
for char in s:
if char in longest:
longest = longest[(longest.index(char))+1:] + char
else:
longest = longest + char
max_so_far = max(max_so_far, len(longest))
return max_so_far def main():
s = Solution()
string = "abcabcbb"
print s.lengthOfLongestSubstring(string) if __name__ == "__main__":
import time
start = time.clock()
main()
print "%s sec" % (time.clock() - start)

[LeetCode][Python]Longest Substring Without Repeating Characters的更多相关文章

  1. C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告

    Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...

  2. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  3. LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)

    题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...

  4. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  5. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  6. 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]

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

  7. LeetCode之Longest Substring Without Repeating Characters

    [题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...

  8. Leetcode 3. Longest Substring Without Repeating Characters (Medium)

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

  9. [Leetcode Week1]Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...

随机推荐

  1. “如何稀释scroll事件”引出的问题

    背景:我在segmentfault提了个问题如何稀释onscroll事件,问题如下: 面试时问到这个问题,是这样的:    面试官问一个关于滚动到某个位置的时候出现一个顶部的导航栏,答完之后,她接着问 ...

  2. 安装ecshop出错

    在安装Ecshop的时候,遇到两个问题: 1.Strict Standards: Non-static method cls_image::gd_version() should not be cal ...

  3. python基础教程第3章——字符串

    1.字符串格式化 字符串格式化操作符%+转换标志+最小字段宽度+点后跟精度值+转换类型 String模块提供另外一种格式化方式 from string import Template s=Templa ...

  4. VB6.0 String 用法总结

    问题一 VB 去掉字符串的头尾的空格 使用trim() 如: trim(" hello world ")= "hello world" 问题二 判断一个字符串是 ...

  5. MacBook USB Type-C接口很美?其实是缩水的!

    苹果终于推出了12寸的全新MacBook,拥有2304×1440的高分辨率.蝶式结构全尺寸键盘.新的触摸板.14nm Core M处理器和无风扇设计,以及新的USB 3.1 Type-C接口.可以预料 ...

  6. ASP.NET缓存中Cache过期的三种策略

    原文:ASP.NET缓存中Cache过期的三种策略 我们在页面上添加三个按钮并双击按钮创建事件处理方法,三个按钮使用不同的过期策略添加ASP.NET缓存. <asp:Button ID=&quo ...

  7. 触发按钮改变panel面板上的小圆圈颜色

    import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TouChaCol implements ...

  8. Qt widgets

    1,Qt部件Widgets--CheckWidgets,安置其他部件的Widgets,让用户选择数值的部件 选择部件---使用户能够从预定义的条目菜单中做出选择,combination QListBo ...

  9. Windows SVN变更发送邮件通知(JAVA实现)

    之前有过一篇python的实现http://blog.csdn.net/wiker_yong/article/details/10334967 1,新增文件post-commit.bat 内容: re ...

  10. NULL和nullptr的区别

    //error C2665: “go”: 2 个重载中没有一个可以转换所有参数类型 #include <iostream> void go(int num) { std::cout < ...