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.

使用双指针,前一个指针向前走,有不相同的字符的时候标记即可,发现相同时停止,这是后面的字符向后走,直到发现这个字符,然后前指针继续向前走,一直重复直到整个过程的结束,代码如下:

 class Solution {
public:
int lengthOfLongestSubstring(string s) {
if(!s.size()) return ;
int i = , j = ;
int maxSubLen = ;
bool canUse[]; //char类型只有256个
memset(canUse, true, sizeof(canUse));
canUse[s[]] = false;
while(j < s.size()){
if(!canUse[s[j]]){
maxSubLen = max(maxSubLen, j - i);
while(i < j){
if(s[i] != s[j])
canUse[s[i]] = true, ++i;
else{
i++;
break;
}
}
}else{
maxSubLen = max(maxSubLen, j - i + );
canUse[s[j]] = false;
}
++j;
}
return maxSubLen;
}
};

写的有点乱啊,见谅见谅。。

LeetCode OJ:Longest Substring Without Repeating Characters(最长无重复字符子串)的更多相关文章

  1. 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium

    Examples: Description: Given a string, find the length of the longest substring without repeating ch ...

  2. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  3. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现

    最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...

  4. [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

  5. leetcode 3 Longest Substring Without Repeating Characters最长无重复子串

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

  6. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

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

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

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

  8. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  9. [LeetCode] Longest Substring Without Repeating Characters最长无重复子串

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

  10. 3 Longest Substring Without Repeating Characters(最长不重复连续子串Medium)

    题目意思:求字符串中,最长不重复连续子串 思路:使用hashmap,发现unordered_map会比map快,设置一个起始位置,计算长度时,去减起始位置的值 eg:a,b,c,d,e,c,b,a,e ...

随机推荐

  1. JVM 内存知识总结

    本文主要参考内容: http://hllvm.group.iteye.com/group/wiki/3053-JVM http://my.oschina.net/xishuixixia/blog/13 ...

  2. Qt 编码问题QTextCodec

    在学习计算机语言的时候, 关于字体编码问题, 一直是大家开始学习新语言比较头痛的问题, 在这边总结一下关于Qt图形框架开发的编码问题. 一般在Window开发环境里,是GBK编码,在Linux开发环境 ...

  3. Sublime text3:安装插件SublimeREPL解决不支持input

    Sublime text3:安装插件SublimeREPL解决不支持input 安装SublimeREPL 1,调用ctrl+shift+p 输入install回车: 2,输出:sublimerepl ...

  4. [pixhawk笔记]6-uORB流程及关键函数解析

    本文中将结合代码.文档及注释,给出uORB执行流程及关键函数的解析,由于uORB的机制实现较为复杂,所以本文主要学习如何使用uORB的接口来实现通信.回到上一篇笔记中的代码: #include < ...

  5. Thinkphp5.0实战开发二------自动生成目录结构

    序言 ThinkPHP5.0 具备自动创建功能,可以用来自动生成需要的模块及目录结构和文件等,自动生成主要调用\think\Build 类库.ThinkPHP5.0中模块文件夹在application ...

  6. Jquery2 基础核心

    学习要点: 1.代码风格 2.加载模式 3.对象互换 4.多个库之间的冲突 本节简单的介绍一下jQuery 一些核心的问题. 一.代码风格 在jQuery程序中,不管是页面元素的选择.内置的功能函数, ...

  7. [BZOJ4756]Promotion Counting

    Description The cows have once again tried to form a startup company, failing to remember from past ...

  8. [BZOJ1996] chorus合唱队

    Description Input Output Sample Input 4 1701 1702 1703 1704 Sample Output 8 HINT 区间$dp$,首先每个点被放入队伍时队 ...

  9. LeetCode——Find Duplicate Subtrees

    Question Given a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, yo ...

  10. 解决github访问慢的问题

    在windows hosts文件末尾增加以下内容 # GitHub Start 192.30.253.112 github.com 192.30.253.119 gist.github.com 151 ...