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. 【 js 片段 】如何组织表单的默认提交?【亲测有效】

    最近做的一个项目,我分到的部分有表单验证.点击了提交按钮,但我并不想让他跳转页面去提交.于是经过各种百度,各种 stackoverflow,各种抱大神腿之后,有了以下解决办法: 重点就是阻止 form ...

  2. PHP通过api上传图片

    参考:接口实现图片上传 提交端: $url="localhost:805/rdyc/123.jpg"; $img=file_get_contents($url); $img_api ...

  3. 线上Bug修复流程

  4. Android MVP模式就是这么回事儿

    MVP模式 概念就不说了,大家都懂.View层通过Persenter层相互通信,避免了View中大量的业务代码,而将其提取到Model中.其实简单的来说,就是通过接口回调,把业务分离出去.提高代码的可 ...

  5. 带你从零学ReactNative开发跨平台App开发(六)

    ReactNative跨平台开发系列教程: 带你从零学ReactNative开发跨平台App开发(一) 带你从零学ReactNative开发跨平台App开发(二) 带你从零学ReactNative开发 ...

  6. 让索引包含null值的两种方法

    1. 把有NULL值的列与一个常数,或者一个带有not null约束的列一同索引 create index ind_01 on t01(col01,1); 或者 create index ind_01 ...

  7. jsp到java后台中文乱码问题

    ---首先描述一下我的情况,我的jsp    设置了编码格式 <%@ page language="java" contentType="text/html; ch ...

  8. textarea高度跟随文字高度而变化

    html部分: <textarea id="textarea">哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽哈喽</textarea> js部分: < ...

  9. J2EE开发环境--RAP

    J2EE开发环境--RAP J2EE开发环境分四步: 1.JDK环境 2.tomcat 3.redis环境 4.mysql环境 5.RAP包 线上环境,推荐使用源码,自建应用用户,设置对应规则,禁止关 ...

  10. [翻译] iOSSharedViewTransition

    iOSSharedViewTransition iOS 7 based transition library for View Controllers having a Common View 基于i ...