【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode
(一)题目
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.
题目意思:求一字符串中最长的无重复的字符串
(二) 解题过程
一开始拿到这题,直接暴力解决,用双重循环找无重复的子字符串
结果可想而知,超时!Time Limit Exceeded!
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int max = 0 ;
for(int i = 0 ; i < s.length();++i)
{
int j = i+1;
int count = 1;
while(j<s.length())
{
int z = j-1;
while(s[j] != s[z]&&z>=i) z--;//判断是s[j]不等于s[i~j]任一个字符
if(z+1 == i) count++;//如果不等,则count++
j++;
}
if(max<count) max = count;//记录最大值
}
return max;
}
};
O(n^2)的复杂度确实太大了,那么,只能另寻他径了。
降低复杂度的方式:以空间换时间,这里我们可以用到一个数组来记录字符出现过没有。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int last[256]; //字符的ascII码0~256,记录字符最终出现的位置
memset(last,-1,sizeof(int)*256);//初始化为-1
int idx = -1;//用来当前字串开始的位置
int max = 0;
for(int i = 0; i < s.length() ; ++i)
{
if(last[s[i]]>idx) //①如果这个字符出现过,则令idx等于上一次出现该字符的位置序号
{
idx = last[s[i]];
}
if(i -idx > max){ ②//记录最大的无重复字串
max = i-idx;
}
last[s[i]] = i;③
}
return max;
}
};
以abca为例:
i=0:查表last[‘a’] = -1,①不执行 idx=-1, ②执行max =1,更新last表 last[‘a’] = 0;
i=1:查表last[‘b’] = -1,①不执行 idx=-1, ②执行max =2,更新last表 last[‘b’] = 1;
i=2:查表last[‘c’] = -1,①不执行 idx=-1, ②执行max =3,更新last表 last[‘c’] = 2;
i=3:查表last[‘a’] = 0,①执行,idx=0 , ②执行max =3,更新last表 last[‘a’] = 3;
…..
上述算法的时间复杂度为O(n),Accepted!
【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters的更多相关文章
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
- Leetcode 3. Longest Substring Without Repeating Characters (Medium)
Description Given a string, find the length of the longest substring without repeating characters. E ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- 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 exa ...
随机推荐
- Android N(7.0) 被美翻的新特性!
Tamic 专注移动开发!更多文章请关注 Csdn: http://blog.csdn.net/sk719887916/article/details/52612444 $ http://www.ji ...
- Android开发学习之路--Drawable mutations
时间过得很快,明天终于可以拿到房子了,交完这次房租,也可以成为房东了,看看博客也好久没有更新了,最近一直在整机器人,也没有太多时间整理博客. 今天下午和同事一起遇到了一个问题,就是明明没有改变 ...
- EBS技术开发之VPD策略
VPD (虚拟专用数据库的简称),主要作用是根据运行环境的上下文,隐式的添加条 件. 好处是在数据库层解决细粒度的角色权限访问,避免在中间层写大量代码:坏处 是数据屏蔽的逻辑太隐蔽了,对于分析查找问题 ...
- 探究java接口中的变量与方法
关于变量 java接口里的变量都是默认 pubic static final的 为啥? public 接口得能被所有对象调用 static 这个变量是属于接口本身,而不是实现了接口的对象的 具体来说 ...
- [Ubuntu] 14.04 关闭桌面
一直在用Ubuntu的桌面来做调试环境,最近发现桌面会有崩溃的时候,占用资源也比较大,所以想把桌面关闭,只用command界面. 我的系统是Ubuntu14.04 Ctrl+Alt+F1 可以转到命令 ...
- springMVC源码分析--拦截器HandlerExecutionChain(三)
上一篇博客springMVC源码分析--HandlerInterceptor拦截器调用过程(二)中我们介绍了HandlerInterceptor的执行调用地方,最终HandlerInterceptor ...
- java设计模式-----单例设计模式
设计模式是个很高深的东西,我也是略懂皮毛,下面让我用最简洁易懂的语言描述下单例设计模式吧. 一些人总结出来用来解决特定问题的固定的解决方案. 解决一个类在内存中只存在一个对象,想要保证对象的唯一. 1 ...
- ToolBar与AppcompatAcitivity实现浸入式Statusbar效果
toolbar是android sdk API21新增的组件,下面是谷歌官方的介绍文档: A standard toolbar for use within application content. ...
- Java spi机制浅谈
最近看到公司的一些框架和之前看到的开源的一些框架的一些服务发现和接入都采用了java的spi机制. 所以简单的总结下java spi机制的思想. 我们系统里抽象的各个模块,往往有很多不同的实现方案,比 ...
- 给你的流添加缓冲装置——字节块ByteChunk
这是一个很重要的一个字节数组处理缓冲工具,它封装了字节缓冲器及对字节缓冲区的操作,包括对缓冲区的写入.读取.扩展缓冲区大小等等,另外还提供相应字符编码的转码操作.此工具让缓冲操作变得更加方便,除了缓冲 ...