一、题目

Longest Substring Without Repeating Characters,具体请自行搜索。

这个题目,我看了一下,经过一番思考,我觉得实现起来不是很复杂。

但要做到bug free有点难度,主要是边界的问题。

二、这个题目,我自己实现,没有参考代码

提交了5次:

第1次: Wrong Answer,主要是“ ”这个空串不对

第2次、第3次:Runtime Error,"au" out of Range

第4次:Wrong Answer,主要是dvdf 不对

第5次:终于对了

三、改进

虽然自己实现了该问题,但边界的考虑不周,总共错误了4次。

目前存在的问题,就是性能不够好。

看看其他人的实现,再写3次。

争取做到bug free,性能足够好。

下面是我的完整代码实现,需要的拿去:

  1. #include<iostream>
  2. using namespace std;
  3. class Solution{
  4. public:
  5. int lengthOfLongestSubstring(string s){
  6. int curr = 0,len = s.length();
  7. int start,end,maxLength=0;
  8. if(len == 1){
  9. maxLength = 1;
  10. return maxLength;
  11. }
  12. while(curr<len){
  13. start = curr;
  14. end = start + 1;
  15. if(end>=len){
  16. break;
  17. }else{
  18. string sub = s.substr(start,end-start);
  19. while(end<len && sub.find(s.at(end)) == -1 ){
  20. if(end<=len){
  21. end++;
  22. sub = s.substr(start,end-start);
  23. }else{
  24. break;
  25. }
  26. }
  27. if(maxLength<(end-start)){
  28. maxLength = end - start;
  29. }
  30. }
  31. curr++;
  32. }
  33. return maxLength;
  34. }
  35. };
  36. int main(){
  37. Solution s;
  38. cout<<s.lengthOfLongestSubstring("abcabcbb")<<endl;
  39. cout<<s.lengthOfLongestSubstring("bbbbb")<<endl;
  40. cout<<s.lengthOfLongestSubstring("pwwkew")<<endl;
  41. cout<<s.lengthOfLongestSubstring(" ")<<endl;
  42. cout<<s.lengthOfLongestSubstring(" ")<<endl;
  43. cout<<s.lengthOfLongestSubstring("")<<endl;
  44. cout<<s.lengthOfLongestSubstring("au")<<endl;
  45. cout<<s.lengthOfLongestSubstring("dvdf")<<endl;
  46. return 0;
  47. }

网上找一个性能稍微好点也容易理解的:

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. class Solution{
  5. public: //pwwkew
  6. int lengthOfLongestSubstring(string s){
  7. int res = 0;
  8. vector<int> m(128,0);
  9. for(int i=0,j=0;j<s.size();j++){
  10. if(m[s[j]]++ == 0){
  11. res = max(res,j-i+1);
  12. }else{
  13. while(i<j && m[s[j]]>1){
  14. m[s[i]]--;
  15. i++;
  16. }
  17. }
  18. }
  19. return res;
  20. }
  21. };
  22. int main(){
  23. Solution s;
  24. cout<<s.lengthOfLongestSubstring("abcabcbb")<<endl;
  25. cout<<s.lengthOfLongestSubstring("bbbbb")<<endl;
  26. cout<<s.lengthOfLongestSubstring("pwwkew")<<endl;
  27. cout<<s.lengthOfLongestSubstring(" ")<<endl;
  28. cout<<s.lengthOfLongestSubstring(" ")<<endl;
  29. cout<<s.lengthOfLongestSubstring("")<<endl;
  30. cout<<s.lengthOfLongestSubstring("au")<<endl;
  31. cout<<s.lengthOfLongestSubstring("dvdf")<<endl;
  32. return 0;
  33. }

刷题3. Longest Substring Without Repeating Characters的更多相关文章

  1. 【LeetCode刷题系列 - 003题】Longest Substring Without Repeating Characters

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

  2. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

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

  3. Leetcode第三题《Longest Substring Without Repeating Characters》

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

  4. [刷题] 3 Longest Substring Without Repeating Character

    要求 在一个字符串中寻找没有重复字母的最长子串 举例 输入:abcabcbb 输出:abc 细节 字符集?字母?数字+字母?ASCII? 大小写是否敏感? 思路 滑动窗口 如果当前窗口没有重复字母,j ...

  5. LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

    LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters 题记 刷LeetCod ...

  6. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  7. 周刷题第二期总结(Longest Substring Without Repeating Characters and Median of Two Sorted Arrays)

    这周前面刷题倒是蛮开心,后面出了很多别的事情和问题就去忙其他的,结果又只完成了最低目标. Lonest Substring Without Repeating Characters: Given a ...

  8. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  9. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

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

随机推荐

  1. CSS之 元素显示隐藏,用户界面样式,文本溢出隐藏,精灵技术,三角形

    元素的显示与隐藏 display 显示 display 设置或检索对象是否及如何显示 display: none; 隐藏对象 display: block; 除了转换为块级元素, 同时还有显示元素的意 ...

  2. Python3标准库:enum枚举

    1. enum枚举 枚举是一组符号名称(枚举成员)的集合,枚举成员应该是唯一的.不可变的.在枚举中,可以对成员进行恒等比较,并且枚举本身是可迭代的. 1.1 创建枚举 可以使用class语法派生Enu ...

  3. element-ui的upload组件的clearFiles方法

    <template> <div> <el-button @click="clearFiles">重新上传</el-button> & ...

  4. pytest之assert断言

    assert pytest允许您使用标准Python断言来验证Python测试中的期望和值.例如,你可以写下 # content of test_assert1.py def f(): return ...

  5. 部件MSCOMCTL.OCX或其附件之一不能正确注册:一个文件丢失或无效

    部件MSCOMCTL.OCX或其附件之一不能正确注册:一个文件丢失或无效: https://blog.csdn.net/yilese/article/details/71479908

  6. Xlrd模块读取Excel文件数据

    Xlrd模块使用 excel文件样例:

  7. 网易云信融合CDN方案及实践

    日前,网易云信视频云架构师席智勇在第七届GFIC全球家庭互联网大会进行了题为<网易云信融合CDN方案及实践>的分享,以下是演讲内容回顾. 图为 网易云信视频云架构师席智勇 CDN所面临的问 ...

  8. loadrunner11破解失败,已解决“ license security violation.Operation is not allowed ”问题

    参考链接https://blog.csdn.net/yongrong/article/details/7891738,亲测可以解决问题 在64位win7系统中安装LR11时,采用普通的方法无法授权.最 ...

  9. 杭电oj 2072————统计单词数(java)

    problem:统计单词数 思路:利用HashMap的特性——不能反复存储同一个键得数据,所以可以保证map里边儿的元素都是不重复的,存储完毕之后直接输出size就好了 注意事项: 1.利用strin ...

  10. java 中使用MD5加密 , 入库时对密码进行加密

    import lombok.extern.slf4j.Slf4j; import java.security.MessageDigest; @Slf4j public class MD5Util { ...