Leetcode3:Longest Substring Without Repeating Characters@Python
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
首先我解这个题的思路是依次找到一个字符串的所有字串,和已存的LongestSubstring比较长度,如大于,替换,直至找到所有的字串。
我的这个程序可能还有问题,提交后显示为Time Limit Exceeded(超时),不过我有时间会改下它,找到错误。
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
longestSubstring = ''
strLength = len(s)
longestSubstringLength = 0
for i in range(strLength):
subString = ''
str = s[i:strLength]
for j in range(len(str)):
if str[j] not in subString:
subString += str[j]
if j == len(str)-1:
substringLength = len(subString)
if substringLength>longestSubstringLength:
longestSubstringLength = substringLength
longestSubstring = subString
else:
substringLength = len(subString)
if substringLength>longestSubstringLength:
longestSubstringLength = substringLength
longestSubstring = subString
break
return longestSubstringLength
在网上我找到了另外的对于Python特别方便的解法:开一个字典记录字符串中的字符和它的索引,left用来记录当前字符最新出现的地方。
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
res = 0
left = 0
d = {}
for i,ch in enumerate(s):
if ch in d and d[ch] >= left:
left = d[ch] + 1
d[ch] = i
res = max(res,i - left + 1)
return res
Leetcode3:Longest Substring Without Repeating Characters@Python的更多相关文章
- LeetCode3 Longest Substring Without Repeating Characters
题意: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 最长子串(Leetcode-3 Longest Substring Without Repeating Characters)
Question: Given a string, find the length of the longest substring without repeating characters. Exa ...
- 滑动窗口解决最小子串问题 leetcode3. Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. Example ...
- leetcode Longest Substring Without Repeating Characters python
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtyp ...
- Leetcode3.Longest Substring Without Repeating Characters无重复字符的最长字串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- LeetCode3:Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- [Swift]LeetCode3. 无重复字符的最长子串 | Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- [原创]cocos2d-x研习录-第三阶 特性之物理引擎
游戏物理引擎是指在游戏中涉及物理现象的逻辑处理,它用于模拟现实世界的各种物理规律(如赛车碰撞.子弹飞行.物体掉落等),让玩家能够在游戏中有真实的体验. Cocos2D-x中支持Box2D和Chipmu ...
- close-vs-shutdown-socke
http://stackoverflow.com/questions/4160347/close-vs-shutdown-socket http://blog.csdn.net/jnu_simba/a ...
- Orchard学习笔记
1.下载Orchard sourcrs资源文件,同时也可以去百度下载中文包 资源地址(https://github.com/OrchardCMS/Orchard/releases/download/1 ...
- Nginx和Apache配置日志格式记录Cookie
记录Cookie有什么用? 有时候我们需要通过web服务器的访问日志来统计UV(独立访客),并据此分析用户的行为.而UV是依据cookie数据得出的统计.UV相对于IP的好处是:IP是一个反映网络虚拟 ...
- OpenRefine 数据清洗工具
OpenRefine(官方网站:http://openrefine.org/).它能自动对数据内容进行修正与整理.OpenRefine可以实现数据排序.自动查找重复条目并完成数据记录.OpenRefi ...
- IIS性能相关的配置、命令
IIS性能相关的配置.命令 应用程序池回收 不要使用缺省的“固定时间间隔(分钟)”:1740(即29小时),建议改为0 可以根据实际情况设置特定时间回收,比如凌晨4点 最大工作进程数 可以根据实际情况 ...
- split 转义
问题:str.split("?");报错了 原因: .?*+!^$[]()\在正则里需要转义 转义需双反斜线.splt("\\+");
- IOS-细节错误
当页面显示时一直奔溃,错误提示-[UICachedDeviceWhiteColor pointSize]: unrecognized selector sent to instance 原因是设置导航 ...
- 如何在 Delphi 中静态链接 SQLite
搞了我几个小时,终于成功在 Delphi 中静态链接了 SQLite (v3.5.4),下一步就是研究加密了,呵呵中间其实遇到很多问题,今天累了,就不说了,改天补上 下载测试工程 下面说说方法 1.当 ...
- H5摇一摇遇到的问题
一.如何对摇晃效果进行反馈 刚开始的处理方式是,摇晃过程中不做任何处理,但后来反馈说这种效果不好,好像就没有摇动一样,如果声音也不响的话,就真的和什么都没发生一样. 后来想了想,加入摇晃过程动画,就像 ...