【一天一道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 ...
随机推荐
- uboot-tiny4412启动流程(下)----如何将自己的裸板测试程序加入uboot中启动测试
今天在工作上搞了一天高通的芯片uboot程序,目的是希望将一个裸板的程序移植到uboot中,并且开机让它运行.这个芯片是NXP4330,目前是高通的一个芯片,基于ARM-contexA9架构,那么就跟 ...
- Android的Intent机制详解
Intent 是一个消息传递对象,您可以使用它从其他应用组件请求操作.尽管 Intent 可以通过多种方式促进组件之间的通信,但其 基本用例主要包括以下三个: 启动 Activity: Activit ...
- SpriteKit游戏Delve随机生成地牢地图一个Bug的修复
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) Delve是一个很有意思的地牢探险类型的游戏,其中每一关的地图 ...
- 在OC代码中创建Swift编写的视图控制器
背景 近日在和一群朋友做项目,我和另一位同学负责iOS客户端,我是一直使用OC的,而他只会Swift,因此在我们分工协作之后,就需要把代码合在一起,这就牵扯到如何在TabbarController中添 ...
- 集合框架之List接口
有序的 collection(也称为序列).此接口的用户可以对列表中每个元素的插入位置进行精确地控制.用户可以根据元素的整数索引(在列表中的位置)访问元素,并搜索列表中的元素. 与 set 不同,列表 ...
- JSP自定义方法库
如果JSTL的方法库没有满足需要,可以使用自定义方法进行扩展 public class Function{ public static int length(Object obj){ //返回对象的长 ...
- socket系列之服务器端socket——ServerSocket类
一般地,Socket可分为TCP套接字和UDP套接字,再进一步,还可以被分为服务器端套接字跟客户端套接字.这节我们先关注TCP套接字的服务器端socket,Java中ServerSocket类与之相对 ...
- Android中Socket通信之TCP与UDP传输原理
一.Socket通信简介 Android与服务器的通信方式主要有两种,一是Http通信,一是Socket通信.两者的最大差异在于,http连接使用的是"请求-响应方式",即在请求时 ...
- HTML5中 HTML格式化/HTML样式/链表/表格-样式 韩俊强的博客
HTML5学习从简单到复杂,循环渐进! 每日更新关注:http://weibo.com/hanjunqiang 新浪微博! 1.HTML格式化 <!DOCTYPE html> <h ...
- Android开发学习之路--Content Provider之初体验
天气说变就变,马上又变冷了,还好空气不错,阳光也不错,早起上班的车上的人也不多,公司来的同事和昨天一样一样的,可能明天会多一些吧,那就再来学习android吧.学了两个android的组件,这里学习下 ...