标签:数据 左移 ble dong targe https c++ substring 字符
C++
class Solution3 {
public:
int lengthOfLongestSubstring(string s) {
if(s.size() == 0) return 0;
unordered_set<char> lookup;
int maxStr = 0;
int left = 0;
for(int i = 0; i < s.size(); i++){
while (lookup.find(s[i]) != lookup.end()){ // 相当于一个指针先进行左移操作 left , i表示右指针
lookup.erase(s[left]);
left ++;
}
maxStr = max(maxStr,i-left+1); // i 和 left 之间的数组就是我们想要的数据
lookup.insert(s[i]);
}
return maxStr;
}
};
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length() == 0) return 0;
Set<Character> occ = new HashSet<Character>();
int n = s.length();
int rk = -1, ans = 0;
for(int i = 0; i<n; i++){
if(i!=0){
occ.remove(s.charAt(i - 1));
}
while(rk + 1 < n && !occ.contains(s.charAt(rk + 1))) {
occ.add(s.charAt(rk + 1));
++rk;
}
ans = Math.max(ans, rk - i + 1);
}
return ans;
}
}
class Solution {
public int lengthOfLongestSubstring(String s) {
if(s.length() == 0) return 0;
Set<Character> occ = new HashSet<Character>();
int n = s.length();
int left = 0;
int maxStr = 0;
for(int i = 0; i<n; i++){
while(occ.contains(s.charAt(i))) {
occ.remove(s.charAt(left));
left += 1;
}
maxStr = Math.max(maxStr, i - left + 1);
occ.add(s.charAt(i));
}
return maxStr;
}
}
简而言之, 可以代入 abcab 和 abcba
对于 abcab
首先进入字符串 扩充阶段 while (lookup.find(s[i]) != lookup.end()) 不起作用 因为 lookup 里面没有可以找到重复的字符
变为 abc maxStr = 3
然后 abc 接着 while (lookup.find( ${a字符} ) != lookup.end()) 进入循环
maxStr 还是3 变为 bca 同理最后变为 cab
abc 插入b 的时候 变为 cb
标签:数据 左移 ble dong targe https c++ substring 字符
原文地址:https://www.cnblogs.com/eat-too-much/p/14766609.html