[LeetCode] Longest Substring Without Repeating Characters最长无重复子串
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.
- 子串与子序列不同,子串是需要连续的,子序列只要求顺序,不要求连续。
- 表示好子串(窗口)很重要,确认清楚索引指向是否在窗口内。
- 因为需要标记字符,所以直观想到的是hash_map or unordered_map,不过基于AscII 只要一个128 位的bool 数组,discuss 他们声明为256,不过用128通过了。
- 数组下标如果直接使用字符好像隐式转换失败,需要加上强制转换。
算法实现:
- 初始化窗口索引lft=rgt=0,同时初始化标记数组,表示那个一个字符是否在窗口内。
- 右索引遍历字符串
- 如果遇到不在窗口内的字符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最长无重复子串的更多相关文章
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串 C++实现java实现
最长无重复字符的子串 Given a string, find the length of the longest substring without repeating characters. Ex ...
- [LeetCode] 3.Longest Substring Without Repeating Characters 最长无重复子串
Given a string, find the length of the longest substring without repeating characters. Example 1: In ...
- LeetCode Longest Substring Without Repeating Characters 最长不重复子串
题意:给一字符串,求一个子串的长度,该子串满足所有字符都不重复.字符可能包含标点之类的,不仅仅是字母.按ASCII码算,就有2^8=128个. 思路:从左到右扫每个字符,判断该字符距离上一次出现的距离 ...
- 【LeetCode】3.Longest Substring Without Repeating Characters 最长无重复子串
题目: Given a string, find the length of the longest substring without repeating characters. Examples: ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)
Given a string, find the length of the longest substring without repeating characters. Example 1: ...
- 3. Longest Substring Without Repeating Characters - 最长无重复字符子串-Medium
Examples: Description: Given a string, find the length of the longest substring without repeating ch ...
随机推荐
- 【PHP】foreach语法
foreach 语法结构提供了遍历数组的简单方式.foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息.有两种语法: foreach ($name ...
- Redis之set类型操作
接口: package com.net.test.redis.base.dao; /** * @author*** * @Time:2017年8月10日 下午2:32:12 * @version 1. ...
- python3.7 time模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 time模块 #time模块没有time.py文件,是内置到解释 ...
- 三次样条插值matlab实现
三次样条插值matlab实现 %三次样条差值-matlab通用程序 - zhangxiaolu2015的专栏 - CSDN博客 https://blog.csdn.net/zhangxiaolu201 ...
- 动态规划(入门,滚动数组,记录的都是状态):SWUSTACM-1010 魔兽争霸之最后的反击
题目: 1010: 魔兽争霸之最后的反击 Time Li ...
- Codeforces Round #496 (Div. 3) ABCDE1
//B. Delete from the Left #include <iostream> #include <cstdio> #include <cstring> ...
- Leetcode 872. 叶子相似的树
题目链接 https://leetcode-cn.com/problems/leaf-similar-trees/description/ 题目描述 请考虑一颗二叉树上所有的叶子,这些叶子的值按从左到 ...
- hadoop伪分布式集群的搭建
集群配置: jdk1.8.0_161 hadoop-2.6.1 linux系统环境:Centos6.5 创建普通用户 dummy 设置静态IP地址 Hadoop伪分布式集群搭建: 为普通用户添加su ...
- MongoDB快速入门学习笔记6 MongoDB的文档删除操作
db.集合名称.remove({query}, justOne)query:过滤条件,可选justOne:是否只删除查询到的第一条数据,值为true或者1时,只删除一条数据,默认为false,可选. ...
- MongoDB快速入门学习笔记3 MongoDB的文档插入操作
1.文档的数据存储格式为BSON,类似于JSON.MongoDB插入数据时会检验数据中是否有“_id”,如果没有会自动生成.shell操作有insert和save两种方法.当插入一条数据有“_id”值 ...