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的更多相关文章

  1. 【LeetCode】3. Longest Substring Without Repeating Characters 无重复字符的最长子串

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 公众号:负雪明烛 本文关键词:无重复字符,最长子串,题解,leetcode, 力扣,py ...

  2. 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  3. 【LeetCode】3. Longest Substring Without Repeating Characters (2 solutions)

    Longest Substring Without Repeating Characters Given a string, find the length of the longest substr ...

  4. 【LeetCode】003. Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  5. 【一天一道LeetCode】 #3 Longest Substring Without Repeating Characters

    一天一道LeetCode (一)题目 Given a string, find the length of the longest substring without repeating charac ...

  6. leetcode-【中等题】3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  7. leetcode题解 3. Longest Substring Without Repeating Characters

    题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...

  8. 《LeetBook》leetcode题解(3):Longest Substring Without Repeating Characters[M]——哈希判断重复

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  9. LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)

    Given a string, find the length of the longest substring without repeating characters. For example, ...

随机推荐

  1. hdu 1299 Diophantus of Alexandria(数学题)

    题目链接:hdu 1299 Diophantus of Alexandria 题意: 给你一个n,让你找1/x+1/y=1/n的方案数. 题解: 对于这种数学题,一般都变变形,找找规律,通过打表我们可 ...

  2. ssh爆破脚本

    前些天,基友发我一个ssh爆破工具,看起来很吊的样子.然后我就无聊自己写了个py脚本的. 单线程:慢成狗----- #coding:utf-8 #author:jwong import threadi ...

  3. javaee 规范技术

    J2EE的13种核心技术 一.JDBC(Java Database Connectivity) JDBC API为访问不同的数据库提供了一种统一的途径,象ODBC一样,JDBC对开发者屏蔽了一些细节问 ...

  4. 【Map】Double Queue

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13258   Accepted: 5974 Des ...

  5. canvas画扇形图(本文来自于http://jo2.org/html5-canvas-sector/)

    1.定义画扇形的构造函数: //扇形CanvasRenderingContext2D.prototype.sector = function (x, y, radius, sDeg, eDeg) {/ ...

  6. c# 操作xml之xmlReader

    xmlReader的名称空间using System.Xml; xmlReader是通过流的方式来读取xml文件的内容 <?xml version="1.0" encodin ...

  7. 【转】delphi 保存到txt文件

    procedure TForm1.btn1Click(Sender: TObject); var astr: string; sList: TStrings; path: string; begin ...

  8. C# 语言规范_版本5.0 (第10章 类)

    1. 类 类是一种数据结构,它可以包含数据成员(常量和字段).函数成员(方法.属性.事件.索引器.运算符.实例构造函数.静态构造函数和析构函数)以及嵌套类型.类类型支持继承,继承是一种机制,它使派生类 ...

  9. GoogleCodeJam

    2016年没有参赛,在师兄的介绍下,试了一下简单的一题,需要注意的是读写数据的形式还有具体代码. 2016资格赛 A题 Problem Bleatrix Trotter the sheep has d ...

  10. JQ怎么跳出 each循环

    return false;——跳出所有循环:相当于 javascript 中的 break 效果. return true;——跳出当前循环,进入下一个循环:相当于 javascript 中的 con ...