LeetCode-3. Longest Substring Without Repeating Characters
  Ld1ydcUEqbSi 2023年11月02日 27 0
i++


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.


题解:

动态记录上一次不重复点

class Solution {
public:
int lengthOfLongestSubstring(string s) {
int prev = -1, res = 0;
int len = s.length();
vector<int> idx(200, -1);
for (int i = 0; i < len; i++) {
if (idx[s[i]] != -1) {
prev = max(prev, idx[s[i]]);
}
res = max(res, i - prev);
idx[s[i]] = i;
}
return res;
}
};

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  rEZj93RghFYQ   2023年11月02日   20   0   0 i++leetcode-java
  dUbcXj9lnElT   2023年11月02日   29   0   0 #includei++c++
  dUbcXj9lnElT   2023年11月02日   19   0   0 #include连通块i++
Ld1ydcUEqbSi
最新推荐 更多