刷题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, ...
随机推荐
- CSS之 元素显示隐藏,用户界面样式,文本溢出隐藏,精灵技术,三角形
元素的显示与隐藏 display 显示 display 设置或检索对象是否及如何显示 display: none; 隐藏对象 display: block; 除了转换为块级元素, 同时还有显示元素的意 ...
- Python3标准库:enum枚举
1. enum枚举 枚举是一组符号名称(枚举成员)的集合,枚举成员应该是唯一的.不可变的.在枚举中,可以对成员进行恒等比较,并且枚举本身是可迭代的. 1.1 创建枚举 可以使用class语法派生Enu ...
- element-ui的upload组件的clearFiles方法
<template> <div> <el-button @click="clearFiles">重新上传</el-button> & ...
- pytest之assert断言
assert pytest允许您使用标准Python断言来验证Python测试中的期望和值.例如,你可以写下 # content of test_assert1.py def f(): return ...
- 部件MSCOMCTL.OCX或其附件之一不能正确注册:一个文件丢失或无效
部件MSCOMCTL.OCX或其附件之一不能正确注册:一个文件丢失或无效: https://blog.csdn.net/yilese/article/details/71479908
- Xlrd模块读取Excel文件数据
Xlrd模块使用 excel文件样例:
- 网易云信融合CDN方案及实践
日前,网易云信视频云架构师席智勇在第七届GFIC全球家庭互联网大会进行了题为<网易云信融合CDN方案及实践>的分享,以下是演讲内容回顾. 图为 网易云信视频云架构师席智勇 CDN所面临的问 ...
- loadrunner11破解失败,已解决“ license security violation.Operation is not allowed ”问题
参考链接https://blog.csdn.net/yongrong/article/details/7891738,亲测可以解决问题 在64位win7系统中安装LR11时,采用普通的方法无法授权.最 ...
- 杭电oj 2072————统计单词数(java)
problem:统计单词数 思路:利用HashMap的特性——不能反复存储同一个键得数据,所以可以保证map里边儿的元素都是不重复的,存储完毕之后直接输出size就好了 注意事项: 1.利用strin ...
- java 中使用MD5加密 , 入库时对密码进行加密
import lombok.extern.slf4j.Slf4j; import java.security.MessageDigest; @Slf4j public class MD5Util { ...