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. 巧用SimpleDateFormat将Date类型数据按照规定类型转换。

    在使用SimpleDateFormat之前,我们来了解一下这个类.SimpleDateFormat is a concrete class for formatting and parsing dat ...

  2. 微信小程序image组件

    image组件:是小程序专门针对图片的组件,功能强大 image组件的属性: src:类型 字符串 图片资源的路径 mode:类型 字符串 图片裁剪缩放模式 lazy-load:类型 布尔 图片的懒加 ...

  3. React-Native 之 GD (二十一)APP 打包

    1.生成一个签名密钥: 在 /android/app 下运行 说明:keytool -genkey -v -keystore my-release-key.keystore -alias my-key ...

  4. linux基本目录

    / 根目录: dev : 存放抽象硬件 ib : 存放系统库文件 sbin : 存放特权级二进制文件 var : 存放经常变化的文件 home : 普通用户目录 etc : 存放配置文件目录 /etc ...

  5. fatal: repository 'xxxx' not found

    环境:centOS7  背景:公司代码仓库迁移,因而配置的jenkins自动打包git地址也要跟着变化. 问题描述:git clone http xxxx.git后报错: fatal: reposit ...

  6. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_08 转换流_2_编码引出的问题_FileReader读取GBK格式文件

    IDEA默认的编码格式是UTF-8 新建一个txt文件.输入你好 ANSI就是系统默认编码.保存即可. 通过IDE打开是乱码的,因为默认打开的方式是UTF-8 转换为char类型 输出了乱码

  7. dict用法

    1 dict.items() https://www.runoob.com/python3/python3-att-dictionary-items.html 2 setdefault的用法 注意se ...

  8. Caffe::Snapshot的运行过程

    Snapshot的存储 概述 Snapshot的存储格式有两种,分别是BINARYPROTO格式和hdf5格式.BINARYPROTO是一种二进制文件,并且可以通过修改shapshot_format来 ...

  9. 新手解惑:nginx&php-fpm&fastcgi 是什么关系

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者.   web server(比如说nginx)只是内容的分发者.比如,如果请求/index ...

  10. MVC 源码系列之控制器执行(一)

    控制器的执行 之前说了Controller的激活,现在看一个激活Controller之后控制器内部的主要实现. public interface IController { void Execute( ...