leetcode-3 最长无重复字串
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.遍历字符串
2.以每个字符串为基准,向后搜索,暂存不重复的字符,遇到重复字符,结束内循环,统计不重复字符个数,更新结果。
时间复杂度:O(n3)
空间复杂度:> O(n)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res = ;
int len = s.length();
if(len <= )
return len;
for(int i=; i<len; i++)
{
string tmpStr = "";
int k = i;
while(!find(tmpStr, s[k]) && k < len)
{
tmpStr += s[k];
k++;
}
if(tmpStr.length() > res)
res = tmpStr.length();
}
return res;
}
//搜索元素是否存在(已经记录过)
bool find(string str, char c)
{
for(int j=; j<str.length(); j++)
if(str[j] == c)
return true;
return false;
}
};
优化?改进搜索复杂度
使用string find(char c)函数来替换我自己实现的find()函数,果然快了好多。
时间复杂度:大于O(n2) 小于 O(n3)
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int res = ;
int len = s.length();
if(len <= )
return len;
for(int i=; i<len; i++)
{
string tmpStr = "";
int k = i;
//使用string find函数替换我的find函数
while(tmpStr.find(s[k]) == tmpStr.npos && k < len)
{
tmpStr += s[k];
k++;
}
if(tmpStr.length() > res)
res = tmpStr.length();
}
return res;
}
};
当我使用unordered_map<char, char>来存储元素后,发现用时和空间没有继续减小,反而增大了许多,几乎与最开始采用的方法一致了。比较奇怪!
那么有没有办法消除一层循环呢?
待续......
leetcode-3 最长无重复字串的更多相关文章
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- [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 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)
这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- Leetcode(3)无重复字符的最长子串
Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...
- 最长无重复子串问题 leetcode 3
一.代码及注释 class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(); //字符串的长度 ...
- lintcode: 最长无重复字符的子串
题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 ...
- lintcode-384-最长无重复字符的子串
384-最长无重复字符的子串 给定一个字符串,请找出其中无重复字符的最长子字符串. 样例 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc" ...
随机推荐
- Django和Flask这两个框架对比
Flask 在 Django 之后发布,现阶段有大量的插件和扩展满足不同需要 Django发布于2005年,Flask创始于2010年年中. Django功能大而全,Flask只包含基本的配置, D ...
- Mysql字段修饰符(约束)
(1).null和not null not null不可以插入null,但可以插入空值. 数值型.字符型.日期型都可以插入null,但只有字符型可以插入空值. 使用方法如下: mysql> cr ...
- hadoop:/bin/bash: /bin/java: No such file or directory
Stack trace: ExitCodeException exitCode=127 In HADOOP_HOME/libexec/hadoop-config.sh look for the if ...
- 原创:Mac AppleScript 自动登录两个QQ
前提,已有登录过的账号,且没有设置为自动登录 tell application "QQ" activate tell application "System Events ...
- MySQL复制表结构
示例SQL: create table testdb.test_table_back like testdb.test_table
- Docker从入门到动手实践
一些理论知识,我这里就不累赘了 docker 入门资料,参考:https://yeasy.gitbooks.io/docker_practice/content/ Dockerfile常用命令,图片来 ...
- 码云clone提示“you do not have permission to pull from the repository”
使用git进行项目下载,换了电脑,配置了账号和邮箱后,pull一个私有项目的时候,出现如下问题: 原因分析: 由于没有设置Gitee的SSH公钥.生成公钥和配置公钥的办法,可以参考Gitee帮助里面的 ...
- 20190722java学习习惯小结
1.周一——周六: 学习: 周日: 巩固练习测试. 2.java 大数据. python 人工智能 .. 3.写技术博客! 4.python应用: 人工智能.web开发.自动化运维.数据分析.爬虫.游 ...
- 解决Maven依赖本地仓库eclipse报错的问题
一.应用场景 有时候项目报红色的感叹号错误也是由于项目中没有导入相关jar报导致报错 为了使用maven强大的包依赖管理和项目管理功能,故在项目中使用maven2作为项目建构工具. 但是我的项目在内网 ...
- boost::bind四种应用场景的例子
普通函数 int f( int a, int b ){return a + b;}boost::bind( f, _1, 9 )( 1 ) 成员函数 struct demo{int f( in ...