【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/
题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
解题思路:维护一个窗口,每次关注窗口中的字符串,维护一个HashSet,每扫描一个字符,如果HashSet中包含该字符,则移动左边窗口至重复字符的下一个字符,如果HashSet中没有改字符,则移动右边窗口。时间负复杂度为O(n),空间复杂度也为O(n)。具体代码如下:
public class Solution
{
public static void main(String[] args)
{
String str="abcbbc";
System.out.println(lengthOfLongestSubstring(str));
}
public static int lengthOfLongestSubstring(String s)
{
if(s==null && s.length()==0)
return 0;
HashSet<Character> set = new HashSet<Character>();
int max = 0; //最大不重复字符串长度
int left = 0; //不重复字符串起始位置
int right = 0; //不重复字符串结束为止
while(right<s.length())
{
//如果集合中包含当前所指的字符
if(set.contains(s.charAt(right)))
{
//获取当前不重复字符串的长度,如果大于max,则将max值替换
if(max<right-left)
{
max = right-left;
}
//将left移动到第一个重复的字符后面
while(s.charAt(left)!=s.charAt(right))
{
set.remove(s.charAt(left));
left++;
}
left++;
}
else
{
set.add(s.charAt(right));
}
right++;
}
max = Math.max(max,right-left);
return max;
}
}
【LeetCode OJ】Longest Substring Without Repeating Characters的更多相关文章
- 【Leetcode 3】Longest Substring Without Repeating Characters0
Description: Given a string, find the length of the longest substring without repeating characters. ...
- 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. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Example 1 ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- 【Leetcode】【Medium】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. For example, ...
随机推荐
- python 查看目录下所有目录和文件
python查看目录下所有的子目录和子文件 python递归遍历目录结构 我喜欢第一种 方法1 import json, os def list_dir(path, res): for i in os ...
- 【转】【WPF】WPF绑定用法
一.简介 为了后面行文顺利,在进入正文之前,我们首先对本文所涉及到的绑定知识进行简单地介绍.该节包含绑定的基本组成以及构建方式. WPF中的绑定完成了绑定源和绑定目标的联动.一个绑定常常由四部分组成: ...
- (笔记)Linux下查看CPU使用率的命令
1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...
- VIM下的插入模式的相关知识:
1. 建议:当打错一个单词时,删除掉重新打一遍, 避免在错誤的基础上进行修改: 2. 在插入模式下,可以用一些组合键,它也可以用于VIM 命令模式下,也可以用于 base shell 下: ctrl- ...
- e792. 建立一个包括所有数据的SpinnerListModel
By default, if the user is browsing the values in a SpinnerListModel, the iteration stops when eithe ...
- 远程桌面连接工具 Remote Desktop Manager 9.1.2.0 Enterprise 多国语言绿色版附注册码 简单使用
1:修改成中文简体 2: 注册破解 (记得一定要先断网) admin admin@admin.com 31GKI-OK1HY-59H35-Y8GPB-8WDY6 3 : 创建连接 搞定
- Bioperl 解析blast的输出结果
用bioperl 解析blast的默认输出结果, 整理成-m8格式的输出 #!/usr/bin/perl use Bio::SearchIO; my ($blast) = @ARGV; my $sea ...
- Android 获取内存信息
由于工作需要,研究了一下android上获取内存信息的方法,总结如下: 1.SDK获取 在Java层利用API获取很简单,直接使用ActivityManager.MemoryInfo类即可,代码如下: ...
- 浮点数向偶数舍入的问题 Round-to-Even for Floating Point
Round-To-Even在于To-Up , To-Down, To-towards-Zero对比中,在一定数据量基础上,更加精准.To-Up的平均值比真实数值偏大,To-Down偏小. 例如有效 ...
- 适用于Win8的Office2003_SP3精简版集成各类补丁+兼容包
适用于Win8的Office2003_SP3精简版集成各类补丁+兼容包软件名称: Office 软件版本: Office2003_SP3 软件大小: 104M 软件语言: 简体中文 软件授权: 破解 ...