要开学了,不开森。键盘声音有点大,担心会吵到舍友。今年要当个可爱的技术宅呀~

题目: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的更多相关文章

  1. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  2. leetcode: longest substring without repeating characters

    July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  4. [LeetCode]Longest Substring Without Repeating Characters题解

    Longest Substring Without Repeating Characters: Given a string, find the length of the longest subst ...

  5. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  6. LeetCode:Longest Substring Without Repeating Characters(最长不重复子串)

    题目链接 Given a string, find the length of the longest substring without repeating characters. For exam ...

  7. LeetCode——Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  8. [Leetcode] Longest Substring Without Repeating Characters (C++)

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  9. [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. 【NOIP 2015】Day2 T3 运输计划

    Problem Background 公元 \(2044\) 年,人类进入了宇宙纪元. Description 公元\(2044\) 年,人类进入了宇宙纪元. $L $国有 \(n\) 个星球,还有 ...

  2. HDU 5583 Kingdom of Black and White(暴力)

    http://acm.hdu.edu.cn/showproblem.php?pid=5583 题意: 给出一个01串,现在对这串进行分组,连续相同的就分为一组,如果该组内有x个数,那么就对答案贡献x* ...

  3. Go 灵活多变的切片Slice

    我们知道数组定义好之后其长度就无法再修改,但是,在实际开发过程中,有时候我们并不知道需要多大的数组,我们期望数组的长度是可变的, 在 Go 中有一种数据结构切片(Slice) 解决了这个问题,它是可变 ...

  4. trackViewer 氨基酸位点变异位置图谱展示

    内容中包含 base64string 图片造成字符过多,拒绝显示

  5. Program type already present:okio.AsyncTimeout$Watchdog Message{kind=ERROR, text=Program type :okio

    在app中的build.gradle中加入如下代码, configurations { all*.exclude group: 'com.google.code.gson' all*.exclude ...

  6. idea 启动时报 error:java 无效的源发行版

    说白了就是编译的版本不符合,有的地方是jdk1.7 有的地方是jdk1.8 所以你只要每个地方都保持一致就行. 每个地方!! 每个地方!! 每个地方!! 重要的设置说三遍! 以jdk1.7为例 fil ...

  7. STL_map.VC6简单使用例子

    1. #include <windows.h> //使用map时会出现如下警告:主要意思是 identifier was truncated to '255' characters in ...

  8. 数据库 Mysql 使用,优化,索引

    数据库事务的隔离级别,由低到高 : READ UNCOMMITTED(读未提交数据):允许事务读取未被其他事务提交的变更数据,会出现脏读.不可重复读和幻读问题. READ COMMITTED(读已提交 ...

  9. python静态方法、类方法

    常规: class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating'%s ...

  10. leecode第五十三题(最大子序和)

    class Solution { public: int maxSubArray(vector<int>& nums) { int len=nums.size(); )//特殊情况 ...