赞
踩
给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度。
可以使用滑动窗口加哈希表来实现:
class Solution { public: int lengthOfLongestSubstring(string s) { int start = 0, end = 0, len = 0, ans = 0; int size = s.size(); unordered_map<char,int> hash_map; while (end < size) { char tmp = s[end]; if (hash_map.count(tmp) > 0 && hash_map[tmp] >= start) { start = hash_map[tmp] + 1; len = end - start; } hash_map[tmp] = end; ++end; ++len; ans = ans > len ? ans : len; } return ans; } };
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。