一、题目

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. 题解 P5712 【【深基3.例4】Apples】

    题目传送门 思路 仔细读题后,我们可以发现,输出可以分成\(2\)种情况,apple加s与apple不加s,所以我们可以使用if/else来实现. 接着,我们读入n. int n; cin>&g ...

  2. UML 建模 -- 基础知识

    1.UML简介 UML(Unidied Modeling Language)为面向对象软件设计提供统一的,标准的,可视化的建模语言.适用于以用例为驱动,以体系结构为中心的软件设计全程 2.UML模型的 ...

  3. C#在运行时自动引入正确的Dlls

    设置程序Dlls的搜索路径 1.导入Native函数 /// <summary> /// 设置Dlls的搜索路径 /// </summary> /// <param na ...

  4. MySQL的修改和删除数据表字段

    MySQL的修改和删除数据表字段 写在前面: 数据库存在的意义:数据存储和数据管理. 数据库:行(数据),列(字段) 注意:本页是解决了列的字段问题.下一页是解决行的数据问题. 注意,所有的字段名,最 ...

  5. python接口自动化之pytest环境准备与入门(五)

    安装的pytest版本应该与安装的python版本对应,不然会有问题 (我的环境是python3.6与pytest4.5.0) 1.安装pytest pip install pytest==4.5.0 ...

  6. WebService 之 SoapHeader

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  7. (转)HashMap和HashTable源码

    转自: http://www.cnblogs.com/ITtangtang/p/3948406.html http://frankfan915.iteye.com/blog/1152091 一.Has ...

  8. sql server和my sql 命令(语句)的区别,sql server与mysql的比较

    sql与mysql的比较 1.连接字符串sql  :Initial Catalog(database)=x;  --数据库名称      Data Source(source)=x;        - ...

  9. 栈的简单应用之中缀表达式转后缀表达式(C语言实现逆波兰式)

    一.前言   普通人在书写计算式时会选择中缀表达式,这样符合人脑的认知习惯.可计算机处理时后缀表达式才能使处理速度更快,其原因是利用堆栈结构减少计算机内存访问.同时它也是一个很好锻炼栈这个数据结构的应 ...

  10. python中使用anaconda对不平衡数据的处理包imblearn的安装

    为了建模,处理不平衡数据,想使用SMOTEENN方法进行数据平衡处理,为此需要下载对应的包imblearn   最开始直接从anaconda中进行: conda install  imblearn 报 ...