作者:迷茫的路人甲- | 来源:互联网 | 2024-11-05 00:05
篇首语:本文由编程笔记#小编为大家整理,主要介绍了4_Longest Substring Without Repeating Characters相关的知识,希望对你有一定的参考价值。 //abcab
篇首语:本文由编程笔记#小编为大家整理,主要介绍了4_Longest Substring Without Repeating Characters相关的知识,希望对你有一定的参考价值。
//abcabcbb abc 3
//bbbbb b 1
//pwwkew wke 3
#include<iostream>
#include
#include
using namespace std;
int lengthOfLongestSubstring(string s)
if(s.size()&#61;&#61;0) return 0;
//unodered_map map;
vector map(256,-1);
int maxlen&#61;0,count&#61;0;
for(int i&#61;0;i
char c&#61;s[i];
if(map[c]&#61;&#61;-1)
count&#43;&#43;;
else
// find a match
// count
// *x*abcx, 3->4
// ***xbcx, 3->3
// ***axcx, 3->2
int loc&#61;map[c];
if(loc&#43;count4
count&#43;&#43;;
else if(loc&#43;count&#61;&#61;i) // case 3->3
;
else if(loc&#43;count>i) // case 3->2
count&#61;i-loc;
map[c]&#61;i;
if(count>maxlen)
maxlen&#61;count;
return maxlen;
int main()
string str1&#61;"abcabcbb";
cout< string str2&#61;"bbbbb";
cout< string str3&#61;"pwwkew";
cout<
return 0;
//g&#43;&#43; Longest_Substring.cc -o Longest_Substring
//./Longest_Substring