# -*- 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. 在Linux上怎么安装和配置DenyHosts工具

    使用DenyHosts能够进行自动屏ip的功能,掌握DenyHosts在Linux系统中的安装是很有必要的,那么在Linux系统中要如何安装DenyHosts工具呢?安装后又要如何配置呢?这都是用户需 ...

  2. jQuery验证控件(转载)

    转自:http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html 官网地址:http://bassistance.de/jquery ...

  3. Oracle EBS-SQL (PO-6):检查订单接收总数.sql

    SELECT sum(rcvt.quantity) 接收事务处理汇总数--已排除退货 --rsh.receipt_num                   收据号, --pov.vendor_nam ...

  4. javascript函数值的重写

    原文:javascript函数值的重写 javascript函数值的重写 定义了一个函数,需要重写这个函数并使用原先的函数值.做法是: 1.定义一个变量让原先函数的值指向它,把原先函数的指向一个新的函 ...

  5. docker 指定容器名字

    docker:/root# docker run -itd --name linux123 ubuntu /bin/bash Unable to find image 'ubuntu:latest' ...

  6. android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效

    android4.0 禁止横竖屏切换使用 android:configChanges="orientation|keyboardHidden"无效    在之前的版本中都是在Man ...

  7. java 多线程使用方法及Socket的使用

    public class newThread implements Runnable{ public void run(){ dosome(); } public void dosome(){ Sys ...

  8. ar解压deb包

    解压文件 ar -x libstdc++6_4.7.2-5_i386.deb tar -zxvf data.tar.gz

  9. 高仿精仿微信应用ios源码下载

    微信,超过3亿人使用,能够通过手机网络给好友发送语音.文字消息.表情.图片和视频,还可以分享照片到朋友圈.通过摇一摇.查看附近的人,你可以认识新的朋友.使用扫一扫,你可以扫描二维码.条码.图书和街景. ...

  10. kubuntu添加windows字体

    1.选取字体 在/usr/share/fonts/truetype中新建一个目录,命名为ms,然后将Windows\fonts 目录下的tahoma.ttf.tahomabd.ttf(Tahoma的粗 ...