刷题3. Longest Substring Without Repeating Characters
一、题目
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的更多相关文章
- 【LeetCode刷题系列 - 003题】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. Example 1: ...
- Leetcode第三题《Longest Substring Without Repeating Characters》
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [刷题] 3 Longest Substring Without Repeating Character
要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...
- LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters
LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)
这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...
- (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters
3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...
- 【leetcode刷题笔记】Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- Python之四:控制流
1.If 逻辑判断: if a: b elif c: d else: e 先判断a语句块的值是否为真,如果为真,则执行b语句块,如果不为真则转到elif判断c语句块的值是否为真,如果为真执行d语句块, ...
- vue 查看dist文件里的结构
场景:优化打包后的代码,提高性能. 1.方式一:report-json. 1.1 package.json文件里加入以下命令, "report": "vue-cli-se ...
- 消息队列和Kafka
------20191211闪
- easyui-numberbox后台获取数据后,鼠标一点击就自动清空了
<input type="text" name="txtMeterInitData" id="txtMeterInitData" cl ...
- 微信小程序判断input是否为空
微信小程序中用到input值时候,判断其内容是否为空,可以用if-else判断内容的length,也可以给input加点击事件,判断其内容:以下是我解决问题的过程wxml代码 <view cla ...
- PP: Shape and time distortion loss for training deep time series forecasting models
Problem: time series forecasting Challenge: forecasting for non-stationary signals and multiple futu ...
- .NET知识梳理——4.特性Attribute
1. 特性 1.1 特性Attribute 特性就是一个类,继承自Attribute抽象类(该类无抽象方法.避免实例化),约定俗成用Attribute类结尾,标记时可省略掉Attribu ...
- C# Timer 控件的用法
一.主要的属性 在 Windows 窗体应用程序中,定时器控件(Timer)与其他的控件略有不同,它并不直接显示在窗体上,而是与其他控件连用. Enabled 属性: 用于设置该Timer控件是否可用 ...
- 2019-08-13 纪中NOIP模拟B组
T1 [JZOJ1534] rank 题目描述 小h和小R正在看之前的期末&三校联考成绩,小R看完成绩之后很伤心,共有n个学生,第i个学生有一个总成绩Xi,因为他的排名是倒数第k个,于是小R想 ...
- hibernate跟Mybatis/ ibatis 的区别,为什么选择?(转)
第一章 Hibernate与MyBatisHibernate 是当前最流行的O/R mapping框架,它出身于sf.NET,现在已经成为Jboss的一部分. Mybatis 是另外一种优秀的O/R ...