[LeetCode 题解]: Longest Substring Without Repeating Characters
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.
题解:
使用贪心策略,Hash记录字符上一次出现的位置,并记录起点。
每次更新的时候,记得将start checkpoint 之间出现的字符location清空,以免重复。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int location[],i,index,j;
for(i=;i<;i++) location[i]=-;
int start=,maxLength=,templ=;
for(i=;i<s.size();i++)
{
index = s[i];
if(location[index]== -)
templ++;
else
{
if(templ>maxLength)
{
maxLength = templ;
// cout<<s.substr(start,templ)<<endl;
}
for(j=start;j<location[index];j++)
location[s[j]]=-;
templ =templ - (location[index]-start);
start = location[index]+;
}
location[index]=i;
}
if(i==s.size() && templ>maxLength) maxLength=templ;
return maxLength;
}
};
转载请注明出处: http://www.cnblogs.com/double-win/
[LeetCode 题解]: Longest Substring Without Repeating Characters的更多相关文章
- LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
problem: Given a string, find the length of the longest substring without repeating characters. For ...
- LeetCode题解——Longest Substring Without Repeating Characters
题目: 给定一个字符串,返回其中不包含重复字符的最长子串长度. 解法: 维持两个指针,第一个指向子串开始,第二个负责遍历,当遍历到的字符出现在子串内时,应计算当前子串长度,并更新最长值:然后第一个指针 ...
- C++版- Leetcode 3. Longest Substring Without Repeating Characters解题报告
Leetcode 3. Longest Substring Without Repeating Characters 提交网址: https://leetcode.com/problems/longe ...
- [Leetcode Week1]Longest Substring Without Repeating Characters
Longest Substring Without Repeating Characters题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/longes ...
- LeetCode 3 Longest Substring Without Repeating Characters(最长不重复子序列)
题目来源:https://leetcode.com/problems/longest-substring-without-repeating-characters/ Given a string, f ...
- LeetCode 3 Longest Substring Without Repeating Characters 解题报告
LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...
- [LeetCode] 3. Longest Substring Without Repeating Characters 解题思路
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode][Python]Longest Substring Without Repeating Characters
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/longest ...
- LeetCode之Longest Substring Without Repeating Characters
[题目描述] Given a string, find the length of the longest substring without repeating characters. Exampl ...
随机推荐
- [z]单次遍历带权随机选取
http://www.gocalf.com/blog/weighted-random-selection.html 没事可以看看,这个博客里面很多文章不错
- Dijkstra算法的另一种证明
按:今天看Tanenbaum的计算机网络时讲到了Dijkstra算法.关于算法的正确性,<算法导论>给出了严格的证明.CLRS的证明基于一个通用的框架,非常清晰.今天只是随意想想是否有其他 ...
- 第六篇 Flask 中内置的 Session
Flask中的Session非常的奇怪,他会将你的SessionID存放在客户端的Cookie中,使用起来也非常的奇怪 1. Flask 中 session 是需要 secret_key 的 from ...
- openLayers 3 之入门
openLayers 3 之入门 openlayer是web GIS客户端开发提供的javascript类库,也是开源框架,可以加载本地数据进行展示地图 1.下载相关引用的js.css文件 2.类似于 ...
- Maven Profiles 定义不同环境的参数变量
应用场景 我们在开发的时候会遇到需要区分正式环境.测试环境.开发环境使用不同的参数,如数据库的用户名及密码等.这时可以用Spring 的PropertyPlaceholderConfigurer 来配 ...
- 基于 DirectX11 的 MMDViewer 02-创建一个窗口
项目的创建和配置: 1.新建一个 Win32 空项目 2.创建源码文件夹.库文件夹和资源文件夹 3.在 VS2013(我使用的 IDE 是 vs2013)配置这些文件夹 这里使用了 $(Solutio ...
- WPF 动态创建 DataTemplate 及数据绑定
WPF 动态创建 DataTemplate 及数据绑定 运行环境:Window7 64bit,.NetFramework4.61,C# 6.0: 编者:乌龙哈里 2017-02-22 参考: star ...
- MySQL用变量的方法添加伪序号列(自增序列)
在进行数据筛选时,可能会用到给每一条数据配上一个唯一的序号,便于进行定位. 方法: 序号的设置: @rownum :=@rownum + 1 AS rownum 获取序号的伪表[必须]: (S ...
- ubuntu 18.04 - server版 开机启动脚本
ubuntu 18.04 不再使用 inited 管理系统,改用 systemd systemd 默认读取 /etc/systemd/system 下的文件,该目录下的文件会链接/lib/system ...
- Delphi数据库的三层架构的问题和解决方法
Delphi数据库的三层架构的问题和解决方法 原创 2014年03月26日 16:26:03 标签: Delphi / 数据库三层架构 / DCOM / DCOMConnection 790 //-- ...