[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 ...
随机推荐
- setInterval与setTimeout
在自己用canvas画一个时钟时,画秒钟用的是利用图片将重复的线条遮住,但是会出现有两个秒钟线条同时存在,才想起setInterval有那么个坑,查了点资料,记录下,若有不对的或者未写到的点,还请大家 ...
- python练手习题
不断记录python常见习题,不断寻求更多更好的解决办法.持续更新中..... 练习: 1. list两两元素交换位置,如[1,2,3,4,5,6] 执行后为 -> [2,1,4,3,6,5] ...
- 洛谷 P1736 创意吃鱼法(多维DP)
题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...
- TI C64X+通用库函数使用手册
在使用前,当知悉以下几点: 函数进程由手动汇编而成,已充分发挥器件效率.同时TI对外提供C和线性汇编代码 对于个人一些特殊应用,DSPLIB可能会带来额外的cycle消耗 TI DSPLIB依平台和时 ...
- Mysql密码加密方式
以Mysql 4.1版本为分界线,两种加密方式 Mysql323加密:(16位) select old_password('root'); //Mysql自带加密函数old_password(str ...
- datagrid的右键菜单
1. 2.右键菜单,主要是用onRowContextMenu:function(e,index,row){}方法来实现 onRowContextMenu:function(e,index,row){ ...
- Cakephp在Controller中显示sql语句
Cakephp在Controller中查询语句一般是: $this->Model->find(); 那么这条语句对应的sql语句是什么呢? 可以通过下面方法显示: 1. $dbo = Co ...
- easyui-combogrid必填为空时无法通过表单验证的问题
在使用easyui-combogrid时,由于html解析出的格式是如下三层: <td> <input id="txcombo" class="easy ...
- idea 占用内存优化调整
idea 占用内存优化调整 https://www.cnblogs.com/metoy/p/5967696.html https://blog.csdn.net/zdxxinlang/arti ...
- Careercup - Microsoft面试题 - 5485521224597504
2014-05-12 06:19 题目链接 原题: Given an input list of lists.. flatten the list. For e.g. {{,}, {}, {,}} . ...