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 a substring, "pwke" is a subsequence and not a substring.

我的答案:

class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
if len(s) > 0:
list_s = []
s1 = ''
for i in range(len(s)):
for j in range(len(s[i:])):
s2 = s[i:]
if s2[j] not in s1:
s1 += s2[j]
else:
list_s.append((len(s1), s1))
s1 = s2[j]
list_s.append((len(s1), s1))
s1 = ''
list_2 = sorted(list_s)
return list_2[-1][0]
return 0

某大神标准答案:

class Solution:
# @return an integer
def lengthOfLongestSubstring(self, s):
start = maxLength = 0
usedChar = {} for i in range(len(s)):
if s[i] in usedChar and start <= usedChar[s[i]]:
start = usedChar[s[i]] + 1
else:
maxLength = max(maxLength, i - start + 1) usedChar[s[i]] = i return maxLength

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

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

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

  2. Leetcode 3. Longest Substring Without Repeating Characters(string 用法 水题)

    3. Longest Substring Without Repeating Characters Medium Given a string, find the length of the long ...

  3. Longest Substring Without Repeating Characters(Difficulty: Medium)

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

  4. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

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

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

  6. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

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

  7. No.003:Longest Substring Without Repeating Characters

    问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...

  8. No.003 Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...

  9. LeetCode3 Longest Substring Without Repeating Characters

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

随机推荐

  1. 适配器(GOF23)

    ---恢复内容开始--- 摘要:由于应用环境的变化,需要将现存的对象放到新的环境中去,但新环境的接口是现存对象不满足的. 意图:将原本接口不兼容的类通过转换,使得它们能够一起工作,复用现有的类 ada ...

  2. HDU2181(KB2-C)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. Vue2.x之父子组件数据传递

    父传子,并且通过fatherEvent接收子组件传过来的值 <template> <div class='father'> <Son :fatherData=" ...

  4. js与native的交互

    WebView与Javascript交互(Android): WebView与Javascript交互是双向的数据传递,1.H5网页的JS函数调用Native函数 2.Native函数调用JS函数,具 ...

  5. jquery 给iframe里的元素添加事件

    $("#iframeId").on("load", function(event){//判断 iframe是否加载完成 这一步很重要 $("#divI ...

  6. Debian 8 升级到 9 Debian 9 How to upgrade Debian 8 Jessie to Debian 9 Stretch

    How to upgrade Debian 8 Jessie to Debian 9 Stretch Contents 1. Objective 2. What's New 3. Preparatio ...

  7. Intel超低功耗CPU的一些信息

    2015年底: Intel Braswell是专门针对超低功耗移动和桌面平台的一个家族,现有赛扬N3000/N3050/N3150.奔腾N3700四款型号,其中N300的热设计功耗只有区区4W,其他三 ...

  8. ulimit命令&pthread_create() error: Resource temporarily unavailable

    http://www.ibm.com/developerworks/cn/linux/l-cn-ulimit/ https://my.vertica.com/docs/5.0/HTML/Master/ ...

  9. xxxx签名算法逆向&&python脚本实现

    前言 有一段时间没看安卓了,找几个软件练练手. 这是一个考驾照用的 app. 官方网址: http://www.******baodian.com/ 本文就分析一下在 重置密码时对 数据包 进行签名来 ...

  10. 3The superclass “javax.servlet.http.HttpServlet" was not found on the Java Build Path 之一

    另外一篇短文里还有第三种解决方案,查看请点击这里 1.异常信息 创建maven web项目时,出现 The superclass “javax.servlet.http.HttpServlet&quo ...