当前位置:网站首页>384 · longest substring without repeated characters
384 · longest substring without repeated characters
2022-04-22 07:29:00 【yinhua405】
describe
Given a string , Please find the longest substring in which there are no duplicate characters .
Examples
Examples 1:
Input : "abcabcbb"
Output : 3
explain : The longest string is "abc".
Examples 2:
Input : "bbbbb"
Output : 1
explain : The longest string is "b".
Challenge
O(n) Time complexity
int lengthOfLongestSubstring(string &s) {
// write your code here
map<char, int> smap;
int start = 0;
int end = 0;
int ssize = s.size();
if (ssize == 1)
{
return 1;
}
int minstart = 0;
int minSize = 0;
map<char, int> stmap;
while (start < ssize)
{
if (end - start > minSize)
{
minstart = start;
minSize = end - start;
}
if (end == ssize)
{
start++;
end = start;
continue;
}
if (smap.count(s[end]))
{
start=smap[s[end]]+1;
end = start;
smap = stmap;
}
else
{
smap[s[end]] = end;
end++;
}
}
return minSize;
}
版权声明
本文为[yinhua405]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220613324692.html
边栏推荐
- Virtual machine disk space shrinks
- Introduction
- 384 · 最长无重复字符的子串
- 437. 路径总和 III
- 重写与重载的定义与区别
- LeetCode -3 - (字符串相加、最大连续1的个数<ⅠⅢ>、考试的最大困扰度、删除链表的倒数第N个结点)
- [number theory] congruence (4): univariate linear congruence equations (elimination of two, Chinese remainder theorem)
- LeetCode - 8 - (三数之和、Z字形变换、两数之和<链表>、盛最多水的容器、电话号码的字母组合)
- Win10 modify command line default font
- In the process of class loading, the allocation area of class variables is different from that of instance variables
猜你喜欢

Virtual machine disk space shrinks

Host cannot Ping virtual machine in bridging mode

最强操作符学习之路(C语言详解)

LeetCode - 4 - (接雨水、无重复字符的最长子串、分发糖果、二叉树的<前中后层>序遍历)
![[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted](/img/bf/0851c85f766432f6d6306d52e67188.png)
[DRC RTSTAT-1] Unrouted nets: 1 net(s) are unrouted

C language | array

LeetCode - 2 - (括号生成、最长回文串、环形链表、反转链表、两两交换链表中的节点)

顺序表 增删查(找)

Idea does not display the run dashboard view window

Add author photos and author profiles to latex
随机推荐
Vscode, this is enough
[number theory] congruence (III): univariate linear congruence equation
Introduction
[number theory] congruence (I): basic concepts and properties of congruence
C.Ducky Debugging(简单判断/签到)(2021年度训练联盟热身训练赛第五场 )
SQL review, grammar notes, fresh out
【数论】同余(一):同余的基本概念与性质
SecureCRT infinite loop script
1242 · 无重叠区间
二叉树链式结构操作LeetCode+牛客(详解)
浅谈时间复杂度与空间复杂度
LeetCode - 1 - (树的子结构、组合、螺旋矩阵、全排列<ⅠⅢ>)
Redis進階
Anaconda installation and use
LeetCode - 2 - (括号生成、最长回文串、环形链表、反转链表、两两交换链表中的节点)
437. 路径总和 III
L1-071 前世档案 (20 分) (类似二分)
15 · 全排列
指针 结构体 const 小结
C language | array