【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, ...
随机推荐
- 谈谈mongodb,mysql的区别和具体应用场景
最近对数据库比较感兴趣,于是就去研究了下部分相关热门的数据库. MySQL 关系型数据库. 在不同的引擎上有不同 的存储方式. 查询语句是使用传统的sql语句,拥有较为成熟的体系,成熟度很高. 开源数 ...
- C++ 模板 template
#include <iostream> using namespace std; /* 模板的作用: 1. 不用声明类型, 传什么进来就是什么类型, 返回也是什么类型 2. 方法封装起来, ...
- VS下关于 _CRT_SECURE_NO_WARNINGS 问题的分析与解决
一.问题的起因 六月下旬,老师布置了有关图形学的课设,于是我将我在VS2013中写好的代码进行编译,结果得到了以下信息: 二.解决方法 1,按照英文提示,我先将fopen改成了fopen_s后,发现错 ...
- Objective-C MacOS以管理员权限执行程序
在MacOS下非常多操作是须要管理员权限的, 比方我们执行chmod.在命令行下能够使用sudo chmod来申请以管理员权限执行.可是使用XCode写的程序是不能使用sudo的. 须要自己写代码来申 ...
- C# 将DateTime.Now.DayOfWeek的值转为中文星期
1. 如果常规使用DateTime.Now.DayOfWeek则得到是英文,如果加ToString后再加上相关参数(“G”.“g”.“X”.“x”.“F”.“f”.“D”或“d”),则还会有所变化. ...
- Ubuntu 安装IntelliJ IDEA
1. 下载IDEA 官网地中:http://www.jetbrains.com/idea/download/index.html 选择对应操作系统的版本.下载后,我的文件名称为: idea ...
- js 闭包实例
var db = (function() { // 创建一个隐藏的object, 这个object持有一些数据 // 从外部是不能访问这个object的 var data = {}; // 创建一个函 ...
- memcached能获取所有的key吗
memcached能获取所有的key吗 Memcache 查看列出所有key方法 Memcached中获取所有的key 特别要注意:memcached保存的值需要序列化,否则是无法保存的,而且是不会报 ...
- SVN中图标符号的含义
黄色感叹号(有冲突): 这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别人的 ...
- [转]Python多线程与多线程中join()的用法
https://www.cnblogs.com/cnkai/p/7504980.html Python多线程与多进程中join()方法的效果是相同的. 下面仅以多线程为例: 首先需要明确几个概念: 知 ...