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, ...
随机推荐
- hihoCoder 1339 Dice Possibility(DP)
http://hihocoder.com/problemset/problem/1339 题意: 求一个骰子扔n次后最后点数相加为m的概率. 思路: f[i][j]表示扔到第i次时总值为j的概率. # ...
- Runnable、Callable、Executor、Future、FutureTask关系解读
在再度温习Java5的并发编程的知识点时发现,首要的就是把Runnable.Callable.Executor.Future等的关系搞明白,遂有了下述小测试程序,通过这个例子上述三者的关系就一目了然了 ...
- tarjan 缩点(模板)
描述: 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 注:允许多次经过一条边或者一个点,但是,重复经过的点,权值只计算一次. 思路: ...
- linux系统下各类软件安装笔记
安装环境: linux版本:ubuntu 16.04 安装python3.6 sudo add-apt-repository ppa:jonathonf/python-3.6 ...
- 浅谈Linux文件系统
Linux 与其他类 UNIX 系统一样并不区分文件与目录:目录是记录了其他文件名的文件.使用命令 mkdir 创建目录时,若期望创建的目录的名称与现有的文件名(或目录名)重复,则会创建失败. Lin ...
- Tp5.1使用导出Excel
composer require phpoffice/phpexcel 不管它的警告,都能用的. use PHPExcel; use PHPExcel_IOFactory; public static ...
- eclipse导入项目文件以及 import项目文件后有个红色感叹号
eclipse导入项目文件 步骤:File —>Import—>General—>Existing Projects into Workspace 然后进去选择项目文件的具体路径即可 ...
- 《剑指offer》第五十六题(数组中唯一只出现一次的数字)
// 面试题56(二):数组中唯一只出现一次的数字 // 题目:在一个数组中除了一个数字只出现一次之外,其他数字都出现了三次.请 // 找出那个吃出现一次的数字. #include <iostr ...
- vue-cli3快速创建项目
文档:https://cli.vuejs.org/zh/guide/ 条件: npm 更至最新 node >=8.9 1.全局安装 npm install -g @vue/cli 或 yarn ...
- d3选择全部子节点,不知道class和id
https://github.com/d3/d3-selection/issues/63 selection.selectAll("*")