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 ...
随机推荐
- Oracle 基础学习笔记
知识点 一.登陆数据库: 登陆数据库: sqlplus system/oracle123456 二.新建用户.授权(连接数据库.创建表.表空间.查询某用户下的表) 语法: create user [用 ...
- 从命令行git转到Tortoise
阅读前请注意:本文先学命令行git的,但过不久遇到乱码问题久久不能解决,最后转向tgit. 2016-29 安装 next 下一步 设置目录 勾选需要的项: Additional icons是关于图标 ...
- springboot集成logback日志
简介 spring boot内部使用Commons Logging来记录日志,但也保留外部接口可以让一些日志框架来进行实现,例如Java Util Logging,Log4J2还有Logback. 如 ...
- Codeforces Round #267 (Div. 2) D. Fedor and Essay tarjan缩点
D. Fedor and Essay time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- javascript 获得以秒计的视频时长
<!DOCTYPE html> <html> <body> <h3>演示如何访问 VIDEO 元素</h3> <video id=&q ...
- javaee开发模式
model1模式:技术组成:jsp+javaBeanmodel1的弊端:随着业务复杂性 导致jsp页面比较混乱model2模式:技术组成:jsp+servlet+javaBeanmodel2的优点:开 ...
- django人类可读性
一些Django的‘奇技淫巧’就存在于这些不起眼的地方. 为了提高模板系统对人类的友好性,Django在django.contrib.humanize中提供了一系列的模板过滤器,有助于为数据展示添加“ ...
- VC.遍历文件夹中的文件
1.VC下遍历文件夹中的所有文件的几种方法 - 年少要轻狂 - CSDN博客.html(https://blog.csdn.net/wllmsdn/article/details/27220999) ...
- PHP的session的实现机制
一.默认机制,用磁盘文件来实现PHP会话.php.ini配置:session.save_handler = files 1.session_start() A. session_start()是ses ...
- dockerfile debian 和pip使用国内源
python官方镜像是基于debian的.国内使用时定制一下,加快下载速度. 1 debian本身使用国内源 dockfile中: #国内debian源 ADD sources.list /etc/a ...