刷题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, ...
随机推荐
- Docker最全教程——从理论到实战(二十三)
如何节约云端成本? 上云在大部分情况下就是为了降低成本,在这方面,主流的容器服务基本上都能够有效地降低成本——不仅能够高效自动化的管理和控制容器,而且不需支付Kubernetes 主节点的费用.不过, ...
- vue常用插件之视频播放(rtmp m3u8)
vue-video-player(5.0.2) 最近我的项目做了一个监控视屏的显示,后台提供的视屏格式是rtmp 后来又改为m3u8,没成功,原因是占用内存过大,所以取消了这种方式 一.安装 npm ...
- vue(一)--监听事件
1.vue-on:监听事件: demo:点击按钮,number+1 v-on 还可以缩写为 @ 2.事件修饰符 .stop:等同于JavaScript中的event.stopPropagation() ...
- java - 锁的种类及详解
锁类型 锁根据其特性能够划分出各种各样的锁类型,该文主要介绍以下锁的作用及特性 乐观锁/悲观锁 独享锁/共享锁 互斥锁/读写锁 可重入锁 公平锁/非公平锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 ...
- 6.Dockerfile 指令
概述 我们已经介绍了 FROM,RUN,还提及了 COPY, ADD,其实 Dockerfile 功能很强大,它提供了十多个指令.下面我们继续讲解其他的指令. COPY 格式: COPY <源路 ...
- Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Type
问题描述 将项目挂载到 Myeclipse 的 tomcat 上,启动 tomcat ,报错“Initialization of bean failed; nested exception is ja ...
- solr es调优化和问题排查
(1)TOP 显示当前进程状态,结合 ps -aux 可以看是哪一个服务.mpstat 可以看是cpu的负载 (2)TOP -H -u 用户名 显示该用户下 所有的线程. 还有pstree (3)js ...
- 为QT应用程序添加图标 转
第1,第2种方法经本人验证可行 1:通过qmake生成makefile实现过程: (1) 找到一张图片.ico,名字改为myappico.ico: (2) 创建一个新的文本文档,内部添加 IDI_I ...
- 笔记(tm_springboot)
1.@change="getFile($event)" 2.File imageFolder= new File(request.getServletContext().getRe ...
- mysql 对数据的自增ID重新进行排序
创建表格时添加: create table table1(id int auto_increment primary key,...) 创建表格后添加: 删除原有主键: ALTER TABLE `ta ...