length of the longest substring without repeating character
Given a string, find the length of the longest substring without repeating characters.
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
这道题目一拿到手,想都不用想,直接暴力遍历。然而结果虽然可行,但是效率太低,最终还有一个案例没通过,晕。还是要多动脑啊
public int lengthOfLongestSubstring(String s) {
int length = 0;
if(isUnique(s))
{
length = s.length();
}
int index = s.length();
for(int i = 0; i < index; i++)
{
for(int j = index; j >= i; j--)
{
String str = s.substring(i, j);
if(isUnique(str) && str.length() > length)
{
//从字符串的后面往前遍历,遇到第一个符合条件的肯定就是最长的之一,因此不用再往前遍历了
length = str.length();
break;
}
//剩下的子字符串的长度小于等于暂时的结果,也无需再往前遍历
if(j - i <= length)
{
break;
}
}
}
return length;
}
public boolean isUnique(String s)
{
Set<Character> set = new HashSet<Character>();
char[] chars = s.toCharArray();
for(char c: chars)
{
set.add(c);
}
return (set.size() == s.length());
}
无论怎么去优化,最终还是有一个案例没能通过。貌似暴力解法在此题行不通??
看到各种题解都提及到滑动窗口解法,于是去查了一下这个算法的思想,并根据自己的理解去写一下代码,终于通过了,虽然还有很大的提升空间...
public int lengthOfLongestSubstring(String s) {
int result = 0;
int left = 0; //窗口左边界
int right = 0; //窗口右边界
int length = s.length();
Set<Character> set = new HashSet<Character>();
while(left < length && right < length)
{
int temp = 0;
if(!set.contains(s.charAt(right)))
{
set.add(s.charAt(right));
right++;
temp = set.size();
if(temp > result)
{
result = temp;
}
}
else
{
//如果发现set中已经存在此字符,就把左边界右移
set.remove(s.charAt(left));
left++;
}
}
return result;
}
length of the longest substring without repeating character的更多相关文章
- 3. Longest Substring Without Repeating Character[M] 最大不重复子串
题目 Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [刷题] 3 Longest Substring Without Repeating Character
要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...
- Leetcode 解题 Longest Substring without repeating charcater python
原题: Given a string, find the length of the longest substring without repeating character For example ...
- 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, ...
- No.003:Longest Substring Without Repeating Characters
问题: Given a string, find the length of the longest substring without repeating characters.Example:Gi ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- No.003 Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters Total Accepted: 167158 Total Submissions: 735821 Diff ...
- Java [leetcode 3] Longest Substring Without Repeating Characters
问题描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
随机推荐
- 设计模式C++描述----14.外观(Facade)模式
一. 举例说明 还以我以前做的文件系统(FileSys)为例: 文件系统是一个独立的系统,它提供一套核心的文件操作. 除了文件系统,还有四个子系统,分别是杀毒子系统(KillVirus),压缩子系统( ...
- 从0开始独立完成企业级Java电商网站开发(服务端)
数据表结构设计 唯一索引unique,保证数据唯一性 CREATE TABLE `mmall_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ...
- 学 Python (Learn Python The Hard Way)
学 Python (Learn Python The Hard Way) Contents: 译者前言 前言:笨办法更简单 习题 0: 准备工作 习题 1: 第一个程序 习题 2: 注释和井号 习题 ...
- CSS3 变形、过渡、动画、关联属性浅析
一.变形 transform:可以对元素对象进行旋转rotate.缩放scale.移动translate.倾斜skew.矩阵变形matrix.示例: transform: rotate(90deg) ...
- T3hack大部分随机化数据
1000 2000 1 2 1269 1 3 7707 1 4 3329 4 5 6789 1 6 6691 3 7 -1 1 8 2037 6 9 5427 6 10 5690 4 11 4847 ...
- mysql-清除binlog日志命令
记录一个清除MySQL里binlog日志的命令,可用在定时任务脚本里. 只保留1天前的日志: PURGE MASTER LOGS BEFORE DATE_SUB(CURRENT_DATE, INTER ...
- 使用Typescript重构axios(四)——实现基础功能:处理post请求参数
0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...
- 将训练集构建成ImageNet模型
以下程序实现将训练集构建为ImageNet模型,训练集图片为56个民族 import java.io.File; import java.io.FileNotFoundException; impor ...
- Python多线程与队列
Python多线程与Queue队列多线程在感官上类似于同时执行多个程序,虽然由于GIL的存在,在Python中无法实现线程的真正并行,但是对于某些场景,多线程仍不失为一个有效的处理方法: 1,不紧急的 ...
- VS2017,遇到异常:这可能是由某个扩展导致的
网上看的解决办法没有解决,干脆自己亲自动手搞吧! 具体问题如下: 解决方案: 按照提示路径打开日志文件定位问题所在,打开之后,拉倒最后看到如下图所示: 我的问题是因为安装了一个叫 "Clau ...