19.Longest Substring Without Repeating Characters(长度最长的不重复子串)
Level:
Medium
题目描述:
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: 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.
思路分析:
给定一个字符串,找到其长度最长的不重复子串,用滑动窗口的思想,设置窗口start,应尽量使窗口变大,从第一个字符开始遍历,遍历时,如果字符未出现过,在map中给出该字符和下标的映射,窗口的大小始终为start到i,如果遍历到某个字符在map中已经有它的映射,那么start的值发生变化,更新为这个字符所对应的map中的下标,即左窗口向右滑动。
代码:
public class Solution{
public int lengthOfLongestSubstring(String s){
if(s==null||s.length()==0)
return 0;
int []map=new int [256];
int start=0;
int len=0;
for(int i=0;i<s.length();i++){
if(map[s.charAt(i)]>start) //大于start的原因是abba这种情况,当b重复时start更新为2,如果这里不是大于start而是大于0,那么访问a的时候会认为a重复,所以必须是大于start。
start=map[s.charAt(i)];
map[s.charAt(i)]=i+1;
len=Math.max(len,(i+1-start));
}
return len;
}
}
19.Longest Substring Without Repeating Characters(长度最长的不重复子串)的更多相关文章
- 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(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- LeetCode[3] Longest Substring Without Repeating Characters
题目描述 Given a string, find the length of the longest substring without repeating characters. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- redis学习五 集群配置
redis集群配置 0,整体概述 整体来说就是: 1,安装redis 2,配置多个redis实例 3,安装 ruby和rubygems 4,启动red ...
- Solaris10镜像情况下如何修复boot archive
在某些情况下(比如:异常宕机)solaris10的boot archive可能会损坏,导致solaris无法启动,此时需要手工修复boot archive. 本文通过模拟boot archive损坏, ...
- Android 4学习(5):概述 - Android应用程序的生命周期
参考:<Professional Android 4 Application Development> Android应用程序生命周期 Android应用程序无法控制自己的生命周期,因此它 ...
- Eclipse: “The import java.io cannot be resolved”
检查一下选项: 重点看jdk的绑定 43down voteaccepted Check your Eclipse preferences: Java -> Installed JREs. The ...
- SpringMVC + AJAX 实现多文件异步上传
转自:https://www.jianshu.com/p/f3987f0f471f 今天,我就这个问题来写一篇如何用 SpringMVC + AJAX 实现的多文件异步上传功能.基本的代码还是沿用上篇 ...
- 重启Oracle服务
转自:https://blog.csdn.net/wu2700222/article/details/78021207 #su– oracle sqlplus/ as sysdba //关闭服务 sh ...
- 1-EasyNetQ介绍(黄亮翻译)
EasyNetQ 是一个容易使用,坚固的,针对RabbitMQ的 .NET API. 假如你尽可能快的想去安装和运行RabbitMQ,请去看入门指南. EasyNetQ是为了提供一个尽可能简洁的适用与 ...
- 人工智能二之Sublime Text3环境配置
1.在Ubuntu中按CTRL+ALT+T打开命令窗口,按下面步骤和命令进行安装即可: 添加sublime text 3的仓库: sudo add-apt-repository ppa:webupd8 ...
- python爬虫(8)--Xpath语法与lxml库
1.XPath语法 XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历.XPath 是 W3C XSLT 标准的主要元素,并且 XQuery ...
- Eclipse中,将tab缩进改为4个空格
用4个空格来缩进 , 不要用Tab来缩进 , 因为Tab在不同平台的点位不一样 eclipse->preferences->General->Editors->Text Edi ...