【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.
找到不包含重复字母的最长字符串
解题思路:
1.用双指针i,j;其中j往后遍历,thiscount++,当遇到重复的字符时候,j停下
2.用BitMap保存已经遍历过的字符
3.当j停下时候,i往后遍历至重复字符第一次出现的值后面,同时thiscout--
int lengthOfLongestSubstring(char* s) {
int i=,j=;
int flag[]={}; //创建BitMap
int c;
int count=,thiscount=;
while(s[j])
{
c=s[j];
if(!(flag[c>>]&(<<(c&0x1f)))) //当此字符不重复时
{
flag[c>>]|=(<<(c&0x1f)); //标记字符
thiscount++;
if(thiscount>count)
count=thiscount;
}
else
{
while(s[i]!=s[j])
{
c=s[i];
flag[c>>]^=(<<(c&0x1f)); //把重复字符及其第一次出现位置之前的字符从BitMap中删除
thiscount--;
i++;
}
i++;
}
j++;
}
return count;
}
【LeetCode】3. Longest Substring Without Repeating Characters的更多相关文章
- 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)
Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...
- 【LeetCode】003. 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 ...
- leetcode-【中等题】3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode题解 3. Longest Substring Without Repeating Characters
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)
Given a string, find the length of the longest substring without repeating characters. For example, ...
随机推荐
- HDU 1045 Fire Net(DFS)
Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a ci ...
- easy-ui 有依赖关系的下拉列表(省市区县)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- metrics实践 (metrics-spring)
这里主要介绍metrics与spring集成的使用方式. 1 添加maven依赖 <dependency> <groupId>com.ryantenney.metrics&l ...
- 第一百二十四节,JavaScriptCookie与存储
JavaScriptCookie与存储 学习要点: 1.cookie 2.cookie局限性 3.其他存储 随着Web越来越复杂,开发者急切的需要能够本地化存储的脚本功能.这个时候,第一个出现的方案: ...
- Python学习笔记——基础篇2【第三周】——计数器、有序字典、元组、单(双)向队列、深浅拷贝、函数、装饰器
目录 1.Python计数器Counter 2.Python有序字典OrderredDict 3.Python默认字典default 4.python可命名元组namedtuple 5.Python双 ...
- CentOS 7中将Tomcat设置为系统服务
tomcat 需要增加一个pid文件,在tomca/bin 目录下面,增加 setenv.sh 配置,catalina.sh启动的时候会调用,在该文件中添加如下内容 CATALINA_PID=&quo ...
- Python笔记6(异常)-20160924
1. NameError 当视图访问一个未定义的变量则会发生NameError.
- pro asp.net mvc5 7
一个类可以依靠IProductRepository这一接口获取Product对象,而不必知道这些对象从哪里来,也不必知道该接口的实现类如何递交这些对象,这就是存储库模式的本质
- 一.Maven的安装和配置整理
Maven的安装和配置 1.1安装 进入Maven官网的下载页面:http://maven.apache.org/download.cgi选择当前最新版本:" ...
- php专业面试总结
PHP专业面试题汇总 一.PHP基础: 2 二.数据库部分 5 三.面向对象部分 9 四.ThinkPHP部分 12 五.smarty模板引擎 16 六.二次开发系统(DEDE.ecshop): 18 ...