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, ...
随机推荐
- python实现八皇后问题
import random def judge(state, nextX): #判断是否和之前的皇后状态有冲突 nextY = len(state) for i in range(nextY): if ...
- Perl CGI编程
http://www.runoob.com/perl/perl-cgi-programming.html 什么是CGI CGI 目前由NCSA维护,NCSA定义CGI如下: CGI(Common Ga ...
- 【Selenium2】【selenium之 定位以及切换frame(iframe)】
参考:http://blog.csdn.net/huilan_same/article/details/52200586 总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切, ...
- SpringBoot中加密com.github.ulisesbocchio
Jasypt Spring Boot 为 Spring Boot 项目中的属性源(property sources)提供加密支持. 有三种方法可以在项目中集成 jasypt-spring-boot: ...
- django人类可读性
一些Django的‘奇技淫巧’就存在于这些不起眼的地方. 为了提高模板系统对人类的友好性,Django在django.contrib.humanize中提供了一系列的模板过滤器,有助于为数据展示添加“ ...
- [原][osg][osgEarth]EarthManipulator关于oe漫游器的handle部分解读以及修改(仿照谷歌,修改oe漫游器中focal(视角切换)功能 续 二)
bool EarthManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) ...
- 删除GitHub或者GitLab 上的文件夹,git rm -r --ceched 文件夹名 ,提交commit,git push
方法一 这里以删除 .setting 文件夹为案例 git rm -r --cached .setting #--cached不会把本地的.setting删除 git commit -m 'delet ...
- 《剑指offer》第五十三题(数组中数值和下标相等的元素)
// 面试题53(三):数组中数值和下标相等的元素 // 题目:假设一个单调递增的数组里的每个元素都是整数并且是唯一的.请编程实 // 现一个函数找出数组中任意一个数值等于其下标的元素.例如,在数组{ ...
- Android application backup
警告 AndroidMenifest中application标签下android:allowBackup="true"时,会警告: Warning:On SDK version 2 ...
- 学习笔记1—python基础
1.安装pip: python -m pip install -U pip (打开命令行窗口:Anaconda Prompt) 升级:python -m pip install --upgrade p ...