leetcode-algorithms-3 Longest Substring Without Repeating Characters
leetcode-algorithms-3 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.
Example 2:
Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:
Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
解法1
一步步检查所有字串看是否有重复的字符
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
string longest;
for (int i = 0; i < s.size(); ++i)
{
for (int j = s.size() - i; j > 0; --j)
{
string longesttemp = s.substr(i, j);
bool repect = false;
int a[256] = {0};
for (int n = 0; n < longesttemp.size(); ++n)
{
int x = longesttemp[n];
++a[x];
if (a[x] > 1)
{
repect = true;
break;
}
}
if (!repect && (longesttemp.size() > longest.size())) longest = longesttemp;
}
}
return longest.size();
}
};
时间复杂度: O(n^3).
空间复杂度: O(min(n,m)).
解法2
解法1的时间复杂度太高了,效率低下.字符串查找要怎么提高效率,首先都应该想到Sliding Window算法.设定一个窗口[i, j],将i到j的内容存入map,下面滑动j,如果s[j] (s表示字符串)在map中存在,表示字符串已经重复了,记下最大值,然后将i窗口滑到s[j]上次的位置.
下面是代码的实现:
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int n = s.length();
std::unordered_map<char, int> m;
int len = 0;
for (int i = 0, j = 0; j < n; ++j)
{
auto fiter = m.find(s[j]);
if (fiter != m.end())
{
i = (fiter->second) > i ? fiter->second : i;
}
int temp_len = j - i + 1;
len = (len > temp_len) ? len : temp_len;
m[s[j]] = j + 1;
}
return len;
}
};
时间复杂度: O(n).只有一个j循环.
空间复杂度: O(m).m是最大的不重复子串的长度.
解法3
对于解法2有没更快捷的方式.参考KMP算法对字符串的处理,我们可以将字符存入整型数组,值存储字符所在的位置,这样就可以在算法2的基础上少一次查询.
class Solution
{
public:
int lengthOfLongestSubstring(string s)
{
int n = s.length();
int len = 0;
int index[128] = {0};
for (int i = 0, j = 0; j < n; ++j)
{
i = i > index[s[j]] ? i : index[s[j]];
int temp_len = j - i + 1;
len = (len > temp_len) ? len : temp_len;
index[s[j]] = j + 1;
}
return len;
}
};
时间复杂度: O(n).
空间复杂度: O(128).
leetcode-algorithms-3 Longest Substring Without Repeating Characters的更多相关文章
- 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
- 【LeetCode OJ】Longest Substring Without Repeating Characters
题目链接:https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题目:Given a string ...
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】003. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
随机推荐
- Async、Await
Async.Await:net4.x新增的异步编程方式: 目的:为了简化异步程序编写 Async方式, 使用Async标记Async1为异步方法, 用Await标记GetRequestStreamAs ...
- 以结算价交易TAS和以市价交易TAM
CME Group的合约规格中提到TAS和TAM交易,如:Gold Futures Contract Specs Gold 期货 合约规格 Trading at Settlement (TAS) is ...
- 常用模块(json/pickle/shelve/XML)
一.json模块(重点) 一种跨平台的数据格式 也属于序列化的一种方式 介绍模块之前,三个问题: 序列化是什么? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化. 反序列化又是什么? 将 ...
- HDU 5726 GCD(RMQ+二分)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5726 题意:给出一串数字,现在有多次询问,每次询问输出(l,r)范围内所有数的gcd值,并且输出有多 ...
- 【Selenium2】【selenium之 定位以及切换frame(iframe)】
参考:http://blog.csdn.net/huilan_same/article/details/52200586 总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切, ...
- ES6中Promise的入门(结合例子)
一.Promise的前言 解决回调地狱 //以往回调方式 函数1(function(){ //代码执行...(ajax1) 函数2(function(){ //代码执行...(ajax2) 函数3(f ...
- 【BZOJ】3295: [Cqoi2011]动态逆序对
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3295 mamaya,弱鸡xrdog终于会写树套树啦.... 将树状数组中每一个节点看成一棵 ...
- Jquery中的DOM操作:
DOM是 Document Object Model的缩写,是一种与浏览器,平台,语言无关的接口,使用该接口可以访问页面中所有的标准组件,下面介绍一下常用的一些DOM操作: 选择节点: 将在下篇博客中 ...
- (一)PHP简介
什么是 PHP? PHP 是 "PHP Hypertext Preprocessor" 的首字母缩略词 PHP 是一种被广泛使用的开源脚本语言 PHP 脚本在服务器上执行 PHP ...
- Java Virtual Machine(Java虚拟机)
JVM是Java Virtual Machine(Java虚拟机)的缩写,JVM是一种用于计算设备的规范,它是一个虚构出来的计算机,是通过在实际的计算机上仿真模拟各种计算机功能来实现的. Java语言 ...