【一天一道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 ...
随机推荐
- apply函数用法
procedure: (apply proc arg1 ... args) Proc must be a procedure and args must be a list. Calls proc ...
- SublimeText3解决中文乱码
1)安装Sublime Package Control. 在Sublime Text 3上用Ctrl+-打开控制台并在里面输入以下代码,Sublime Text 2就会自动安装Package ...
- Spring之DAO模块
Spring的DAO模块提供了对JDBC.Hibernate.JDO等DAO层支持 DAO模块依赖于commons-pool.jar.commons-collections.jar Spring完全抛 ...
- Servlet - 基础
Servlet 标签 : Java与Web HTTP协议 HTTP(hypertext transport protocol),即超文本传输协议.这个协议详细规定了浏览器(Browser)和万维网服务 ...
- 基于Web在线考试系统的设计与实现
这是一个课程设计的文档,源码及文档数据库我都修改过了,貌似这里复制过来的时候图片不能贴出,下载地址:http://download.csdn.net/detail/sdksdk0/9361973 ...
- Android之Animation动画各属性的参数意思(二)
现在就来讲讲Animation里这四个标签的属性. 一.这四个标签alpha.scale.translate.rotate共有的属性为: android:duration 动画持续时间, ...
- Linux目录架构详解
Linux和Windows操作系统的显著区别之一就是目录架构的不同.Linux操作系统的目录架构遵循文件系统层级结构标准.不知你是否使用ls命令浏览过Linux的根目录"/",亲爱 ...
- linux中查看现在使用的shell是ksh还是bash?以及怎样修改?
查看系统支持的shell: cat /etc/shells 查看现在使用的shell: 修改默认shell: 另外,修改了系统默认shell之后不会立即生效,之后再次登录系统修改的shell才会生 ...
- Android初级教程使用服务注册广播接收者监听手机解锁屏变化
之前第七章广播与服务理论篇写到: 特殊的广播接收者(一般发广播次数频率很高) 安卓中有一些广播接收者,必须使用代码注册,清单文件注册是无效的 屏幕锁屏和解锁 电量改变 今天在这里就回顾一下,且用代码方 ...
- Collections类解析
最常用的排序: 需要实现Comparable接口 1.什么是Comparable接口 此接口强行对实现它的每个类的对象进行整体排序.此排序被称为该类的自然排序 ,类的 compareTo 方法被称为它 ...