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 ...
随机推荐
- golang调用c++的dll库文件
最近使用golang调用c++的dll库文件,简单了解了一下,特作此笔记:一.DLL 的编制与具体的编程语言及编译器无关 dll分com的dll和动态dll,Com组件dll:不管是何种语言写的都可以 ...
- 用Linq操作数据小记
小记 public void UpdateWarhouse(Administrator admin) { var warhouseStr = Request["warhouse"] ...
- Delphi、C C++、Visual Basic数据类型的对照 转
Delphi.C C++.Visual Basic数据类型的对照 变量类型 Delphi C/C++ Visual Basic 位有符号整数 ShortInt char -- 位无符号整数 Byte ...
- VR内容定制请找北京动软VR团队,长年承接VR/AR应用、游戏内容定制
最近这一拔VR及AR浪潮得到业界的热捧,与2015年年底到2016年年初乐相.蚁视.睿悦.焰火工坊等VR创业公司,陆续发布融资的信息不无关系.业界也有统计数据称,约90%的VR投资案例,发生在2015 ...
- MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存
二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...
- C# 随机红包算法
static void Main(string[] args) { ; ; double minAmount = 0.01; Random r = new Random(); ; i < num ...
- 【巩固】bootstrap笔记二
这段主要记录如何给排版完的页面加一些动画效果,用到了的插件有: wow.min.js jquery.singlePageNav.min.js animate.css 将导航条上对应的菜单和页面上对应的 ...
- spring mvc 删除返回字符串中值为null的字段
在spring的配置文件中进行一下配置: <bean class="org.springframework.web.servlet.mvc.method.annotation.Requ ...
- Java生成与解析二维码
1.下载支持二维码的jar包qrcode.jar和qrcode_swetake.jar, 其中qrcode_swetake.jar用于生成二维码,rcode.jar用于解析二维码,jar包下载地址(免 ...
- store操作
store.remove(rs); store.sync({ success: function (e, opt) { this.store.commitChanges(); }, failure: ...