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. maven原理

    http://www.cnblogs.com/onlys/archive/2011/01/04/1925466.html 基本原理Maven的基本原理很简单,采用远程仓库和本地仓库以及一个类似buil ...

  2. 【CodeVS】1293

    输入输出样例 思路:看到题目我萌第一眼想到的肯定是求联通快对吧,但是这个联通快有点奇特,因为 这样他也算是一个联通快.解决此题其实有三种解法:1)宽搜(这个符合基本法):2)并查集:3)灌水法 但是蒟 ...

  3. 某种数列问题 (一场欢乐赛的T2)

    个人觉得挺难的一道DP题 不会 没有思路 于是去找的正解 于是.. #include <iostream> #include <cstring> #define Max 100 ...

  4. Socket编程中的长连接、短链接以及心跳包机制详解

    参考:http://blog.csdn.net/zdwzzu2006/article/details/7723738 一.定义 1.TCP连接 当网络通信时采用TCP协议时,在真正的读写操作之前,se ...

  5. Mac下安装node.js和webpack

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "PingFang SC"; color: #393939 } p.p2 ...

  6. sql语句实现隐藏手机号码中间四位的方法

    1、select REPLACE(mobile,SUBSTR(mobile,4,4), '****') as mobile from tableName 2、select INSERT(mobile, ...

  7. 接口测试:如何定位BUG的产生原因

    转自公众号<QA之道> 我们从在日常功能测试过程中对UI的每一次操作说白了就是对一个或者多个接口的一次调用,接口的返回的内容(移动端一般为json)经过前端代码的处理最终展示在页面上.ht ...

  8. 那些年,我们被耍过的bug——haslayout

    你被IE的bug耍过几次了? IE,这个令所有网站设计人员讨厌,但又不得不为它工作的浏览器.不论是6.7还是8,它们都有一个共同的渲染标准haslayout,所以haslayout 是一个非常有必要彻 ...

  9. Swift3GCD

    GCD的使用在Swift3中的方法 //串行队列 let q:DispatchQueue = DispatchQueue(label: "xiaosi") //并发队列 qos : ...

  10. HDU 5795 A Simple Nim(SG打表找规律)

    SG打表找规律 HDU 5795 题目连接 #include<iostream> #include<cstdio> #include<cmath> #include ...