【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 ...
随机推荐
- Ubuntu 16.04 远程登入root 用户
安装 open ssh: sudo apt-get install openssh-server 修改 root 密码 sudo passwd root 以其他账户登录,通过 sudo nan ...
- Python __builtin__模块
你有没有好奇过当我们打开Python后就可以直接使用str(),list(),eval(),print(),max()这样的函数,而不用导入任何模块? 其实原因很简单,就是当我们打开Python解释器 ...
- sublime相关小技巧
1.快速建立一个新文件:Ctrl+n 2.修改多个相同符号:Ctrl+D 3.建立语言后缀的文件保存,例如我想创建PHP的语言脚本,先按Ctrl+Shift+p,打开Command Palette,输 ...
- java Vamei快速教程15 IO基础
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 计算机最重要的功能是处理数据.一个有用的计算机语言需要拥有良好的IO功能,以便让未 ...
- 又一次摔MFC坑里了
因为公司的个项目最近开始写MFC了,又遇到一个坑爹的问题,使用的View视图模式在VS2010中创建的工程,默认就带入了许多的Style,例如Office 2007的许多漂亮样式确实很方便,但是同样也 ...
- [VC]vc中socket编程步骤
sockets(套接字)编程有三种,流式套接字(SOCK_STREAM),数据报套接字(SOCK_DGRAM),原始套接字(SOCK_RAW): 基于TCP的socket编程是采用的流式套接字.在这个 ...
- 由一道CTF pwn题深入理解libc2.26中的tcache机制
本文首发安全客:https://www.anquanke.com/post/id/104760 在刚结束的HITB-XCTF有一道pwn题gundam使用了2.26版本的libc.因为2.26版本中加 ...
- Luogu [P1334] 瑞瑞的木板(手写堆)
其实这个题完全不需要用手写堆,只需要一遍遍sort就行了…… 但是! 为了练习手写堆,还是用手写堆做了. 在做本题之前,如果你没有什么思路的话,建议先做Luogu的合并果子. 好,假设你已经做过了合并 ...
- java基础面试题:抽象类中是否可以有静态的main方法?
- wepy框架构建小程序(1)
wepy框架构建小程序(1) 基本操作: # 安装脚手架工具 npm install wepy-cli -g # 创建一个新的项目 npm init standard myproject # 进入新项 ...