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;
}

LeetCode 第 3 题(Longest Substring Without Repeating Characters)的更多相关文章

  1. leetcode第三题--Longest Substring Without Repeating Characters

    Problem:Given a string, find the length of the longest substring without repeating characters. For e ...

  2. leetcode第三题Longest Substring Without Repeating Characters java

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

  3. LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)

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

  4. 【LeetCode】3 、Longest Substring Without Repeating Characters

    题目等级:Medium 题目描述:   Given a string, find the length of the longest substring without repeating chara ...

  5. Leetcode经典试题:Longest Substring Without Repeating Characters解析

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

  6. LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters

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

  7. LeetCode(3)Longest Substring Without Repeating Characters

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

  8. 刷题之路第三题--Longest Substring Without Repeating Characters

    问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...

  9. LeetCode Hash Table 3. Longest Substring Without Repeating Characters

    HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...

随机推荐

  1. 基于 FPGA 的图像边缘检测

    本文主要内容是实现图像的边缘检测功能 目录 mif文件的制作 调用 ip 核生成rom以及在 questasim 仿真注意问题 灰度处理 均值滤波:重点是3*3 像素阵列的生成 sobel边缘检测 图 ...

  2. 精通CSS高级Web标准解决方案(2-2 可视化格式模型之定位概述)

    视觉格式化模型 块级元素(块框).行内元素(行内框),可以使用display改变生成的框的类型,display:block让行内元素(比如<a>)表现的跟块级元素一样,display:no ...

  3. js时间格式化工具,时间戳格式化,字符串转时间戳

    在开发中经常会用到时间格式化,有时候在网上搜索一大堆但不是自己想要的,自己总结一下,写一个时间格式化工具方便以后直接使用,欢迎大家来吐槽…… 1 2 3 4 5 6 7 8 9 10 11 12 13 ...

  4. CMMI5

    了解CMMI5是什么? 这种解决问题的思想很有用.

  5. 【bzoj1925】[Sdoi2010]地精部落 组合数学+dp

    题目描述 传说很久以前,大地上居住着一种神秘的生物:地精. 地精喜欢住在连绵不绝的山脉中.具体地说,一座长度为 N 的山脉 H可分 为从左到右的 N 段,每段有一个独一无二的高度 Hi,其中Hi是1到 ...

  6. Python Base Two

    //fourth day to study python 24. In python , how to create funcation. we can use def to define funca ...

  7. C# ORM框架

    SQLSUGAR http://www.codeisbug.com/Doc/8/1159 附带mysql工具类,最优使用上面sqlsugar using System; using System.Co ...

  8. ios 的版本记录

    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDictionary); // ap ...

  9. ThinkPHP 的 Vender的简单实用

    ThinkPHP 的 Vender的简单实用 框架版本:3.2 示例一.调用二维码类: Vendor('phpqrcode.phpqrcode'); $QRcode = new \QRcode (); ...

  10. AC日记——爱改名的小融 codevs 2967

    2967 爱改名的小融  时间限制: 1 s  空间限制: 16000 KB  题目等级 : 白银 Silver 题解       题目描述 Description Wikioi上有个人叫小融,他喜欢 ...