LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
problem:
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.
thinking:
(1)寻找子串,要做一次遍历。
(2)暴力破解法,检查每个子串,利用hash table的映射思想。开一个数组,下标为字符的ASCII码,因为没有说明字符串的类型,能够採取一些措施压缩hash table的大小。暴力破解法的最坏时间复杂度为O(n*n)
(3)发现一个O(n)的算法。非常赞
事实上,这里非常多工作都是反复的、没用的。
看一个样例:
S="abbdeca"。
t1="abbdeca",t1[1]==t1[2]。
t2="bbdeca"。t2[0]==t2[1]。
t3="bdeca",一直扫描到最后。
t4="deca"、t5、t6、t7都同上。
我们在处理t1的时候已经扫描到了s[2],然后处理t3的时候扫描了s[2]到s[6],这两个子串已经扫描完了整个母串。
换言之,能使得子串停止扫描的位置仅仅有两处:1.s[2]。2.s[6](结尾)。
对于还有一个样例S="aaab",能使子串停止扫描的位置各自是:s[1],s[2],s[3](结尾)。
所以我们能够考虑仅仅扫描母串,直接从母串中取出最长的无反复子串。
对于s[i]:
1.s[i]没有在当前子串中出现过。那么子串的长度加1;
2.s[i]在当前子串中出现过,出现位置的下标为j。那么新子串的起始位置必须大于j,为了使新子串尽可能的长,所以起始位置选为j+1。
code:
说明:凝视的解法复杂度为O(n*n),没凝视的位O(n)
#include <iostream>
#include <string>
#include <memory.h> using namespace std;
/*
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int a[100]; //压缩hash table
memset(a,0,sizeof(int)*100);
int length = s.size();
int index=0;
int max=0;
for(int i=0;i<length;i++)
{
int j=i;
while((j<length)&&(a[s.at(j)-32]==0))//压缩hash table
{
a[s.at(j)-32]=1;
index++;
j++;
}
memset(a,0,sizeof(int)*100);
max=(max>index)?max:index;
index=0;
if(j==length-1) //这里也有一个小技巧。能够有效减少时间复杂度
break;
}
return max;
}
};
*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int locs[256];//保存字符上一次出现的位置
memset(locs, -1, sizeof(locs)); int idx = -1, max = 0;//idx为当前子串的開始位置-1
for (int i = 0; i < s.size(); i++)
{
if (locs[s[i]] > idx)//假设当前字符出现过,那么当前子串的起始位置为这个字符上一次出现的位置+1
{
idx = locs[s[i]];
} if (i - idx > max)//这里是关键! !!!!! ! !!
{
max = i - idx;
} locs[s[i]] = i;
}
return max;
}
};
int main()
{
string str = "abcdab";
Solution mysolution;
cout<<mysolution.lengthOfLongestSubstring(str)<<endl; }
LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题的更多相关文章
- [LeetCode 题解]: Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- LeetCode题解——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 Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- 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 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [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 ...
随机推荐
- Python多线程学习(一、线程的使用)
Python中使用线程有两种方式:函数或者用类来包装线程对象. 1. 函数式:调用thread模块中的start_new_thread()函数来产生新线程.如下例: import thread de ...
- Rsync同步神器
Rsync清理大批量垃圾数据 在Linux下删除海量文件的情况,需要删除数十万个文件.这个是之前的程序写的日志,增长很快,而且没什么用.这个时候,我们常用的删除命令rm -fr * 就不好用了,因为要 ...
- BZOJ1010: [HNOI2008]玩具装箱toy(dp+斜率优化)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 12451 Solved: 5407[Submit][Status][Discuss] Descript ...
- (转)ORA-01502
问题:ora-01502 索引或这类索引的分区处于不可用状态 引发:移动数据表分区,导致索引失效 解决:重建失效索引 1. select index_name ,status from user_i ...
- solr 4.8+mysql数据库数据导入 + mmseg4j中文全文索引 配置笔记
转载请标明出处:http://www.cnblogs.com/chlde/p/3768733.html 1.如何将solr部署,请参考之前的文章 2.按上述配置好后,在solr_home文件夹中,将包 ...
- nodeJs的一些常识知识
在项目目录中打开命令窗口 1. npm init 生成一个 package.json.(npm inii -y 直接生成,不用确定). 2.npm i . 下载 package.json devDep ...
- Java中方法重载
方法重载:指在同一个类中,允许存在一个以上的同名方法,只要它们的参数列表不同即可,与修饰符和返回值类型无关. 参数列表:个数不同,数据类型不同,顺序不同. 重载方法调用:JVM通过方法的参数列表,调用 ...
- 跳出语句 break continue
break 使用场景:终止switch或者循环 在选择结构switch语句中 在循环语句中 离开使用场景的存在是没有意义的 public static void main(String[] args) ...
- PHP 时间处理
1:获取当前日期格式时间 date("Y-m-d H:i:s"); 2:转化为时间戳 strtotime( date("Y-m-d") ) 3:转化为日期 ...
- 理解Faster-RCNN 中的Anchor
先上图看一下Faster R-CNN操作流程: 图片说明:Faster R-CNN=Fast R-CNN+RPN,其中Fast R-CNN结构不变:RPN负责生成proposals,配合最后一层的f ...