leetcode 之Longest Substring Without Repeating Characters
来源:互联网 发布:mac itunes 12.7 铃声 编辑:程序博客网 时间:2024/05/19 18:12
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
定义 L[ i ] =s[m...i],表示无重复字符的最长子串,且以 s[m] 开始,以s[i]结束。 同时我们对 [m...i]维护一个 hashmap,结构为<char ,int >,键为字符,对应的值是出现在原始字符串s中的位置。当我们计算到 s[i+1]时,
1.如果s[i+1]没有出现在s[m...i]中(可以用hashmap来查找),则将s[i+1]添加进去,此时L[i+1]= s[m...i i+1]。
2.如果s[i+1]出现在s[m...i]中, 假设其位置为j,则 L[i+1]=s[j+1...i+1]。
class Solution {public: int lengthOfLongestSubstring(string s) { unordered_map<char, int> hash; int maxlen = 0; int index = 0; for (int i = 0; i < s.size(); i++) { if(hash.find(s[i]) != hash.end()) { index = hash[s[i]]; for (auto it = hash.begin(); it != hash.end();) { if (it->second <= index) { it = hash.erase(it); }else{ it++; } } } hash[s[i]] = i; if (maxlen < hash.size()) maxlen = hash.size(); } return maxlen; }};
0 0
- leetcode之Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- LeetCode 之 Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- leetcode之 Longest Substring Without Repeating Characters
- Leetcode 之 Longest Substring Without Repeating Characters
- leetcode 之Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- 【Leetcode】之Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- leetcode之Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- leetcode之 Longest Substring Without Repeating Characters
- LeetCode之Longest Substring Without Repeating Characters
- LeetCode 之 Longest Substring Without Repeating Characters
- 装饰模式
- try catch可以这么用:校验!
- Java中的集合类图
- android numberpicker控件的布局在没有标题情况下设置3.0新样式方法
- Android双向滑动菜单完全解析,教你如何一分钟实现双向滑动特效
- leetcode 之Longest Substring Without Repeating Characters
- 赠送 HttpClient 和HttpURLConnection 的轻型网络框架 ---》常见异常分析
- 浅谈如何写递归函数
- 粒子滤波
- 【BNUOJ】电网建设(MST,prim)
- test、
- node.js中的require
- Android多点触控技术实战,自由地对图片进行缩放和移动
- 一头栽下结束生命的鹰