【题目】

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

【分析】

从左到右扫描,当遇到重复字母时,以上一个重复字母的index+1,作为新的搜索起始位置,直到最后一个字母。

【代码】

/*********************************
*   日期:2015-01-20
*   作者:SJF0115
*   题目: 3.Longest Substring Without Repeating Characters
*   网址:https://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
*   结果:AC
*   来源:LeetCode
*   时间复杂度:O(n)
*   空间复杂度:O(1)
*   博客:
**********************************/
#include <iostream>
#include <cstring>
using namespace std;class Solution {
public:int lengthOfLongestSubstring(string s) {int len = s.length();if(len <= 1){return len;}//ifbool visited[256];// 初始化memset(visited,false,sizeof(visited));// 计算int max = 0,cur = 0,start = 0;for(int i = 0;i < len;){// 没有元素和该元素重复if(!visited[s[i]]){visited[s[i]] = true;++cur;++i;}//ifelse{// 更新最大长度if(cur >= max){max = cur;}//ifcur = 0;memset(visited,false,sizeof(visited));int index = 0;// 寻找之前重复元素的下一个元素的下标for(int j = start;j < i;++j){if(s[i] == s[j]){index = j+1;break;}//if}//forstart = index;i = index;}}//forif(cur > max){max = cur;}//ifreturn max;}
};int main(){Solution solution;string str("abbacdafg");int result = solution.lengthOfLongestSubstring(str);// 输出cout<<result<<endl;return 0;
}

【代码二】

class Solution {
public:int lengthOfLongestSubstring(string s) {int len = s.length();if(len <= 1){return len;}//if// 记录上次出现的位置int last[256];// 初始化memset(last,-1,sizeof(last));// 计算int max = 0,cur = 0,start = 0;for(int i = 0;i < len;++i){// 有元素和该元素重复if(last[s[i]] >= 0){if(cur > max){max =cur;}//if// 当遇到重复字母时,以上一个重复字母的index+1,// 作为新的搜索起始位置i = last[s[i]] + 1;cur = 0;memset(last,-1,sizeof(last));}//if++cur;last[s[i]] = i;}//forif(cur > max){max = cur;}//ifreturn max;}
};

【分析三】

对上一个思路继续进行优化。

在上一个思路中当遇到重复字母时,以上一个重复字母的index+1,作为新的搜索起始位置。上一个重复字母的下一个字母到当前字母可以不用重复遍历。

在本思路中搜索起点不是上一个重复字母的index+1而是当前字母。

通过记录上一个Lonest Substring Without Repeating Characters的起点,来计算出在当前字母时的长度。

【代码三】

class Solution {
public:int lengthOfLongestSubstring(string s) {int len = s.length();if(len <= 1){return len;}//if// 记录上次出现的位置int last[256];// 初始化memset(last,-1,sizeof(last));// 计算int max = 0,cur = 0,start = 0;for(int i = 0;i < len;++i){int pos = (int)s[i];// 有元素和该元素重复if(last[pos] >= start){// not last[pos] >= 0// 更新长度cur = cur - (last[pos] - start);// 更新起点start = last[pos] + 1;}//ifelse{++cur;}// 更新字符位置last[pos] = i;if(cur > max){max =cur;}//if}//forreturn max;}
};

[LeetCode]3.Longest Substring Without Repeating Characters相关推荐

  1. 【贪心】LeetCode 3. Longest Substring Without Repeating Characters

    LeetCode 3. Longest Substring Without Repeating Characters Solution1:我的答案 该方法中哈希表记录的是字符出现的次数.标准的贪心算法 ...

  2. LeetCode:3. Longest Substring Without Repeating Characters

    https://leetcode.com/problems/longest-substring-without-repeating-characters/description/ 内容描述: Give ...

  3. [LeetCode]--3. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  4. leetcode 3. Longest Substring Without Repeating Characters 最长非重复子串的长度 滑动窗口法

    题目链接 根据我们之前介绍的滑动窗口法的解法: 滑动窗口法详解 leetcode 438. Find All Anagrams in a String 滑动窗口法 这题,我们不难解决,使用之前的模板. ...

  5. python刷leetcode_零基础python刷leetcode -- 3. Longest Substring Without Repeating Characters

    算法很重要,但是每天也需要学学python,于是就想用python刷leetcode 的算法题,和我一起开始零基础python刷leetcode之旅吧.如有不对的地方,希望指正,万分感谢~~ 题目 最 ...

  6. leetcode 3. Longest Substring Without Repeating Characters | 3. 无重复字符的最长子串(双指针+滑窗)

    题目 https://leetcode.com/problems/longest-substring-without-repeating-characters/ 题解 双指针+滑窗,维护一个 set, ...

  7. LeetCode 3 Longest Substring Without Repeating Characters 区间,想法 难度:1

    https://leetcode.com/problems/longest-substring-without-repeating-characters/ 思路:从某点结束所能取到的最早开头是到目前出 ...

  8. leetcode 3. Longest Substring Without Repeating Characters

    记得这要+1,j = std::max(j,container[s[i]]+1); class Solution { public:int lengthOfLongestSubstring(strin ...

  9. [LeetCode] 3. Longest Substring Without Repeating Characters 题解

    问题描述 输入一个字符串,找到其中最长的不重复子串 例1: 输入:"abcabcbb" 输出:3 解释:最长非重复子串为"abc" 复制代码 例2: 输入:&q ...

最新文章

  1. 华为持续引领,开辟5G Massive MIMO绿色新赛道
  2. 有关军事人机混合智能的再再思考
  3. 人工智能python框架_Python 与 AI 智能框架 - 随笔分类 - Hopesun - 博客园
  4. 邓西百度网盘多帐号文件一键搜索工具
  5. 今日arXiv精选 | Survey/ICCV/ACM MM/ICML/CIKM/SIGIR/RecSys/IROS
  6. wxWidgets:wxMouseEvent类用法
  7. ROS官网新手级教程总结
  8. 字符转换工具,仿牛族字符转换
  9. PTA编程总结3:抓老鼠
  10. Python进阶(四)Python中的异常
  11. 苹果mac图像后期处理软件:Lightroom Classic
  12. SaaS服务商盘点之ERP篇
  13. C语言基础题练习10道
  14. webstorm比较文件差异
  15. webp格式怎么改png?如何将webp转换格式?
  16. 使用 IDEA 的阿里插件扫描出的问题的级别
  17. 使用SDK Manager给TX2刷机且安装OpenCV3.4.0、CUDNN7.6.5、Pytorch、Miniforge(含百度云安装包)
  18. java报刊管理系统_Java课程设计——报刊管理系统
  19. L1D1:嵌入式Linux C语言开发工具及基础命令
  20. 优秀的 GitHub 项目合集

热门文章

  1. Android混合推送,MUI框架-推送配置核心代码-个推推送
  2. 360显示html邮件内容,“邮件怎样发送视频内容”的解决方案
  3. Moore状态机和Mealy状态机的区别
  4. 初识Tcl(六):Tcl 数组
  5. 【 C 】字符串查找基础笔记
  6. ET001 不可不掌握的 Logstash 使用技巧
  7. P2P征信 大数据堪当重任?
  8. APOC 15 Years Celebration
  9. 离开页面提示是否保存页面修改内容的简单实现
  10. C#温故而知新学习系列之.NET框架高级特性—概述.NET框架中的反射(一)