[LeetCode] 3. Longest Substring Without Repeating Characters(Python)
[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)相关推荐
- 【贪心】LeetCode 3. Longest Substring Without Repeating Characters
LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...
- leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)
Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode]3.Longest Substring Without Repeating Characters
[题目] Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode:3. Longest Substring Without Repeating Characters
https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...
- [LeetCode]--3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode 3. Longest Substring Without Repeating Characters-- c++,java,python解法
题目地址: Given a string, find the length of the longest substring without repeating characters. Example ...
- leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)
题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...
- leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法
题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...
- python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters
算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...
最新文章
- 登录之图形跟短信验证码
- 2021-04-09 随机模拟—蒙特卡洛方法 Matlab代码实现
- 授予数据库账号dba权限_从自建Oracle迁移至云原生数据仓库AnalyticDB PostgreSQL
- linux关机时循环输出脚本,Linux关机时执行指定脚本功能实现
- PCB设计--PCB画图技巧
- Andriod Studio 使用心得,持续更新中
- 数据库三级模式两级映像
- lpush rpush 区别_redis中lpush、rpush、lset、lrem是什么
- Error response from daemon: failed to parse mydockerfile-centos: ENV must have two arguments
- openfire4.2.1 + smack4.2.2即时通信工具开发(android端登录、发送消息、接收消息)
- 2023最新毕业设计选题 -python毕设选题推荐 - 如何选题 避免被坑
- R语言可视化——熵曲线
- 青龙面板篇——美团(细致教程 有手就行~)
- 工厂生产管理流程有哪些环节?
- 二维数组在内存空间地址不一定连续
- 利用python爬取豆瓣电影top250
- android安全开发_现代android安全开发
- 微视linux scsi驱动超时错误处理
- 屡陷丑闻的 Facebook,试图靠 AI Bot 管住员工的嘴
- InnoDB 离线转储工具