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.
分析:贪心法,从头扫描到尾,O(n)搞定 用一个map[256]来记录出现过的字符位置。遇到没有出现过的字符,将map对应位置标记,并且子串长度+1;遇到出现过的字符,将子串自上一次出现该字符位置前面的截断,对这部分截断的字符,标记map为notFound,重新计算子串长度
参考: http://blog.unieagle.net/2012/09/30/leetcode%E9%A2%98%E7%9B%AE%EF%BC%9Alongest-substring-without-repeating-characters/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int map[] ;
for(int i = ; i< ; i++)
map[i] = -;
const int Notfound = -;
int firstPos = , lastPos = ,len = ;
int i, j;
int maxLen = ;
for(i = ; i < s.size(); i++ )
{
int value = map[s[i]] ;
if(value == Notfound)
{
len++;
map[s[i]] = i;
maxLen = maxLen > len ? maxLen : len;
}else{
lastPos = value;
for(j = firstPos ; j < lastPos ; j++)
map[s[j]] = Notfound ;
firstPos = lastPos + ;
len = i - firstPos + ;
map[s[i]] = i;
}
}
return maxLen ;
}
};
LeetCode_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. For exam ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 3. Longest Substring Without Repeating Characters(c++) 15ms
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- 【leetcode】Longest Substring Without Repeating Characters
题目描述: Given a string, find the length of the longest substring without repeating characters. For exa ...
- Longest Substring Without Repeating Characters(C语言实现)
Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...
- leetcode: longest substring without repeating characters
July 16, 2015 Problem statement: Longest Substring Without Repeating Characters Read the blog: http: ...
- [LeetCode_3] Longest Substring Without Repeating Characters
LeetCode: 3. Longest Substring Without Repeating Characters class Solution { public: int lengthOfLon ...
- Longest Substring Without Repeating Characters (c#)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- mysql sql优化<1>
<pre name="code" class="html">explain SELECT t.* FROM ( SELECT t1.sn AS cl ...
- bzoj1149
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1149 水题..... 直接BFS. #include<cstdio> #inclu ...
- centos6.4安装Vmware exsi CLI
1,Vmware官网Exsi CLI下载链接 https://download2.vmware.com/software/sdk/VMware-vSphere-CLI-4.1.0-254719.x86 ...
- 第24讲 UI_布局 之帧布局 表格布局 绝对布局
第24讲 UI_布局 之帧布局 表格布局 绝对布局 3. FrameLayout(帧布局) 帧布局是从屏幕的左上角(0,0)坐标开始布局,多个组件层叠排序,后一个组件总会将前一个组件所覆盖,除非最后一 ...
- JAVA获取oracle中sequences的最后一个值
项目中,用到一个序列作单号,框架用的是ssh,在dao层去拿的时候,运行时报错为dual is not mapped,[select *.nextval nextvalue from dual] 后来 ...
- 转:DesiredCapabilities内容详解--Appium服务关键字
## Appium 服务关键字 <expand_table> |关键字|描述|实例| |----|-----------|-------| |`automationName`|你想使用的自 ...
- curl 学习
<?php // $username =13800138000; // $password =123456; // $sendto =13912345678; // $message = &qu ...
- 给那些因为Firebug而舍不得FireFox的朋友
Google Chrome浏览器调试 作为Web开发人员,我为什么喜欢Google Chrome浏览器 [原文地址:http://www.cnblogs.com/QLeelulu/archive/20 ...
- HDU ACM 1078 FatMouse and Cheese 记忆化+DFS
题意:FatMouse在一个N*N方格上找吃的,每一个点(x,y)有一些吃的,FatMouse从(0,0)的出发去找吃的.每次最多走k步,他走过的位置能够吃掉吃的.保证吃的数量在0-100.规定他仅仅 ...
- MongoDB[mark]总忘记它们是干啥的
MongoDB集群包括一定数量的mongod(分片存储数据).mongos(路由处理).config server(配置节点).clients(客户端).arbiter(仲裁节点:为了选举某个分片存储 ...