蜗牛慢慢爬 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 ...
随机推荐
- aircrack-ng 破解无线网络
1.科普当今时代,wifi 已成为我们不可缺少的一部分,上网.看视频.玩游戏,没有 wifi 你就等着交高额的流量费吧,本来我想单独的写 wpa 破解和 wps 破解,后来觉得分开写过于繁琐,索性合并 ...
- 浅谈Vue响应式(数组变异方法)
很多初使用Vue的同学会发现,在改变数组的值的时候,值确实是改变了,但是视图却无动于衷,果然是因为数组太高冷了吗? 查看官方文档才发现,不是女神太高冷,而是你没用对方法. 看来想让女神自己动,关键得用 ...
- go语言 os.Rename() cannot move the file to a different disk drive 怎么办
时值我小病在家休养生息,喜欢跳广场舞的外公来寻求我的帮助,他们跳广场舞是将存有歌曲的U盘插到音响上面,而音响大部分都是只能显示歌曲的索引index,不能直接显示歌曲名,所以为了方便他们会在U盘里面对歌 ...
- sqlserver之group by 与over函数
group by 函数主要用来对数据进行分组,over()函数则是一个“开窗函数”,它更多的是与聚合函数如:sum().max().min().avg().count()等函数以及排名函数如:row_ ...
- 如何将M文件转成独立可执行程序
如何将MATLAB程序编译成独立可执行的程序?生成独立可执行的程序(exe文件)步骤 1.安装编译器.可有多种选择,matlab自带了一个LCC,推荐使用VC++6.0,我基于VS 2013实现 ...
- Oracle入门第二天(下)——单行函数
一.概述 以下内容完整参阅,参考官方文档函数手册部分:https://docs.oracle.com/cd/E11882_01/nav/portal_5.htm 离线chm手册英文版:链接:https ...
- JQuery树插件——ztree
API与Demo:http://www.treejs.cn/v3/api.php 使用插件,第一步依然是引入: <link rel="stylesheet" href=&qu ...
- 20155220吴思其 实验2 Windows口令破解
实验目的: 了解Windows口令破解原理 对信息安全有直观感性认识 能够运用工具实现口令破解 实验人数 每组一人 系统环境 windows 实验工具 LC5 SuperDic 实验原理 口令破解方法 ...
- 20155222 2016-2017-2 《Java程序设计》实验二
1 测试 public class MyUtil{ public static String percentage2fivegrade(int grade){ //如果成绩小于60,转成"不 ...
- java随机数的生成
我们经常会用到随机数的生成,作为唯一性的id或者标识: long now = System.currentTimeMillis(); SimpleDateFormat dateFormat=new S ...