LeetCode Longest Substring Without Repeating Characters
来源:互联网 发布:学会python能干嘛 编辑:程序博客网 时间:2024/05/16 09:06
LeetCode解题之Longest Substring Without Repeating Characters
原题
找出一个字符串的最长字符串,要求该字符串中没有重复的字符。
注意点:
- 考虑空字符等特殊情况
例子:
输入: “abcabcbb”
输出: 3
输入: “bbbbbb”
输出: 1
解题思路
将没有重复字符的子串称为目标字符串。遍历字符串的所有字符,记录下每个字符最近出现的下标位置,如果该下标比目标字符串的起始下标大,说明目标字符串中已经有该字符了,刷新目标字符串的起始坐标。找出所有目标串的最大长度就是题目要求的答案。
AC源码
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if not s: return 0 if len(s) <= 1: return len(s) locations = [-1 for i in range(256)] index = -1 m = 0 for i, v in enumerate(s): if (locations[ord(v)] > index): index = locations[ord(v)] m = max(m, i - index) locations[ord(v)] = i return mif __name__ == "__main__": assert Solution().lengthOfLongestSubstring("abcea") == 4
欢迎查看我的Github (https://github.com/gavinfish/LeetCode-Python) 来获得相关源码。
0 0
- LeetCode: Longest Substring Without Repeating Characters
- LeetCode Longest Substring Without Repeating Characters
- LeetCode: Longest Substring Without Repeating Characters
- [Leetcode] Longest Substring Without Repeating Characters
- leetcode 23: Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- LeetCode 3 - Longest Substring Without Repeating Characters
- [LeetCode]Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters
- 【leetcode】 Longest Substring Without Repeating Characters
- LeetCode-Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [LeetCode] Longest Substring Without Repeating Characters
- [leetcode] Longest Substring Without Repeating Characters
- LeetCode - Longest Substring Without Repeating Characters
- LeetCode 3: Longest Substring Without Repeating Characters
- leetcode-3:Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- openSUSE 42.1 下安装 Oracle JDK
- %lf\n和%f\n的区别
- IOS视频开发之MPMoviePlayerController
- ARM_s5pv210_porting_1
- gem5: the trace of cache set and cache line write hit counters
- LeetCode Longest Substring Without Repeating Characters
- 【JS】浅谈匿名函数与闭包
- lua语言基础
- java 线程技术详解
- 简单逆向分析使用案例(5)--CrackMe_04.exe 获取用户名和密码
- c++_6 : 构造函数和析构函数
- Notepad++ 正则表达式替换字符串
- mac上一键配置和安装adb驱动或者环境
- java web mysql数据库插入数据乱码问题解决方法