【Longest Substring Without Repeating Characters】cpp
题目:
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
代码:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, int> visited;
for ( char c='a'; c<='z'; ++c ) visited[c]=-;
int begin = ;
int longest_substring = ;
for ( int i = ; i < s.size(); ++i )
{
char curr = s[i];
if ( visited[curr]>=begin )
{
longest_substring = std::max(longest_substring, i-begin);
begin = visited[curr]+;
}
visited[curr]=i;
}
return std::max((int)s.size()-begin, longest_substring);
}
};
tips:
Greedy解法,主要维护以下两个内容。
维护一个哈希表:<字母,上一次字母出现的位置>
维护一个begin:表示不重复字符串的起始位置
如果当前字母在之前出现过,且出现的位置大于等于begin,则证明遇到了重复的字符;更新begin的位置为该字母上一次出现的位置+1。
在整个过程中,每次遇到不重复的字符时,就往后走。
这里要注意边界条件,即最长的字符串包含最后s的最后一个字符。
则在推出循环后,要再更新一次longest_substring。
===============================================
第二次过这道题,还是用hashmap的思路,第一次超时了,原因是想删除在local长度之前的那些hashmap表中的字符。
第二次修改了一下,hashmap表中的字符即使有重复的也不要紧,只要不在local长度范围内,都可以接受继续往后走,然后更新字符最新的位置就可以了。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
map<char, int> charPosition;
int global = ;
int local = ;
for ( int i=; i<s.size(); ++i )
{
if ( charPosition.find(s[i])==charPosition.end() || charPosition[s[i]]<i-local )
{
charPosition[s[i]] = i;
local++;
}
else
{
global = max(global, local);
local = i - charPosition[s[i]];
charPosition[s[i]] = i;
}
}
return max(global, local);
}
};
【Longest Substring Without Repeating Characters】cpp的更多相关文章
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- 【LeetCode】Longest Substring Without Repeating Characters 解题报告
[题意] Given a string, find the length of the longest substring without repeating characters. For exam ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- 【JAVA、C++】LeetCode 003 Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【leetcode】Longest Substring Without Repeating Characters (middle)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode从零单排】No 3 Longest Substring Without Repeating Characters
题目 Given a string, find the length of the longest substring without repeating characters. For exampl ...
- 【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
一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...
随机推荐
- Eucalyptus-NC管理
1.前言 Elastic Utility Computing Architecture for Linking Your Programs To Useful Systems (Eucalyptus) ...
- 用sql语句按周、按月、按季、按年统计
--按mySql语法统计按周,月,季,年.income为合计的价格字段,createDate为交易时间. select sum(income)as revenue,week(createDate) a ...
- 页面中插入视频兼容ie8以上的浏览器
有时候页面中需要插入视频,如果不考虑ie8的话:就是直接用h5标签<video></video>就可以了: 但是有的时候需求是需要考虑ie8,这样我们就可以用插件,这种vide ...
- linux 命令——18 locate (转)
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 di ...
- noip模拟赛#39
昨晚打开的题想了一会发现都不会后决定慢慢想.然后早上开校会的时候莫名其妙的都想出来了... T1:m=100,ai=50000,i<=5.1到m的数每个数只能用一次,判断是否能够有这些数的某些数 ...
- 2018.6.13 Java语言基础复习总结
Java语言基础与面向对象编程实践 第一章 初识Java 1.1机器语言 机器语言是指一台计算机全部的指令集合.机器语言室友0和1组成的二进制数,是一串串由0和1组成的指令序列,可将这些指令序列交给计 ...
- python_19_编码解码
msg="我爱北京天安门" #字符串转成Byte类型 print(msg.encode())#encode 编码 print(msg.encode(encoding="u ...
- Linux 开启关闭防火墙
开放防火墙端口添加需要监听的端口 /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT/sbin/iptables -I INPUT -p tcp ...
- kernel
http://sebastianraschka.com/Articles/2014_kernel_pca.html
- HTML复选框checkbox默认样式修改
此方法可以将复选框的默认样式替换成任意样式.如图: 未选择: 选择时: 思路:将复选框隐藏,利用lebal元素的焦点传递特性,用lebal的样式替代复选框. 代码如下: <!DOCTYPE ht ...