LeetCode 第 3 题(Longest Substring Without Repeating Characters)
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)的更多相关文章
- leetcode第三题--Longest Substring Without Repeating Characters
Problem:Given a string, find the length of the longest substring without repeating characters. For e ...
- leetcode第三题Longest Substring Without Repeating Characters java
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- LeetCode第三题—— Longest Substring Without Repeating Characters(最长无重复子字符串)
题目描述 Given a string, find the length of the longest substring without repeating characters. Example ...
- 【LeetCode】3 、Longest Substring Without Repeating Characters
题目等级:Medium 题目描述: Given a string, find the length of the longest substring without repeating chara ...
- Leetcode经典试题:Longest Substring Without Repeating Characters解析
题目如下: Given a string, find the length of the longest substring without repeating characters. Example ...
- LeetCode解题笔记 - 3. Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- LeetCode(3)Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. For examp ...
- 刷题之路第三题--Longest Substring Without Repeating Characters
问题简介:求给定字符串中最长的字符不重复的字符串的长度 问题详解: 给定一个字符串,寻找给定字符串中包含的最长的字符不重复的字符串的长度 注:答案必须是子字符串,不是子序列 是连续的字符不重复的字符串 ...
- LeetCode Hash Table 3. Longest Substring Without Repeating Characters
HashMap的应用可以提高查找的速度,键key,值value的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- C++ POST方式访问网页
bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &s ...
- Educational Codeforces Round 20 A. Maximal Binary Matrix
A. Maximal Binary Matrix time limit per test 1 second memory limit per test 256 megabytes input stan ...
- URAL 1106 Two Teams二分图
S - Two Teams Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- Unity3D - UGUI的初级应用
添加字体: 把下载好的字体拖拽到Project面板中 - 点击Text组件中Text属性后面的圆点 - 选择刚刚拖拽的字体即可. 创建ToggleGroup(开关组): 1.在Canvas下创建两个T ...
- 设计模式(八)组合模式 Composite
组合模式: 允许你将对象组合成树形结构来表现“整体/部分”层次结构.组合能让客户以一致的方式处理个别对象以及对象组合. 组合模式适用于创建复杂的对象,这个对象包含某些个别的对象以及这些对象的组合. 从 ...
- 【Luogu】P3761城市(dfs)
题目链接 emmm我思维好水…… 想了一会lct发现好像不对,然后开始转DP稍微有一点思路,然后看了题解…… 首先可以枚举边,然后原树被你拆成了两个子树. 设D1D2是两个子树的直径,W1W2是子树内 ...
- HDU——1257最少拦截系统(贪心)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hosts文件位置
windows C:\WINDOWS\system32\drivers\etc mac /etc/hosts 修改hosts文件会遇到无法保存的问题,解法方法参考下文 http://mtoou.inf ...
- 北京集训TEST13——PA(第k小数)
题目: Description [问题描述] 从n个数中选若干(至少1)个数求和,求所有方案中第k小的和(和相同但取法不同的视为不同方案).[输入格式] 第一行输入2个正整数n,k. 第二 ...
- 金鹰教程网 FLASH8.0(AS)视频教程(下载地址)自认为最好的一个Flash教程
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] 可以用迅雷新建批量任务下载,很方便的。 金鹰教程网 FLASH8.0教学视频 到目前(2008年7月29日21:2 ...