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.

Hide Tags

Hash Table Two Pointers String

 

    这题有前面的基础其实很简单的,http://www.cnblogs.com/Azhu/p/4127606.html
    思路类似,使用双索引表示目标的头尾,左索引指向能去的第一个,右索引指向不取的第一个,那么初始化均为0,长度就是右-左,两者相等表示子串为空。
注意:
  • 子串与子序列不同,子串是需要连续的,子序列只要求顺序,不要求连续。
  • 表示好子串(窗口)很重要,确认清楚索引指向是否在窗口内。
  • 因为需要标记字符,所以直观想到的是hash_map or  unordered_map,不过基于AscII 只要一个128 位的bool 数组,discuss 他们声明为256,不过用128通过了。
  • 数组下标如果直接使用字符好像隐式转换失败,需要加上强制转换。

算法实现:

  1. 初始化窗口索引lft=rgt=0,同时初始化标记数组,表示那个一个字符是否在窗口内。
  2. 右索引遍历字符串
    • 如果遇到不在窗口内的字符rgt+1,改变flag,计算更新最长子串长度。
    • 如果在窗口内,lft+1,修改flag,重新判断rgt 位置的字符。

我写的代码:

 #include <iostream>
#include <string>
using namespace std; class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.length();
if(n<) return n ;
int lft = ,rgt =,maxlen = ;
bool sign[] = {false};
while(rgt<n){
// cout<<lft<<" "<<rgt<<" "<<maxlen<<endl;
if(sign[(int)s[rgt]]==false){
sign[(int)s[rgt]]=true;
rgt++;
if(maxlen<rgt-lft) maxlen = rgt - lft;
continue;
}
sign[(int)s[lft]] = false;
lft++;
}
return maxlen;
}
}; int main()
{
string s= "242522f23r23rt432twrfs122";
Solution sol;
cout<<sol.lengthOfLongestSubstring(s)<<endl;
return ;
}

[LeetCode] Longest Substring Without Repeating Characters最长无重复子串的更多相关文章

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

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

  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 Longest Substring Without Repeating Characters 最长不重复子串

    题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...

  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. For example, ...

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

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

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

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

随机推荐

  1. 【JAVA】JVM常用工具

    JDK内置工具使用 jps(Java Virtual Machine Process Status Tool)    查看所有的jvm进程,包括进程ID,进程启动的路径等等. jstack(Java ...

  2. python3 题目 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?

    方法一:for循环遍历 counter=0 for i in range(1,5): for j in range(1,5): for k in range(1,5): if i !=j and j ...

  3. 时间转换,django的时间设置,re模块简单校验密码和手机号

    时间转换和密码,手机的re模块简单校验 import re,time def check_userinfo(request): pwd = request.POST.get("pwd&quo ...

  4. request_resource

    1.全局变量 resource结构体定义如下,指针parent.sibling.child用于构建树状结构. struct resource { resource_size_t start; reso ...

  5. Debug调试文件

    在debug.h中设置g_debug_switch即可控制调试级别. /* debug.c */ #include "debug.h" const char *get_log_le ...

  6. git上传自己的代码

    感谢这个哥们的博客,不过里面有些错误. http://www.cnblogs.com/ruofengzhishang/p/3842587.html 下面是我自己的实践成功的: 这篇文章写得是windo ...

  7. 运维Python面试题

    本章内容 1 osodjfoskjdofjsdjfjsdf 123 sdfdfadf   1 2 3 4 5 6 from django.db import models     class user ...

  8. 【Trapping Rain Water】cpp

    题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...

  9. 转行自学 Java 之路的纪念册

    前言: 最近在重读<小狗钱钱>,我对其中的"成功日记"概念特别深刻,偶尔也会记一记“成功日记”. 想了想人生走找到今天,阶段性“成功日记”有没有呢? 有的!几年前的一篇 ...

  10. live 555 freebsd 或centos 7.4 实现代理视频直播服务

    live 555   freebsd 或centos 7.4 实现代理视频直播服务 the live555 media server    在线直播服务器 关于此服务器 此服务是一个无安全的rtsp服 ...