leetcode 003
3. Longest Substring Repeating Character
Difficulty:Medium
The link:
Description :
Given a string, find the length of the longest substring without repeating character.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
Solutions:
Solution A:
(* 为解决) 想实现KMP算法 KMP 介绍
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
look_up = {}
for i in range(len(s)):
if s[i] not in look_up:
look_up[s[i]] = i
else:
# l 现在字典的长度
l = len(look_up)
# m 出现重复字母位置
m = look_up[s[i]]
# str01 字符串切片
str01 = s[m:l]
for i, item in enumerate(str01):
if s[i] not in look_up:
i = m + l
l = l + 1
return l
Solution B:
dict,get() The method get() returns a value for the given key. If key is not available then returns default value None.
dict.get(key, default = None)
- key - This is the Key to be searched in the dictionary.
- default - This is the Value to be returned in case key does not exist.
class Solution:
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
l, start, n = 0, 0, len(s)
maps = {}
for i in range(n):
start = max(start, maps.get(s[i], -1)+1)
l = max(l, i - start+1)
maps[s[i]] = i
return l
leetcode 003的更多相关文章
- LeetCode #003# Longest Substring Without Repeating Characters(js描述)
索引 思路1:分治策略 思路2:Brute Force - O(n^3) 思路3:动态规划? O(n^2)版,错解之一:420 ms O(n^2)版,错解之二:388 ms O(n)版,思路转变: 1 ...
- [Leetcode]003. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/ public class Solution ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [leetCode][003] Intersection of Two Linked Lists
[题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- Longest Substring Without Repeating Characters ---- LeetCode 003
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Leetcode:003 无重复字符串
Leetcode:003 无重复字符串 关键点:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复 ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- Leetcode刷题第003天
一.只出现一次的数字 class Solution { public: int singleNumber(vector<int>& nums) { ; for (auto num ...
- leetcode python 003
## 给定一个字符串,求其最长无重复的子字符串##给定“abcabcbb”,答案是“abc”,长度为3.##给定“bbbbb”,答案是“b”,长度为1.##鉴于“pwwkew”,答案是“wke”,长度 ...
随机推荐
- 动态淀粉质(划掉)题单&简要题解
简介 动态点分治的思想:还不太清楚诶怎么办. 大概是通过降低树高来降低每次修改和询问的复杂度吧,还可以把树上一个连通块的信息统计到一个点(重心)上.具体实现方式和普通的静态点分治没有太大的区别,只是把 ...
- [BZOJ3527][ZJOI2014]力:FFT
分析 整理得下式: \[E_i=\sum_{j<i}{\frac{q_i}{(i-j)^2}}-\sum_{j>i}{\frac{q_i}{(i-j)^2}}\] 假设\(n=5\),考虑 ...
- CS2001 VS编译错误
Severity Code Description Project File Line Suppression State Error CS2001 Source file 'C:\Workspace ...
- db2查看当前用户模式及当前用户的权限
1.连接数据库:db2 connect to appdb 2.查询当前用户模式:select current schema from sysibm.sysdummy1 或 select current ...
- springBoot项目常用maven依赖以及依赖说明
springBoot项目常用maven依赖以及依赖说明 1:maven-compiler-plugin <build> <plugins> <!-- 指定maven编译的 ...
- 网络流小记(EK&dinic&当前弧优化&费用流)
欢 迎 来 到 网 络 瘤 的 世 界 什么是网络流? 现在我们有一座水库,周围有n个村庄,每个村庄都需要水,所以会修水管(每个水管都有一定的容量,流过的水量不能超过容量).最终水一定会流向唯一一个废 ...
- qbzt day5 上午
动态规划 递推 递归 记忆化搜索 斐波那契数列 1.用其他已经计算好的结果计算自己的结果(递推) 2.用自己的值计算别人的值(考虑对之后的项做出的贡献) cin >> n; f[]= ...
- FlexPaper做的类似百度文库的效果
这里有个误区,虽然我的截图这里有个FlexPaperViewer.swf, 但是这个文件还是要放在网站根目录一个. <%@ Page Language="C#" Auto ...
- 【cs231n作业笔记】二:SVM分类器
可以参考:cs231n assignment1 SVM 完整代码 231n作业 多类 SVM 的损失函数及其梯度计算(最好)https://blog.csdn.net/NODIECANFLY/ar ...
- GMM demo
# GMM model # // library(mvtnorm) ) n1 = n2 = mu1 = c(,) mu2 = c(-,-) sigma1 = matrix(c(,.,.,),nrow= ...