# -*- 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. 利用CSS3选择器定制checkbox和radio

    之前在一个项目中需要定制checkbox,于是乎用图片模拟了一下,之后发现个更好用的方法(*因为兼容问题 在移动开发中用用就好) 效果: 1 2 3 4 5 6 7 实现代码: <style t ...

  2. HTML特殊符号编码大全

    HTML特殊字符编码:往网页中输入特殊字符,需在html代码中加入以&开头的字母组合或以&#开头的数字.下面就是以字母或数字表示的特殊符号大全. ´ ´ © © > > µ ...

  3. Python:staticmethod vs classmethod

    Being educated under Java background, static method and class method are the same thing. But not so ...

  4. 国标电表DLT645转MODBUS TCP协议转换器MRD-5021,工业设备,浪涌三级保护MRD

    DL/T645转ModbusTcp协议转换器 MRD-5021具有1 路RS485及1路以太网接口,最多支持同时采集5个DL/T645-1997或者5个2007协议国标电表设备,支持DL/T645协议 ...

  5. Mybatis基础入门 I

    作为ORM的重要框架,MyBatis是iBatis的升级版.Mybatis完全将SQL语句交给编程人员掌控,这点和Hibernate的设计理念不同(至于Hibernate的理念,待我先学习学习). 下 ...

  6. 给ecshop后台增加管理功能页面

    给ecshop后台增加管理功能页面 比如我们增加一个统计报表叫做 物流费用统计报表 放在后台“报表统计”栏目中 具体操作步骤: 第一步,我们要添加一个菜单到后台,然后设置语言项,最后设置权限,这样,后 ...

  7. 一张图告诉你如何优化web 性能

  8. REST和JAX-RS相关知识介绍

    REST REpresentational State Transfer:代表性状态传输.具象状态传输 REST定义了应该如何正确地使用Web标准,例如HTTP和URI.REST并非标准,而是一种开发 ...

  9. 20160125--Spring

    package com.hanqi; import java.util.*; import com.hanqi.User; public class HelloWorld { public Hello ...

  10. KMP字符串匹配

    #include<iostream> using namespace std; #define MAX 255 typedef unsigned char BYTE; typedef BY ...