LeetCode 第 3 题(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.
Given “bbbbb”, the answer is “b”, with the length of 1.
Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

求一个字符串的最长无反复子串。也是个比較简单的题目。

涉及到的知识点主要是字符串操作和怎样确定字符是否反复。遍历一个字符串能够用 iterator。推断一个字符是否出现过能够用集合(set)类型。
因此, 我在程序中设立了一个 std::set 型变量 dict。

推断一个字符是否在 dict 中存在,用的是 count() 方法,返回 0 表示不存在这个字符。加入一个字符用的是 insert 方法,删除一个字符是 erase 方法。

另外一个要点是怎样遍历这个字符串。我的程序中设计了头尾两个指针。先用头指针遍历字符串。中间碰到有反复字符了就移动尾指针。直到头尾指针之间没有反复字符为止。这样我的程序仅仅需实时监控头尾指针之间的最大距离即可了。

以下是代码:

int lengthOfLongestSubstring(string s)
{string::const_iterator head = s.cbegin();string::const_iterator tail = s.cbegin();std::set<char> dict;int count, maxCount = 0;while( head != s.cend() ){if( dict.count(*head) == 0){dict.insert(*head);count = dict.size();maxCount = (count > maxCount) ?

count : maxCount; } else { while( *tail != *head ) { dict.erase(*tail); ++tail; } ++tail; } ++head; } return maxCount; }

这个代码尽管计算结果是正确的。可是执行速度略慢。要想提高执行速度,还是要在判别一个字符是否反复的算法上下功夫。由于常见的英文字符就那么几个,所以能够直接用查表法来处理。以下是改进后的代码。

执行速度快了不少。

int lengthOfLongestSubstring(string s)
{string::const_iterator head = s.cbegin();string::const_iterator tail = s.cbegin();char dict[128];memset(dict, 0, 128);int count = 0, maxCount = 0;while( head != s.cend() ){if( dict[*head] == 0){dict[*head] = 1;++ count;maxCount = (count > maxCount) ?

count : maxCount; } else { while( *tail != *head ) { dict[*tail] = 0; -- count; ++tail; } ++tail; } ++head; } return maxCount; }

转载于:https://www.cnblogs.com/gavanwanggw/p/7226959.html

LeetCode 第 3 题(Longest Substring Without Repeating Characters)相关推荐

  1. LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之一:解题思路

    笔者在完成LeetCode第三题(Longest Substring Without Repeating Characters)时,经历了设计.实现.优化三个阶段,于是通过这个三部曲系列,将当初的整个 ...

  2. leetcode(三)—— Longest Substring Without Repeating Characters(最长不重复子串 Python/C++)

    Longest Substring Without Repeating Characters | LeetCode OJ 使用 hash 判重问题首先想到的就是 hash(或者使用 map): 思路: ...

  3. LeetCode.3-最长无重复字符子串(Longest Substring Without Repeating Characters)

    这是悦乐书的第341次更新,第365篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Medium级别的第2题Longest Substring Without Repeating Cha ...

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

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

  5. LeetCode:3. Longest Substring Without Repeating Characters

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

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

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

  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

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

  9. 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters

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

最新文章

  1. 数据库的字段是date java里面能用timestamp吗_数据库中DATETIME,DATE和TIMESTAMP类型 的区别...
  2. Maven Unable to locate the Javac Compiler
  3. AutoHotKey 的使用 —— 使用键盘调节 windows 声音
  4. [crypto]-01-对称加解密AES原理概念详解
  5. 浅谈Python http库 httplib2
  6. 大连公交客运集团认真安排做好2007年防台防汛准备工作
  7. Boost:使用 type <>语法测试功能
  8. 部署LAMP-WordPress站点上线
  9. python折线图怎么添加数值_Python数据可视化:如何创建曲线图
  10. URI和URL及URN的区别
  11. 如何在学习机器学习时学习数学?
  12. latex插入图片之后出现大段空白,并且紧随其后的文字如同被覆盖一般不见了
  13. java实现.net中的枚举
  14. halcon 深度学习英伟达显卡部署
  15. C语言抽签(抽奖)小程序
  16. 冯 诺依曼计算机体系结构要点,冯诺依曼体系结构计算机的要点和工作过程
  17. creo怎么返回上一步_creo拔模怎么用?creo拔模操作技巧图文详解
  18. 安装Ubuntu Linux系统时硬盘分区
  19. 深度学习(DL)-1.3 浅层神经网络 (Shallow neural networks)
  20. linux mbr 转 gpt 数据丢吗,不丢失数据 MBR转GPT分区表教程

热门文章

  1. .net连接DB2的异常SQL0666 - SQL query exceeds specified time limit or storage limit.错误处理
  2. 在线SQL(Insert/Update)语句转JSON工具
  3. Linux之crontab命令
  4. fusionjs 学习二 核心概念
  5. Linux修改本地时间
  6. Git ~ 添加远程仓库 ~Git
  7. EF实体部分更新的问题
  8. CentOS安装中文man手册
  9. a标签的href和onclick的区别
  10. 为什么优秀开发者进入Google后就不参与开源了