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. 浅谈常用的设计模式(new)

    简单工厂模式 抽象工厂模式 代理模式 装饰者模式(Decorator):动态地给一个对象添加一些额外的职责,就增加功能来说,装饰着模式比生成子类更加灵活. 建造者模式:builder构建

  2. 关于vue跨域名对接微信授权认证和APP授权认证

    这种情况一般也只会出现在前后端分离,跨域名授权的时候吧.耗费了一个前端+一个后台+一个网关,熬夜通宵了两天才整出来一套方法(你们见过凌晨6点的杭州吗,对,我下班的时候天黑了,到家天亮了....),和开 ...

  3. input文字垂直居中和按钮对齐问题,兼容IE8

    1.盒子模型问题:请CSS重置 2.按钮不对齐:请浮动或者vertical-align:middle;然后计算宽高,使其对齐 : 3.IE8文本不居中:line-height属性     注意:IE8 ...

  4. react组件更新swiper

    如果swiper渲染出来的数据不是写死的,那么就会涉及到swiper的更新, 那么我们在new 出 swiper 实例的时候,就需要把这个实例添加到组件里面去,在更新的或卸载的时候就可以直接使用 sw ...

  5. SSRS 报表开发小技巧

    说明: 开发工具为: SQL Server Data Tools (英文版)     开发环境为: SQL Server 2012 (英文版) 一. 饼图数据外部显示 首先我们来看3张效果图:  内部 ...

  6. Maven + Spring4

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  7. Android 自定义AlertDialog(退出提示框)

    有时候我们需要在游戏或应用中用一些符合我们样式的提示框(AlertDialog) 以下是我在开发一个小游戏中总结出来的.希望对大家有用. 先上效果图: 下面是用到的背景图或按钮的图片 经过查找资料和参 ...

  8. android设计的布局在阿拉伯语下界面错乱的解决方法

    (1)正在AndroidManifest.xml声明文件的application元素中,增加” android:supportsRtl=true” (2)建] androidの设计的布局在阿拉伯语下界 ...

  9. svn 同步资源库时忽略某些文件类型和文件夹

    项目开发中,开发人员经常用SVN来管理代码,在和服务器同步时,每次都看到一堆.class,.log,target等文件,这样很不舒服. 解决方法: 打开:window-->preferences ...

  10. 把梳子卖给和尚 引起的CRM

    招聘故事  N个人去参加一招聘,主考官出了一道实践题目:把梳子卖给和尚.众多应聘者认为这是开玩笑,最后只剩下甲.乙.丙三个人.   主持人交代:以10日为限,向我报告销售情况.  十 天一到.   主 ...