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. 20145321 《Java程序设计》第4周学习总结

    20145321 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承与多态 6.1 何谓继承 1.继承共同行为: 继承基本上就是避免多个类间重复定义的行为. Pull Up ...

  2. Windows7使用无线网卡建立WiFi热点

    在Windows7下设置热点需要用到命令netsh wlan,具体的设置步骤如下: 1.配置热点 以管理员身份打开命令行模式,输入命令 netsh wlan set hostednetwork mod ...

  3. 防止XSS攻击的方式

    主要有三种请求方式,进行过滤替换非法符号 1.普通的GET请求数据: 2.FORM表单提交数据: 3.Json格式数据提交: 把下面5个文件放入项目中即可 package com.joppay.adm ...

  4. centos7更改主机名

    操作环境 [root@centos701 ~]# uname Linux [root@centos701 ~]# uname -a Linux centos701 3.10.0-693.el7.x86 ...

  5. Spring Boot 之restful风格

    步骤一:restful风格是什么? 我们知道在做web开发的过程中,method常用的值是get和post.可事实上,method值还可以是put和delete等等其他值. 既然method值如此丰富 ...

  6. 微服务(一)eureka

    Eureka概述 一个简单的服务注册,服务发现架构 在CAP理论中,eureka选择了ap,作为注册中心,数据可用比数据不一致更重要 逻辑架构图 Eureka特性 1.当注册中心挂了,客户端之间依然可 ...

  7. 解题报告:hdu2191汶川地震 - 多重背包模板

    2017-09-03 17:01:36 writer:pprp 这是一道多重背包裸题 - 记得是从右向左进行,还有几点需要注意啊,都在代码中表示出来了 代码如下: /* @theme:hdu2191 ...

  8. 对spring boot 之AutoConfiguration 的理解

    来自:http://rensanning.iteye.com/blog/2363467 https://blog.csdn.net/tincox/article/details/79186067 Au ...

  9. centos mysql忘记密码

    1.停止mysql 服务:service mysqld stop; 2.vim命令打开mysql配置文件my.cnf(位置一般为:/etc/my.cnf) 3.在mysqld进程配置文件中添加skip ...

  10. centos7 systemctl一些用法

    systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体. systemctl is-enabled servicename.service #查询服务是否 ...