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 ...
随机推荐
- GUI编程(Tkinter) 笔记分享
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
- 防抖(debounce)和 节流(throttling)
防抖(debounce)和 节流(throttling) 1.防抖和节流出现的原因 防抖和节流是针对响应跟不上触发频率这类问题的两种解决方案. 在给DOM绑定事件时,有些事件我们是无法控制触发频率的. ...
- loadrunner常用web动作函数
web_custom_request ---允许使用任何http请求方法 脚本一: web_custom_request("baidu_request","URL=ht ...
- [考试反思]0902NOIP模拟测试35:摆动
skyh/Mr.zkt214 cbx204 6个200 4个180 172 162 我:rank16,160 呃,可以看到这个分差.... 对了教练说了两句话需要记录一下: 1.不要因为一时情绪而作出 ...
- CSPS模拟 93
恰饭的时候lsc说我颓颓废废是要ak的前兆 所以我rp掉光了=.= T1 思维一片混乱 T2 只会n^3 发现决策单调性,但没想全 只知道$determin(l,r)>=determin(l,r ...
- CSPS模拟测试59
这场考得我心态爆炸......... 开场T1只会$n^{2}$,然后发现bfs时每个点只需要被更新一次,其他的更新都是没用的. 也就是说,我们可以只更新还没被更新的点? 于是我先YY了一个链表,发现 ...
- NOIP模拟 19
最近试考的脑壳疼 晚上还有一场555 T1 count 研究性质题. 研究好了AC,研究不明白就没头绪 首先枚举n的因子d 其次发现因为是树,所以如果合法,贡献只能是1 然后发现如果合法,一定是一棵一 ...
- call 与apply深入
http://blog.csdn.net/bao19901210/article/details/21614761
- Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
上一篇文章我们讲了SqlSessionFactoryBean,通过这个FactoryBean创建SqlSessionFactory并注册进Spring容器,这篇文章我们就讲剩下的部分,通过Mapper ...
- jquery倒计时代码
jquery倒计时代码<pre> <span id="day_show">0天</span> <strong id="hour_ ...