蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]
题目
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", 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.
翻译
给定一个字符串,找到没有重复字符的最长子串的长度。
例子:
给定“abcabcbb”,答案是“abc”,长度为3。
给定“bbbbb”,答案是“b”,长度为1。
给定“pwwkew”,答案是“wke”,长度为3.请注意,答案必须是子字符串,“pwke”是子序列而不是子字符串。
Hints
Related Topics: Hash Table, Two Pointers, String
利用哈希表来存储字符的位置
计算以字符s结尾的子串的长度 需要一个指针来遍历string来获取每个字符s 还需要一个指针来表示当前以s结尾的子串的头部的位置
遇到之前出现过的字符时 表示上一个子串已经包含了s 需要重新计算头部 于是移动头部指针
参考
代码
Java
public class Solution {
public int lengthOfLongestSubstring(String s) {
if (s.length()==0) return 0;
int max = 0;
int start = 0;//表示现在用来计算最长子串的头的位置
Map<Character, Integer> mymap = new HashMap<>();//通过哈希表来存储现在某个字符出现的位置
for(int i=0;i<s.length();i++){
if(mymap.containsKey(s.charAt(i))){//现在这个字符在之前出现过
start = Math.max(start,mymap.get(s.charAt(i)+1);//需要把头移动到这个字符后面(头原来在上一个这个字符前面
}
mymap.put(s.charAt(i),i);//更新字符的位置
max = Math.max(max,i-start+1);//更新最长字串长度
}
return max;
}
}
Python
class Solution(object):
def lengthOfLongestSubstring(self, s):
"""
:type s: str
:rtype: int
"""
start = maxlen = 0
mymap={}
for i in range(len(s)):
if s[i] in mymap:
start = max(start,mymap[s[i]]+1)
maxlen = max(maxlen,i-start+1)
mymap[s[i]] = i
return maxlen
蜗牛慢慢爬 LeetCode 3. Longest Substring Without Repeating Characters [Difficulty: Medium]的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
随机推荐
- Java NIO (1)
Java NIO (1) 看了下java核心技术这本书 关于nio的部分介绍比较少,而且如果自己写服务器的话nio用的还是比较多,整理一下nio的资料 java中nio主要是三个组件 Buffers ...
- 爬取知乎热榜标题和连接 (python,requests,xpath)
用python爬取知乎的热榜,获取标题和链接. 环境和方法:ubantu16.04.python3.requests.xpath 1.用浏览器打开知乎,并登录 2.获取cookie和User—Agen ...
- ios retainCount
retainCount Important: Typically there should be no reason to explicitly ask an object what its reta ...
- Noip 2016 Day 1 & Day 2
Day 1 >>> T1 >> 水题直接模拟AC: 考察三个知识点:1.你能不能编程 2.你会不会取模 3.你脑子抽不抽 然而第一次评测还是90,因为当模运算时 “ en ...
- ArrayList 一个面试题
我们现在有一个集合,集合里面有100个随机数,获取其中的基数: //假设我们得到了100个随机数 List<Integer> lists = new RandomArrayList< ...
- HIVE-分桶表的详解和创建实例
我们学习一下分桶表,其实分区和分桶这两个概念对于初学者来说是比较难理解的.但对于理解了的人来说,发现又是如此简单. 我们先建立一个分桶表,并尝试直接上传一个数据 buckets row format ...
- pwd命令的实现
pwd 命令描述 Linux中用 pwd 命令来查看"当前工作目录"的完整路径. 简单得说,每当你在终端进行操作时,你都会有一个当前工作目录. 在不太确定当前位置时,就会使用pwd ...
- week8课上实践
课上练习. 第一题: 参考 http://www.cnblogs.com/rocedu/p/6766748.html#SECCLA 在Linux下完成"求命令行传入整数参数的和" ...
- 【bzoj3991】[SDOI2015]寻宝游戏 树链的并+STL-set
题目描述 给出一棵树,初始每个点都是非必经的.多次改变某个点的必经状态,并询问从任意一个点出发,经过所有必经的点并回到该点的最小路程. 输入 第一行,两个整数N.M,其中M为宝物的变动次数. 接下来的 ...
- QT-2D编程
QT-[转]2D编程 Qt中提供了强大的2D绘图系统,可以使用相同的API在屏幕上和绘图·设备上进行绘制,主要基于QPainter.QPainterDevice和QPainterEngine这3个类. ...