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. 屏蔽信号的多路选择I/O

    前边提到了多路I/O的方法,这一章屏蔽信号的多路选择与之前的多路I/O一致,只是增加了屏蔽信号的作用.多路选择I/O中我们使用的是select函数,屏蔽信号的多路选择I/O使用的是pselect函数, ...

  2. Shape-in​​side css新属性 几何图形的实现

    https://www.w3cplus.com/css3/css-shapes-101.html

  3. OpenCV KNN 之 使用方法

    http://blog.csdn.net/WL2002200/article/details/43149229 OpenCV 中KNN构造函数如下. C++: CvKNearest::CvKNeare ...

  4. 20145310 《Java程序设计》第4周学习总结

    20145310 <Java程序设计>第4周学习总结 教材学习内容总结 本周主要进行第五章和第六章的学习. 第六章 继承与多态 子类(Inherit)继承父类,避免重复的行为定义,不过并非 ...

  5. 20145328 《Java程序设计》第9周学习总结

    20145328 <Java程序设计>第9周学习总结 教材学习内容总结 第十六章 整合数据库 16.1JDBC 16.1.1JDBC简介 JDBC(Java DataBase Connec ...

  6. Idea 切换git账号

    重置一下账号设置,再次执行拉取或推送会提示重新输入账号密码 进入项目根目录执行:git config --system --unset credential.helper

  7. scrapy之手机app抓包爬虫

    手机App抓包爬虫 1. items.py class DouyuspiderItem(scrapy.Item): name = scrapy.Field()# 存储照片的名字 imagesUrls ...

  8. 【python】argparse学习(转)

    点击这里成为作者 · 更新于 2018-11-14 21:00:36 argparse argparse 是 Python 内置的一个用于命令项选项与参数解析的模块,通过在程序中定义好我们需要的参数, ...

  9. Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'

    Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String' 一.发现问题 <select ...

  10. 我的nlp之路(1)

    1/8日任务 基础篇: 如何使用远程连接从windows或者linux连到服务器进行操作(切换用户,传递文件) (严禁使用root账户) Linux基本bash命令 a)        查看文件大小, ...