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的使用拜托了传统数组的遍历查找方式,对于判断一个字符或者字符串是否已经存在的问题可以非常好的解决.而本题需要解决的问题就是判断新遍历到的字 ...
随机推荐
- 【LeetCode】Remove Duplicates from Sorted Array(删除排序数组中的重复项)
这道题是LeetCode里的第26道题. 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数 ...
- 基于UDP的交互的实例
1.实现简单的客户端.服务端聊天交互 问题是:客户端不能单独一直发消息回复.. 服务端: import socket server=socket.socket(socket.AF_INET,socke ...
- 用requests自动访问网页,下载网页内容
import requests # 请求下载excel def downloading(text_path): # 访问要下载的链接文件 with open(text_path) as f: for ...
- NuGet之控制台管理程序包
NuGet作为VS的扩展程序,已经做好了UI,我们可以通过Manage NuGet Packages 的对话框.这里我们主要说说如何通过控制台进行包管理.使用命令行的方式,其实也是有其好处,对 ...
- php hash防止表单
<?php /** * Created by PhpStorm. * User: brady * Desc: * Date: 2017/7/12 * Time: 15:01 */class te ...
- 【FFT】学习笔记
首先,多项式有两种表示方式,系数表示和点值表示 对于两个多项式相乘而言,用系数表示进行计算是O(n^2)的 而用点值表示进行计算是O(n)的 那么我们自然就会去想如果把系数表示的多项式转化为点值表示的 ...
- gridview中的相关事件操作
原文发布时间为:2008-07-27 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- 优先队列priority_queue的使用
优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序. 每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来存储.例如:我们常用的操作就 ...
- VUE之命令行报错:Component template should contain exactly one root element. If you are using v-if on multiple elements, use v-else-if to chain them instead 解决办法
Failed to compile. ./node_modules/vue-loader/lib/template-compiler?{"id":"data-v-5992 ...
- linux环境设置export
一. shell显示与设置环境变量 1.export //echo $PATH 2.export | grep ROS 3.export ROS_IP=192.168.0.5(添加环境变量ROS_IP ...