【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, ...
随机推荐
- VS下关于 _CRT_SECURE_NO_WARNINGS 问题的分析与解决
一.问题的起因 六月下旬,老师布置了有关图形学的课设,于是我将我在VS2013中写好的代码进行编译,结果得到了以下信息: 二.解决方法 1,按照英文提示,我先将fopen改成了fopen_s后,发现错 ...
- smarty模板开发基础总结
前提:1. 部署smarty模板目录:2. 编写Smarty类的子类,定制好template_dir.compile_dir.config_dir.cache_dir.left_delimiter.r ...
- e861. 在两个组件之间共享输入映射和事件映射
By sharing an InputMap or ActionMap, any change to the shared InputMap or ActionMap will affect all ...
- C# ?? 运算符是什么?
?? 运算符定义在将可空类型分配给非可空类型时返回的默认值. int? c = null; //若 c 为 null,则 d 为 -1,否则把 c 值赋予 dint d = c ?? -1;
- Linux环境安装MySQL数据库(RPM格式的软件包)
1. 下载mysql安装包 下载地址1: http://www.mysql.com/ (mysql官网) 下载地址2: http://mirrors.sohu.com/mysql/ (其 ...
- PHP usort 使用用户自定义的比较函数对数组中的值进行排序
From: http://www.php100.com/cover/php/2395.html usort (PHP 4, PHP 5) usort — 使用用户自定义的比较函数对数组中的值进行排序 ...
- 关于最近WIN7系统错误711的解决办法
昨天晚上有发现部分用户反馈错误711,因为不在现场不清楚是怎么一回事,今天早上又有其他客户反馈他下面有4个用户发生711错误. 刚好在电脑边上,就拨号试下,结果我的也是711 这个711的症状是: 单 ...
- 原版的WEB认证客户端,提供源代码,让用户自行编译
今天在翻找文件,偶尔发现这个文件,就把源代码发给大家吧. 有需要的,可以尽管改,程序在D7下编译 下载地址:下载地址1
- shell脚本重启tomcat
1. 新建shell空脚本文件,如 /home/tr.sh,并设置权限 # chmod 750 /home/tr.sh 2. 设置文件形式: # sed -e 's/\^M//g' /home/tr. ...
- Ubuntu下从外网上北邮人BT
首先你需要有北邮的VPN账号和密码,只要是北邮的学生都有 账号和密码不懂的请查看 VPN账号密码说明 接下来登录 https://sslvpn.bupt.edu.cn,输入账号和密码 已经登录好了 但 ...