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”,长度 ...
随机推荐
- 蒙特卡洛(Monte Carlo)方法求面积
如图,刷微博时,看到一个问题,第一个想到的就是用蒙特卡洛方法求解,当时正在练python,于是尝试用python编写程序. import random # 先求s1 k=0 n=100000000 f ...
- Python学习笔记(二)Sublime Text 3 安装Package Control
原来Subl3安装Package Control很麻烦,现在简单的方法来了 一.简单的安装方法 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码: ...
- 【zabbix】zabbix 高可用架构的实现
https://www.jianshu.com/p/249d47b089b4?utm_campaign=maleskine&utm_content=note&utm_medium=se ...
- Linux驱动开发1——基础知识
1.三类驱动 字符设备驱动:字节流,/dev下有设备节点,file_operations,inode, file 块设备驱动:数据块,/dev下有设备节点,通常有文件系统 网络设备驱动:网络报文的收发 ...
- [转] python关于ctypes使用char指针与bytes相互转换的问题
最近研究人脸识别,需要用python调用so动态库,涉及到c/c++中的指针字符串转Python的bytes对象的问题. 按照ctypes的文档,直观方式是先创建对应的类型数组,再将指针取地址一一赋值 ...
- HashSet 源码分析
HashSet 1)HashSet 是基于 HashMap 的 Set 接口实现,元素的迭代是无序的,可以使用 null 元素. 创建实例 /** * HashSet基于HashMap实现 */ pr ...
- Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems
Delphi XE2 之 FireMonkey 入门(26) - 数据绑定: TBindingsList: TBindExprItems 如果要给一对 "源控件" 和 " ...
- 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_07 缓冲流_5_BufferedWriter_字符缓冲输出流
使用newLine来换行 同样的效果 println的源码里面其实就用的就是newLine()
- Delphi控件-复合控件
http://blog.csdn.net/cml2030/article/details/3166634 Delphi控件-复合控件 标签: delphidestructorbuttonstring ...
- 45 MySQL自增id
45 MySQL自增id 表定义自增id 说到自增id,前面提到mysql的自增id不连续,当表定义的自增值达到上限后的逻辑是:再申请下一个id时,得到的值保持不变 ; insert into t v ...