[LeetCode][Python]Longest Substring Without Repeating Characters
# -*- 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的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目 Given a string, find the length of the longest substring without repeating characters. Examples: ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
随机推荐
- Node.js内置的工具和第三方模块来进行单步调试
1.命令行调试: Node.js调试命令: run 执行脚本,在第一行暂停 restart 重新执行脚本 cont,c 继续执行,知道遇到下一个断点 next,n 单步执行 step,s 单步执行,并 ...
- MediaPlayer类——播放视频和音乐
1)如何获得MediaPlayer实例: 可以使用直接new的方式: MediaPlayer mp = new MediaPlayer(); 也可以使用create的方式,如: MediaPlayer ...
- IO-04. 混合类型数据格式化输入
/** *A4-IO-04. 混合类型数据格式化输入 *C语言实现 *测试已通过 */ #include "stdio.h" int main() { float m1,m2; i ...
- python手记(51)
python通过声音将文件内容隐藏,实现原理是将文件的内容分别插入到声音文件的不同位置中做为当次采样的数据,目前是对英文文本文档加解密 #!/usr/bin/env python # -*- codi ...
- 【转】Logistic regression (逻辑回归) 概述
Logistic regression (逻辑回归)是当前业界比较常用的机器学习方法,用于估计某种事物的可能性.比如某用户购买某商品的可能性,某病人患有某种疾病的可能性,以及某广告被用户点击的可能性等 ...
- ubuntu texlive 中文的配置方法
\documentclass[12pt]{article} \usepackage{CJKutf8} \usepackage{indentfirst}%设置第一段缩进,英语中从第二段才有缩进 \use ...
- Hibernate 、多表关联映射 - 多对多关系映射(many-to-many)
hibernate.cfg.xml: <hibernate-configuration> <session-factory name="sessionFactory&quo ...
- How to use pagination in Magento
classYour_Module_Block_Entityname_ListextendsMage_Core_Block_Template { protected function _construc ...
- 关于oracle的certview
前两天去参加oracle 11g 的两门考试(1Z0-051和1Z0-052),在家看了好几遍题库,我本来想着上午一门,下午考一门,但是我嫌着麻烦,就预约一下午考完两门.在考试完一门后,发现成绩不 ...
- wcf系列学习5天速成——第五天 服务托管
今天是系列的终结篇,当然要分享一下wcf的托管方面的知识. wcf中托管服务一般有一下四种: Console寄宿: 利于开发调试,但不是生产环境中的最佳实践. winform寄 ...