蜗牛慢慢爬 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 ...
随机推荐
- 浅析Vue.js 中的条件渲染指令
1 应用于单个元素 Vue.js 中的条件渲染指令可以根据表达式的值,来决定在 DOM 中是渲染还是销毁元素或组件. html: <div id="app"> < ...
- 浅谈style.height、clientHeight、offsetHeight、scrollHeight
先分别介绍以下,以下资料来自MDN HTMLElement.offsetHeight 是一个只读属性,它返回该元素的像素高度,高度包含该元素的垂直内边距和边框,且是一个整数. Element.clie ...
- 安装虚拟机&Linux命令学习
安装虚拟机&Linux命令学习 基于VirtualBox虚拟机安装Ubuntu 1.下载安装VirtualBox 根据自己电脑(32位操作系统)的实际情况,我在网上找了相应的VirtualBo ...
- [agc010D]Decrementing-[。。。思考题]
Description 传送门 Solution 真是够神秘的啊... Alice和Bob两个真的城会玩. 不过本题一个暗示挺明显的.就是黑板上所有数不论何时gcd为1. 考场上我以为会很复杂,结果. ...
- ELKStack入门篇(一)之ELK部署和使用
一.ELKStack简介 1.ELK介绍 中文指南:https://www.gitbook.com/book/chenryn/elk-stack-guide-cn/details ELK Stack包 ...
- NumPy v1.15手册汉化
NumPy参考 数组创建 零 和 一 empty(shape[, dtype, order]):返回给定形状和类型的新数组,而不初始化条目 empty_like(prototype[, dtype, ...
- 学习HTML 第三节.接近正题:HTML样式-CSS级联样式表
CSS (Cascading Style Sheets)级联样式表 内联样式 内联样式- 在HTML元素中使用"style" 属性 使用内联样式的方法是在相关的标签中使用样式属性. ...
- rocketmq Lock failed,MQ already started -c参数
今天部署rocketmq集群时一台机器部署一个master 和slave,slave部署总是失败,通过查看日志显示下面的错误 java.lang.RuntimeException: Lock fail ...
- selenium自动化之稳定版本环境介绍
大家都知道,目前selenium版本已经升级到3.0了,selenium3只是在selenium2的基础上做了一些调整,最明显的区别就是 selenium2对Firefox的支持最高只支持46及以下版 ...
- centos 7 安装和基本配置
U盘安装centos 7 还是官方文档最准确. 下载centos https://docs.centos.org/en-US/centos/install-guide/downloading/ 制作安 ...