[LeetCode] 3. Longest Substring Without Repeating Characters(Python)

  • 1. 题目
  • 2. 题目理解
  • 3. 代码实现

1. 题目

Given a string s, find the length of the longest substring without repeating characters.
Example 1:
Input: s = “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:
Input: s = “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:
Input: s = “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3. Notice that the answer must be a substring, “pwke” is a subsequence and not a substring.
Example 4:
Input: s = “”
Output: 0
Constraints:
0 <= s.length <= 5 * 104
s consists of English letters, digits, symbols and spaces.

2. 题目理解

输入一个字符串,输出该字符串中最长不重复子串的字符数。

3. 代码实现

1)思路一
将本位存到字符表中,判断下一位字符是否存在于字符表相同:若不同,temp+1,并加入字符表;若相同,temp_max为当前最长字符数,清空字符表,并从下一位开始判断。但存入字符表再进行字符表的匹配会造成一些冗余。
2)思路二
构建字典dict,起始位start,长度最大值maxl。若字典中存在当位且字典中存储的值+1大于起始位的值,起始位改为当前位+1。将当前位更新到字典中,并将当前最长不重复字符数maxl更新为当前位-start+1与当前maxl中的较大值。

class Solution(object):def lengthOfLongestSubstring(self, s):""":type s: str:rtype: int"""start = 0maxl = 0dict = {}for i, value in enumerate(s):if s[i] in dict and dict[value]+1 > start:start = dict[value] + 1dict[value] = imaxl = max(i-start+1, maxl)return maxl

[LeetCode] 3. Longest Substring Without Repeating Characters(Python)相关推荐

  1. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

  2. leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)

    Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...

  3. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  4. [LeetCode]3.Longest Substring Without Repeating Characters

    [题目] Given a string, find the length of the longest substring without repeating characters. For exam ...

  5. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  6. [LeetCode]--3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  7. LeetCode 3. Longest Substring Without Repeating Characters-- c++,java,python解法

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

  8. leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)

    题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...

  9. leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法

    题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...

  10. python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...

最新文章

  1. 登录之图形跟短信验证码
  2. 2021-04-09 随机模拟—蒙特卡洛方法 Matlab代码实现
  3. 授予数据库账号dba权限_从自建Oracle迁移至云原生数据仓库AnalyticDB PostgreSQL
  4. linux关机时循环输出脚本,Linux关机时执行指定脚本功能实现
  5. PCB设计--PCB画图技巧
  6. Andriod Studio 使用心得,持续更新中
  7. 数据库三级模式两级映像
  8. lpush rpush 区别_redis中lpush、rpush、lset、lrem是什么
  9. Error response from daemon: failed to parse mydockerfile-centos: ENV must have two arguments
  10. openfire4.2.1 + smack4.2.2即时通信工具开发(android端登录、发送消息、接收消息)
  11. 2023最新毕业设计选题 -python毕设选题推荐 - 如何选题 避免被坑
  12. R语言可视化——熵曲线
  13. 青龙面板篇——美团(细致教程 有手就行~)
  14. 工厂生产管理流程有哪些环节?
  15. 二维数组在内存空间地址不一定连续
  16. 利用python爬取豆瓣电影top250
  17. android安全开发_现代android安全开发
  18. 微视linux scsi驱动超时错误处理
  19. 屡陷丑闻的 Facebook,试图靠 AI Bot 管住员工的嘴
  20. InnoDB 离线转储工具

热门文章

  1. 关于from scipy.misc import comb ImportError: cannot import name 'comb'的问题
  2. oracle数据库运行脚本 error reading files,Oracle数据库的故障解决
  3. 短信通知接口json报文开发设计总结
  4. 一个屌丝程序员的青春(二一一)
  5. JEECG常见问题大全
  6. 【数模之数据分析-2】
  7. 数据库中索引原理及填充因子
  8. 《徐志摩传》记得也好,最好你忘掉
  9. New Year Garland
  10. 制定小目标的软件APP哪款好