C++ leetcode Longest Substring Without Repeating Characters
要开学了,不开森。键盘声音有点大,担心会吵到舍友。今年要当个可爱的技术宅呀~
题目:Given a string, find the length of the longest substring without repeating characters.
Examples: Given "abcabcbb", the answer is "abc", which the length is 3.
第一反应的解法,直接贴代码吧,38ms的运行时间,击败58%。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
string result(""); //用来存储目前没有重复的子串
int tmp = ; //没有重复字母子串的最大长度
int now = ;
for(int i = ; i != s.size(); i++){
int pos = -;
if ( (pos = result.find(s[i]) )== -)
result = result + s[i];
else{
tmp = result.size()>tmp?result.size():tmp;
result = (pos+==result.size()?"":result.substr(pos+)) + s[i];
}
}
return result.size()>tmp?result.size():tmp;
}
};
很简单的解法,我却写了蛮久的,因为对string不熟悉,而且没考虑越界,学到了两个string的成员函数,一个是find(),另一个是截取子串的函数substr()。自己最智障的地方就是截取子串的时候索引写错了竟然一直都没反应过来,哎,脑子真是个好东西啊。
速度很慢,想把字符串转成hash函数做,但是没想好具体的解法。看一眼大神的,6ms的,有想法了。OK~23ms,击败了65%,下面这个代码和原来的代码算法相似,都是用滑动窗口的做法,但是下面这个解法将find()函数改成了用hash表实现,节省了遍历原串时在子串中查找有没有这个字母的时间,而且将找到现在情况下的最长子串不赋值给一个新的string,只用指针start标注该子串在原串中的起始位置,节省了赋值时间。不过更新start值时,直接将该字母出现的前一个位置+1赋给了start,没有考虑现在子串的起始位置,照这个bug找了好久,脑子啊,我什么时候才能有啊!哭唧唧!但是明明和大神解法一样,怎么测试时间差这么多?喵喵喵?
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int flag[];
int start = ;
int longest = ;
for (int i = ; i <; i++)
flag[i] = -;
int i;
for(i = ; i< s.size(); i++){
if (flag[s[i]] != -)
start = (flag[s[i]] + )>start?flag[s[i]]+:start;
longest = longest >= (i-start+)?longest:(i-start+);
flag[s[i]] = i;
}
return longest;
}
};
好啦,水完了今天的博客和LeetCode~找饭吃~
C++ leetcode Longest Substring Without Repeating Characters的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode]Longest Substring Without Repeating Characters题解
Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)
题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...
- LeetCode——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [Leetcode] Longest Substring Without Repeating Characters (C++)
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- vim命令详解
VIM编辑常用技巧 vim编辑器 简介: vi: Visual Interface,文本编辑器 文本:ASCII, Unicode 文本编辑种类: 行编辑器: sed 全屏编辑器:nano, vi V ...
- NOI1999 生日蛋糕
#include<iostream> #include<cstdio> #include<cmath> using namespace std; #define I ...
- 每天一个小程序—第0001题(uuid模块)
第 0001 题: 做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 一开始以为是 ...
- 51nod 1615 跳跃的杰克
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1615 题意: 思路:一开始是觉得一旦超过了终点,中间某个地方往相反地方跳 ...
- 原生js总结
数据类型 基本类型值包括: undefined,null,Boolean,Number和String,这些类型分别在内存中占有固定的大小空间,它们的值保存在栈空间,我们通过按值来访问的. 引用类型包括 ...
- 两个DIV并排显示
今天做的一个项目,需要做3个div,一个是总框(Div1),另外两个是子框,按比例填满div1,我设置好两个div的width和height,发现效果是两个子div上下显示,如图所示: 要想将两个DI ...
- P3110 [USACO14DEC]驮运Piggy Back
传送门 做过次短路后,再来做这题感觉轻松不少. 这题看着就像最短路模板题. 思路: 虽说题目看起来比较水,但是码起来还是有点难度的.(对我这个蒟蒻来说) 这道题,跟"路障"一题差不 ...
- CentOS7下搭建Nginx+PHP7的安装配置
一.安装编译工具及库文件: yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 环境要求 nginx是C ...
- Python self,init,对象属性
self关键字的作用 __init__初始化 # coding=utf-8 支持中文 class Human(object): laugh = 'Ha' def show_laugh(self): p ...
- Total Commander
Total Commander 是一款应用于 Windows 平台的文件管理器 ,它包含两个并排的窗口,这种设计可以让用户方便地对不同位置的“文件或文件夹”进行操作,例如复制.移动.删除.比较等,相对 ...