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的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- csrf 攻击及防御
1.什么是CSRF攻击: CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/session riding,缩写为:C ...
- 安装adb工包
下载android sdk (很大) 从D:\AndroidSdk\platform-tools目录可以看到: 将adb工具包: adb.exe,AdbWinapi.dll,AdbWinUSBapi. ...
- Spring 4.3.11.RELEASE文档阅读(一):overview
一.宏观概述中的体会和发现 Spring是组件式的框架,它允许我们只使用其一小部分.Spring所做的工作,就是不断的简化我们的操作.比如它的IOC容器,当我们自己应用设计模式,比如说:建造者.工厂. ...
- 将模型储存到本地-FastCoder
// // ViewController.m // 模型转data储存 // // Created by 谭启宏 on 16/3/4. // Copyright © 2016年 tqh. All ri ...
- mysql explain字段意思解释
mysql explain字段意思解释 explain包含id.select_type.table.type.possible_keys.key.key_len.ref.rows.extra字段 id ...
- 关于ubuntu的对拍
感谢夏天dl的blog,写的十分清楚,但是本人对于ubuntu十分不熟悉 所以不怎么会使用. 对拍的可执行文件是sh,就是bash语言 #!bin/bash while true; do ./date ...
- [暑假集训--数位dp]hdu5898 odd-even number
For a number,if the length of continuous odd digits is even and the length of continuous even digits ...
- 洛谷 P 1514 引水入城==Codevs 1066
题目描述 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形,如上图所示,其中每个格子都代表一座城市,每座城市都有一个海拔高度. ...
- 【BZOJ1001】狼抓兔子(平面图最小割转最短路)
题意:有一张平面图,求它的最小割.N,M.表示网格的大小,N,M均小于等于1000. 左上角点为(1,1),右下角点为(N,M).有以下三种类型的道路 1:(x,y)<==>(x+1,y ...
- JS函数(自调函数)与闭包【高级函数】
JavaScript:BOM(浏览器对象)+DOM(文档对象)+ECMAScript javascript面向对象: * 概述: * 发展: * 互联网发展对浏览器页面性能或效果要求越来越高,HTML ...