/*
Given a string, find the length of the longest substring without repeating characters.
Example 1:
Input: “abcabcbb”
Output: 3
Explanation: The answer is “abc”, with the length of 3.
Example 2:
Input: “bbbbb”
Output: 1
Explanation: The answer is “b”, with the length of 1.
Example 3:
Input: “pwwkew”
Output: 3
Explanation: The answer is “wke”, with the length of 3.
Note that the answer must be a substring, “pwke” is a subsequence and not a substring.
*/
/*
整体思路:
使用vector<int> dic(255, -1)进行数据的保存,
*/
class Solution {
public:
int lengthOfLongestSubstring(string s) {
vector<int> dic(255,-1); //巧妙的利用了数组的特性 数组的下标表示对应的字符,
//存储的元素保存该字符在元字符串中的位置
int maxLen = 0; //保存最大不重复子串的长度
int start = -1; //保存前面不重复的位置
for(int i = 0; i < s.size(); i++){
if(dic[s.at(i)] > start){
start = dic[s.at(i)];
}
dic[s.at(i)] = i;
maxLen = max(maxLen, i - start);
}
return maxLen;
}
};
//lambda表达式
//https://www.cnblogs.com/langzou/p/5962033.html
static int x = []() {
std::ios::sync_with_stdio(false); //sync_with_stdio()函数有打开或关闭使用C++风格I/O系统混合C风格的I/O系统的功能。
std::cin.tie(NULL); //取消cin和cout的绑定
std::cout.tie(0);
return 0;
}();
开始的时候巨懵逼,查看运行速度比较快的代码都会有这一块的东西,就随手查了一下,这一堆东西属于lambda,有兴趣的可以参考这一篇博客
https://www.cnblogs.com/langzou/p/5962033.html