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

题目: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. 【BZOJ】1832: [AHOI2008]聚会

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1832 省选出出了CF的感觉..... 显然一发贪心,如果两个点显然就是他们的$LCA$(不 ...

  2. codeforces gym 100947 J. Killing everything dp+二分

    J. Killing everything time limit per test 4 seconds memory limit per test 64 megabytes input standar ...

  3. 数据库无法打开到SQL Server连接

    今天打开数据库,发现连接不上,弹出错误提示: 打开SQLServer Configuration Manager,发现SQL Server状态已经停止,双击启动弹出错误提示: 打开SQL Server ...

  4. python 目录切换

    #- * -coding: utf - - * - import os, sys path = "c:\\" # 查看当前工作目录 retval = os.getcwd() pri ...

  5. 传的参数是url地址时需要特殊处理

    <a href="javascript:;" data-url="{$vo.url}" class="info_generate_qr" ...

  6. Git Gui 常见错误

  7. python 修改excel

    操作描述:需要实现数据不断写入的功能,首先,在固定位置建立一个空白的xls文件:其次,每次产生的数据先判断该xls已有几列数据,后缀上去. 具体过程: 要保证具有三个包,是xlrd,xlwt,xlut ...

  8. JS self=this

    1.每个函数都会有自己的this和arguments:this对象绑定运行环境,arguments绑定调用参数. 2.全局函数:this和全局环境绑定,浏览器指向全局window对象(node.js中 ...

  9. Python Scrapy 爬虫框架实例(一)

    之前有介绍 scrapy 的相关知识,但是没有介绍相关实例,在这里做个小例,供大家参考学习. 注:后续不强调python 版本,默认即为python3.x. 爬取目标 这里简单找一个图片网站,获取图片 ...

  10. java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合

    java8新特性: lambda表达式:直接获得某个list/array/对象里面的字段集合 比如,我有一张表: entity Category.java service CategoryServic ...