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的更多相关文章

  1. 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 ...

  2. [Leetcode]003. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/ public class Solution ...

  3. 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  4. [leetCode][003] Intersection of Two Linked Lists

    [题目]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  5. Longest Substring Without Repeating Characters ---- LeetCode 003

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. Leetcode:003 无重复字符串

    Leetcode:003 无重复字符串 关键点:给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度.示例 1:输入: "abcabcbb"输出: 3 解释: 因为无重复 ...

  7. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...

  8. Leetcode刷题第003天

    一.只出现一次的数字 class Solution { public: int singleNumber(vector<int>& nums) { ; for (auto num ...

  9. leetcode python 003

    ## 给定一个字符串,求其最长无重复的子字符串##给定“abcabcbb”,答案是“abc”,长度为3.##给定“bbbbb”,答案是“b”,长度为1.##鉴于“pwwkew”,答案是“wke”,长度 ...

随机推荐

  1. 图片上传预览转压缩并转base64详解(dShowImg64.js)

    hello,大家好,游戏开始了,欢迎大家收看这一期的讲解.本次的内容是图片的上传预览.最后发源码链接.废话不多说,先上图. 待上传图像 点击蓝色框内,pc可以选择文件,移动端选择拍照或选择图片进行上传 ...

  2. oracle触发器update本表数据

    功能: 1. 允许/限制对表的修改 2. 自动生成派生列,比如自增字段 3. 强制数据一致性 4. 提供审计和日志记录 5. 防止无效的事务处理 6. 启用复杂的业务逻辑 开始 create trig ...

  3. icon组件

    iocn组件,小程序给我们提供了一些小图标,与微信的风格一致 iocn组件的属性: type:类型:string icon的类型: 属性值:success:成功   带背景的 success_no_c ...

  4. MySQL高可用架构之MySQL5.7组复制MGR

    MySQL高可用架构之MySQL5.7组复制MGR########################################################################### ...

  5. java SimpleDateFormat setLenient用法

    参考博客:https://www.cnblogs.com/my-king/p/4276577.html SimpleDateFormat.setLenient(true) : 默认值true,不严格解 ...

  6. windows7 玩 WinKawaks kof2002为什么提示couldn't initialise DirectSound?

    插上 耳机  或者 音响 就ok 呵呵 http://wenwen.sogou.com/z/q200172744.htm windows7 玩 WinKawaks kof2002为什么提示couldn ...

  7. Python uuid库中 几个uuid的区别

    在用到uuid库的时候,发现uuid有很多个,比较好奇,就查了一下他们的区别 uuid1()——基于时间戳 uuid2()——基于分布式计算环境DCE(Python中没有这个函数) uuid3()—— ...

  8. C# datatable 与 xml文件之间的转换

    /// <summary> /// datatable转XML文件 /// </summary> /// <param name="dtTable"& ...

  9. web 前端2 html css一些小问题技巧

    html css一些小问题技巧 1 对于儿子块float后,父亲块如果没内容就不见了,如何让父亲块依然跟随飘起了的儿子块撑起来呢?? 用到的属性after方法  公共方法作为继承即可. 1.1  方法 ...

  10. (4.30)全面了解触发器:DML、DDL、LOGON触发器

    DML.DDL.LOGON触发器 转自:https://www.cnblogs.com/seusoftware/p/9120632.html 触发器可以理解为由特定事件触发的存储过程, 和存储过程.函 ...