一、题目

Longest Substring Without Repeating Characters,具体请自行搜索。

这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂。

但要做到bug free有点难度,主要是边界的问题。

二、这个题目,我自己实现,没有参考代码

提交了5次:

第1次: Wrong Answer,主要是“ ”这个空串不对

第2次、第3次:Runtime Error,"au" out of Range

第4次:Wrong Answer,主要是dvdf 不对

第5次:终于对了

三、改进

虽然自己实现了该问题,但边界的考虑不周,总共错误了4次。

目前存在的问题,就是性能不够好。

看看其他人的实现,再写3次。

争取做到bug free,性能足够好。

下面是我的完整代码实现,需要的拿去:

#include<iostream>
using namespace std; class Solution{
public:
int lengthOfLongestSubstring(string s){
int curr = 0,len = s.length();
int start,end,maxLength=0;
if(len == 1){
maxLength = 1;
return maxLength;
}
while(curr<len){
start = curr;
end = start + 1;
if(end>=len){
break;
}else{
string sub = s.substr(start,end-start);
while(end<len && sub.find(s.at(end)) == -1 ){
if(end<=len){
end++;
sub = s.substr(start,end-start);
}else{
break;
}
}
if(maxLength<(end-start)){
maxLength = end - start;
}
}
curr++;
} return maxLength;
}
}; int main(){
Solution s;
cout<<s.lengthOfLongestSubstring("abcabcbb")<<endl;
cout<<s.lengthOfLongestSubstring("bbbbb")<<endl;
cout<<s.lengthOfLongestSubstring("pwwkew")<<endl;
cout<<s.lengthOfLongestSubstring(" ")<<endl;
cout<<s.lengthOfLongestSubstring(" ")<<endl;
cout<<s.lengthOfLongestSubstring("")<<endl;
cout<<s.lengthOfLongestSubstring("au")<<endl;
cout<<s.lengthOfLongestSubstring("dvdf")<<endl;
return 0;
}

网上找一个性能稍微好点也容易理解的:

#include<iostream>
#include<vector>
using namespace std; class Solution{
public: //pwwkew
int lengthOfLongestSubstring(string s){
int res = 0;
vector<int> m(128,0);
for(int i=0,j=0;j<s.size();j++){
if(m[s[j]]++ == 0){
res = max(res,j-i+1);
}else{
while(i<j && m[s[j]]>1){
m[s[i]]--;
i++;
}
}
}
return res;
}
}; int main(){
Solution s;
cout<<s.lengthOfLongestSubstring("abcabcbb")<<endl;
cout<<s.lengthOfLongestSubstring("bbbbb")<<endl;
cout<<s.lengthOfLongestSubstring("pwwkew")<<endl;
cout<<s.lengthOfLongestSubstring(" ")<<endl;
cout<<s.lengthOfLongestSubstring(" ")<<endl;
cout<<s.lengthOfLongestSubstring("")<<endl;
cout<<s.lengthOfLongestSubstring("au")<<endl;
cout<<s.lengthOfLongestSubstring("dvdf")<<endl;
return 0;
}

刷题3. Longest Substring Without Repeating Characters的更多相关文章

  1. 【LeetCode刷题系列 - 003题】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. Example 1:    ...

  3. Leetcode第三题《Longest Substring Without Repeating Characters》

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  4. [刷题] 3 Longest Substring Without Repeating Character

    要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...

  5. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  6. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  7. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  8. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  9. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

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

随机推荐

  1. react-native简单使用

    基本组件的使用介绍 View: Text: TextInput: Image: Button: ActivityIndicator: ScrollView:这是一个列表滚动的组件 ListView:也 ...

  2. 在Java后端如何添加拦截器

    在安全编码规范中,在Java后端controller层接口需要对调用者的身份进行确认,以防非法用户进行访问.若是在controller层的每个接口处都添加逻辑判断,那么代码重复度高,并且费力费时.此时 ...

  3. Currency Exchange POJ - 1860 spfa判断正环

    //spfa 判断正环 #include<iostream> #include<queue> #include<cstring> using namespace s ...

  4. 如何在Windows上开启Ping或者禁止PING

    方法1:命令行模式 进入服务器后 点击 开始——运行 输入命令: netsh firewall set icmpsetting 8 这样就可以在外部ping到服务器了 非常简单实用! 同样道理,如果想 ...

  5. 如何将.sql文件导入到mysql的数据库中

    首先通过cmd的net start mysql57 启动mysql的服务器 然后,输入命令:mysql -h 127.0.0.1 -u root -p来启动mysql服务 最后 上图画红圈的部分是.s ...

  6. P问题,NP问题,NPC问题学习笔记

    参考:https://www.luogu.org/blog/styx-ferryman/chu-sai-bei-kao-gan-huo-p-wen-ti-np-wen-ti-npc-wen-ti-sh ...

  7. 设置Eclipse中的字符集为UTF-8

    Eclipse 修改字符集 默认情况下 Eclipse 字符集为 GBK,但现在很多项目采用的是 UTF-8,这是我们就需要设置我们的 Eclipse 开发环境字符集为 UTF-8, 设置步骤如下: ...

  8. C++——模板、数组类

    1.函数模板:可以用来创建一个通用功能的函数,以支持多种不同形参,进一步简化重载函数的函数体设计. 声明方法:template<typename 标识符> 函数声明 求绝对值的模板 #in ...

  9. start.sh在linux下启动报错 Can't connect to any repository: ,cannot open git-receive-pack

    个人博客 地址:http://www.wenhaofan.com/article/20181223135418 报错信息 Can't connect to any repository: ,canno ...

  10. python3运行调用htmltestrunner时,报错UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0

    之前解决过一次,又忘了,这次写下来了..百度没有的,跟我环境有关! 环境:自动化运行环境python3.6.5 上期说到了,写了一个bat来运行runallcase.py. 但是双击运行却报错:Uni ...