#include<string>
#include<iostream>
using namespace std;
//收获
//记住string的几个函数用法
// class Solution {
// int a[40002] = { 0 };
// int pre=0;
// public:
// int lengthOfLongestSubstring(string s) {
// for (int i = 0; i <= s.size(); i++)
// {
// if (i == 0)a[0] = 0;
// else
// {
// if (s.find(s[i - 1], 0) == -1 || s.find(s[i - 1], 0) >= i - 1)
// {
// a[i] = a[i - 1] + 1;
// }
// else
// {
// a[i] = i - max((int)(s.rfind(s[i - 1], i-2)),pre)-1;
// pre = max((int)(s.rfind(s[i - 1], i - 2)), pre);
// }
// }
// }
// int max = a[0];
// for (int i = 0; i <= s.size(); i++)
// {
// if (max < a[i])
// max = a[i];
// }
// return max;
// }
// };
class Solution {
int a;//因为dp[i]只与dp[i-1]有关 进一步优化内存
int max1;
int pre=0;
public://总体思路 寻找并记录以s[i]为结尾的字符所对应的最大子串 就是连续最大子数组和的翻版 改了一点
int lengthOfLongestSubstring(string s) {
for (int i = 0; i <= s.size(); i++)
{
if (i == 0)
{
a=0;
max1=0;
}
else
{//如果这个字符在前面找不到
if (s.find(s[i - 1], 0) == -1 || s.find(s[i - 1], 0) >= i - 1)
{
a = a + 1;//那dp[i]=dp[i-1]+1
max1=max(a,max1);
}
else
{//如果这个字符在前面找得到 那么找前面已经重复了的最靠近此字符的字符 记录此时两者距离
a = i - max((int)(s.rfind(s[i - 1], i-2)),pre)-1;
max1=max(a,max1);
pre = max((int)(s.rfind(s[i - 1], i - 2)), pre);
}
}
}
return max1;
}
};
//一般来说C++库函数下标都是从0开始算的
//string查找find 的用法 和库函数中的find不同 要注意
//string a
//a.find(字符/字符串,起始位置)找不到返回-1
//a.rfind 从右往左查
//a.replace(起始位置下标,终止位置下标,"字符串")//左闭右开
//a.insert(起始位置,“字符串”)
//a.insert(起始位置,n个,字符c)
//a.erase(起始位置,多少个)
//a.substr(起始位置,多少个)
int main()
{
}
#include<iostream>
#include<map>
using namespace std;
class Solution {
int a[245]={0};
public:
int lengthOfLongestSubstring(string s) {
if (s.size() == 0)return 0;
else
{
int l = 0;
int r = 0;
int max1 = 1;
while (r < s.size())
{
if (a[s[r]] == 0)
{
a[s[r]]++;
r++;
max1=max(r-l,max1);
}
else
{
a[s[l]]=0;
l++;
}
}
return max1;
}
}
};//这个数组快!重点在于不要删除那个长字符串的元素很耗时间
int main()
{
}