Leetcode_Longest Substring Without Repeating Characters
来源:互联网 发布:mac怎么一键回到桌面 编辑:程序博客网 时间:2024/06/15 19:55
leetcode倒数第三题,地址:http://oj.leetcode.com/problems/longest-substring-without-repeating-characters/
思路:用deque和map来模拟,从后插入deque,同时map存一份表示此char已存入,当发现当前char已存入deque的时候,从deque的front开始pop,知道把与当前char相同的char 给pop出去,同时map也做维护,当q的size最大时,也就是要返回的值。
形象上就是两个指针一前一后在移动,有char进来并不与两指针之间的char重复的时候,后指针移动(往后),否则前指针移动(往后)到重复char的后一位置。map维护快速查询。
参考代码:
class Solution {public: int lengthOfLongestSubstring(string s) { int maxlen = 0, len = 0; deque<char> q; map<char, bool> resmap; for(int i = 0; i < s.length(); ++i) { auto it = resmap.find(s[i]); if(it == resmap.end()) { ++len; q.push_back(s[i]); maxlen = maxlen > len ? maxlen : len; resmap.insert(pair<char,bool>(s[i], true)); } else { while(!q.empty() && q.front() != s[i]) { auto deleteIt = resmap.find(q.front()); resmap.erase(deleteIt); q.pop_front(); } q.pop_front(); q.push_back(s[i]); len = q.size(); } } return maxlen; }};
SECOND TRIAL, 204MS
class Solution {public: int lengthOfLongestSubstring(string s) { if(s.empty()) return 0; unordered_map<char, int>mp; unordered_map<char, int>::iterator it; int i = 0, mlen = 0; for(int k = 0; k<s.length(); ++k) { it = mp.find(s[k]); if(it == mp.end()) mp[s[k]]=k; else { while(s[i]!=s[k]) { it = mp.find(s[i]); mp.erase(it); ++i; } ++i; } mlen = max(mlen, k-i+1); } return mlen; }};
PYTHON VERSION, 392MS
class Solution: # @return an integer def lengthOfLongestSubstring(self, s): if not s: return 0 d = {} maxlen = i = 0 for j in range(0, len(s)): if not d.has_key(s[j]): d[s[j]] = j else: while s[i] != s[j]: del d[s[i]] i+=1 i+=1 maxlen = max(maxlen, j-i+1) 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
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- String_Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters
- 属性的自动实现(Auto-Implemented Properties) - VS2005
- Shopping Cart page
- hadoop2.2.0 单节点安装 -翻译自hadoop官方文档-与原文有出入
- 一个和会计李菊iioio
- 采集卡采集浮地信号源的接线端接线方式
- Leetcode_Longest Substring Without Repeating Characters
- Linux下jetty启动报错解决方法
- 中文女与程序员的爱情
- HDU 3903 Trigonometric Function (三角恒等式&余弦定理)
- 使用JDBC在MySQL数据库中快速批量插入数据
- 《JavaSE基础教程》电子版书正式发布,欢迎大家下载
- acm.zju.1586
- 错误“Unexpected namespace prefix "xmlns" found for tag LinearLayout”的解决方法
- 学校1036: 好老师