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" ...
随机推荐
- java实现https免证书认证
java实现https免证书认证 解决方法: 1.下载两个包,httpclient-4.2.jar和httpcore-4.2.jar,复制以下代码就可使用. 2.调用类代码: String htt ...
- mysql 对应数据库服务器配置 所能承受的tps和qps
总结: 吞吐量实际涵盖了TPS 和 QPS TPS 是指产生事物的请求,比如对数据库 增.删.改 QTP 是对数据库查询动作,无逻辑非事物,比如 查询 假如脚本里面都是get请求,那么出来的吞吐量就是 ...
- python迭代器、生成器、装饰器之迭代器
迭代是Python最强大的功能之一,是访问集合元素的一种方式. 一般分为可迭代对象,迭代器,可迭代对象不一定是迭代器,但迭代器一定是可迭代对象 1.可以直接作用于for循环的数据类型 第一类:集合数据 ...
- 微信服务号一些记录,与DTCMS微信功能二次开发
1.首先必须获得Token CRMComm crm = new CRMComm(); string error = ""; string ...
- Cas(02)——部署Cas Server
部署Cas Server Cas应用都需要有一个Cas Server.Cas Server是基于Java Servlet实现的,其要求部署在Servlet2.4以上版本的Web容器中.在此笔者将其部署 ...
- 数据链路层学习之LLDP
数据链路层学习之LLDP 2013年09月02日 20:38:36 goodluckwhh 阅读数 42323 一.LLDP协议概述 随着网络技术的发展,接入网络的设备的种类越来越多,配置越来越复 ...
- linux下的进程间通信之消息队列
概念: 进程彼此之间可以通过IPC消息进行通信.进程产生的每条消息都被发送到一个IPC消息队列中,这条消息一直存放在队列中,直到另一个进程将其读走为止. 优点:可以通过发送消息来几乎完全避免命名管道的 ...
- SVN安装使用【转】
SVN使用教程总结 SVN简介: 为什么要使用SVN? 程序员在编写程序的过程中,每个程序员都会生成很多不同的版本,这就需要程序员有效的管理代码,在需要的时候可以迅速,准确取出相应的版本. Sub ...
- 共享文件word / excel /ppt 被用戶自己锁定无法编辑-解決方法
共享文件word / excel /ppt 被用戶自己鎖定無法編輯,但用戶嘗試過關閉所有文件和重啓過系統,依然無法編輯. 搜到解決方法: Just in case someone looking fo ...
- 个人总结2019 ASP.NET面试题
1.什么是面向对象? 面向对象就是把一个人或事务的属性,比如名字,年龄这些定义在一个实体类里面.存和取的时候直接使用存取实体类就把这个人的名字,年龄这些全部存了,这个实体类就叫对象,这种思想就叫面向对 ...