赞
踩

https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
解题思路:
具体:
优化:
class Solution { public: int lengthOfLongestSubstring(string s) { // 用一个map保存字符上次出现的位置 // 遍历字符串 // 如果字符没有出现过,则cur++ // 如果字符出现过,则更新 i 的下标为 max(i, board[s[i]]),原因:上次重复的位置可能在i前面,但 i 只能表示当前没有重复的位置 unordered_map<char, int> board; int i = 0, j = 0, ans = 0, cur = 0; for (int j = 0; j < s.size(); ++j) { if (board.count(s[j])) { // 字符s[j]出现过 i = max(board[s[j]], i); cur = j - i; } else ++cur; ans = max(cur, ans)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。