[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 ...
随机推荐
- python之质数
质数(prime number)又称素数,有无限个 质数定义为在大于1的自然数中,除了1和它本身以外不再有其他因数. 示例: num=[]; i=2 for i in range(2,100): j= ...
- FreeRTOS的学习路线
背景 由于之前接触过一些嵌入式RTOS,如Keil-RTX,uCOS-II,也曾经关注过FreeRTOS,但一直没有机会采用FreeRTOS开发.目前FreeRTOS做为主流RTOS,风声正盛.作为嵌 ...
- 2019年Vue学习路线图
https://juejin.im/entry/5c108864f265da61726555ed 官网: https://cn.vuejs.org/index.html js引入地址 https:// ...
- GoF23种设计模式之结构型模式之桥接模式
一.概述 将类的抽象部分与实现分部分离开来,使它们都可以独立地变化. 二.适用性 1.你不希望在抽象和实现之间有一个固定的绑定关系的时候.例如:在程序运行时实现部分应可以被选择或切换. ...
- C++多态实例
#include <iostream> #include <string> using namespace std; //class 实现 class Employee { s ...
- c++ vector实例
#include <iostream> #include <string> #include <vector> #include <iostream> ...
- Gym - 101128F Landscaping(网络流)
题意 给你一个\(N×M\)的草地,有高地有低地. 收割机从低地走到高地或者从高地走到低地都要花费用\(A\),你可以花费用\(B\)把一块高地变成低地,或者把一块低地变成高地.收割机每行每列都是必须 ...
- Patrick and Shopping
Patrick and Shopping 今天 Patrick 等待着他的朋友 Spongebob 来他家玩.为了迎接 Spongebob,Patrick 需要去他家附近的两家商店 买一些吃的.他家 ...
- loj2091 「ZJOI2016」小星星
ref 总的来说,就是 容斥转化为点对应到点集问题. 树形 dp 解决转化后的问题. #include <iostream> #include <cstring> #inclu ...
- luogu2120 [ZJOI2007]仓库建设
大米饼写的太棒辣qwqqwq #include <iostream> #include <cstdio> using namespace std; typedef long l ...