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 最长无重复字串的更多相关文章

  1. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  6. Leetcode(3)无重复字符的最长子串

    Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果: ...

  7. 最长无重复子串问题 leetcode 3

    一.代码及注释 class Solution { public: int lengthOfLongestSubstring(string s) { int n = s.size(); //字符串的长度 ...

  8. lintcode: 最长无重复字符的子串

    题目 最长无重复字符的子串给定一个字符串,请找出其中无重复字符的最长子字符串. 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc",其长度为 ...

  9. lintcode-384-最长无重复字符的子串

    384-最长无重复字符的子串 给定一个字符串,请找出其中无重复字符的最长子字符串. 样例 例如,在"abcabcbb"中,其无重复字符的最长子字符串是"abc" ...

随机推荐

  1. 查看linux是几位操作系统

    查看linux是几位操作系统 摘自:https://blog.csdn.net/a34569345/article/details/80179927 2018年05月03日 14:44:44 bill ...

  2. 123457123457#0#-----com.yimeng.TouNaoWangZhe--前拼后广--brain游戏one

    com.yimeng.TouNaoWangZhe--前拼后广--brain游戏one

  3. vs2012编译的程序不能在XP和2003下执行问题的解决方法

    问题如题,通过无数次百度和谷歌后,发现,微软已经确认这是一个缺陷,安装Vs2012的update 3的升级包就可以解决问题.同时,在分发包的地方,vcredist_x86.exe 随程序分发一份就可以 ...

  4. c# 结构体struct注意事项

    c# 结构struct注意事项 1,不能定义无参构造函数,因为隐式的默认无参构造函数不能被重写 2,当为某个结构编写带有参数的构造函数时,必须显式初始化所有成员,否则编译不过. 3,不允许在结构的实例 ...

  5. .Net Core 定时器Quartz

    最近因为项目需要用到了Quartz,下面简单记录一下. 一.首先需要安装Quartz. 二.定义一个执行的Job类,实现IJob接口,接口有一个方法Execute,用来执行定时任务的实现内容. pub ...

  6. sonar:api/ce/submit接口上传失败

    https://blog.csdn.net/weixin_34185320/article/details/87115268 https://ask.helplib.com/others/post_1 ...

  7. MATLAB知识-解决因缺少libsvm 而运行出现Y must be a vector or a character array.

    matlab版本R2014b 最近运行一个使用svmtrain的程序,出现以下错误: 这是因为是在设定路径里面没有libsvm.辛亏有一位师姐的电脑里面有libsvm的包,我直接用了,这样就不需要下载 ...

  8. [转帖]当 K8s 集群达到万级规模,阿里巴巴如何解决系统各组件性能问题?

    改天学习一下. https://www.cnblogs.com/alisystemsoftware/p/11570806.html   当 K8s 集群达到万级规模,阿里巴巴如何解决系统各组件性能问题 ...

  9. 提车应该检查哪?4S店都怕你检查这4个“雷区”,别等后悔才知道

    https://mbd.baidu.com/newspage/data/landingsuper?context=%7B%22nid%22%3A%22news_9381645601643691163% ...

  10. GFS(Google File System,谷歌文件系统)----(1)文件系统简介

    分布式文件系统 系统是构建在普通的.廉价的机器上,因此故障是常态而不是意外 系统希望存储的是大量的大型文件(单个文件size很大) 系统支持两种类型读操作:大量的顺序读取以及小规模的随机读取(larg ...