[LeetCode] Longest Substring Without Repeating Characters
2015-06-07 21:14
417 查看
Well, there many ways to solve this problem. Let's first look at a naive solution.
The basic idea is simple. Starting from the first character of the string, we visit the next character sequentially. If no duplicates appear, we count the length and update the maximum length. When we meet a repeated character, we find the position of its previous appearance (denoted as pre) and set the starting position to be pre + 1 and repeat the above process till the ending point has exceeded the length of the string. To implement this algorith, we need a hash table to record the last occurrence of each character we see and two pointers for the starting and ending position.
The code is as follows.
I hope this code to be self-explanatory enough. In fact, we can see that the record of end is to obtain the length of the current substring. So we may simply use another variable to store the length directly. The code then becomes as follows.
Both of the above codes have called memset several times and thus take much time. In fact, only the characters after the new starting point (inclusive) need to be reset. So we can further reduce the amout of work. We can use a boolean arary to denote whether the element has been appeared and reset those after the new starting point (inclusive).
The following code runs much faster!
One of my friend suggests the following code. It may be hard to understand for the first time. The trick is that it records the length of the current substring and updates it whenever a duplicate occurred.
Some nice people posted the following short DP code in the LeetCode discuss. The code is damn cool! Please refer to this link for more explanations.
The basic idea is simple. Starting from the first character of the string, we visit the next character sequentially. If no duplicates appear, we count the length and update the maximum length. When we meet a repeated character, we find the position of its previous appearance (denoted as pre) and set the starting position to be pre + 1 and repeat the above process till the ending point has exceeded the length of the string. To implement this algorith, we need a hash table to record the last occurrence of each character we see and two pointers for the starting and ending position.
The code is as follows.
// Naive implementation using two pointers int lengthOfLongestSubstring(string s) { int pos[256]; memset(pos, -1, sizeof(pos)); int start = 0, end = 0, maxlen = 0; while (end < (int)s.length()) { if (pos[s[end]] >= 0) { maxlen = max(maxlen, end - start); start = pos[s[end]] + 1; end = start; memset(pos, -1, sizeof(pos)); } else pos[s[end]] = end++; } return max(maxlen, end - start); }
I hope this code to be self-explanatory enough. In fact, we can see that the record of end is to obtain the length of the current substring. So we may simply use another variable to store the length directly. The code then becomes as follows.
// Naive implementation using one pointer int lengthOfLongestSubstring(string s) { int pos[256]; memset(pos, -1, sizeof(pos)); int start = 0, len = 0, maxlen = 0; while (start < (int)s.length()) { if (pos[s[start]] >= 0) { maxlen = max(maxlen, len); start = pos[s[start]] + 1; memset(pos, -1, sizeof(pos)); len = 0; } else { pos[s[start]] = start++; len++; } } return max(maxlen, len); }
Both of the above codes have called memset several times and thus take much time. In fact, only the characters after the new starting point (inclusive) need to be reset. So we can further reduce the amout of work. We can use a boolean arary to denote whether the element has been appeared and reset those after the new starting point (inclusive).
The following code runs much faster!
int lengthOfLongestSubstring(string s) { bool exist[256] = {false}; int start = 0, end = 0, maxlen = 0; while (end < (int)s.length()) { if (exist[s[end]]) { maxlen = max(maxlen, end - start); while (s[start] != s[end]) exist[s[start++]] = false; start++; end++; } else exist[s[end++]] = true; } return max(maxlen, end - start); }
One of my friend suggests the following code. It may be hard to understand for the first time. The trick is that it records the length of the current substring and updates it whenever a duplicate occurred.
int lengthOfLongestSubstring(string s) { int pos[256]; memset(pos, -1, sizeof(pos)); int len = 0, maxlen = 0; for (int i = 0; i < s.length(); i++) { int pre = pos[s[i]]; if (pre == -1 || i - len > pre) len++; else { maxlen = max(maxlen, len); len = i - pre; } pos[s[i]] = i; } return max(maxlen, len); }
Some nice people posted the following short DP code in the LeetCode discuss. The code is damn cool! Please refer to this link for more explanations.
// Dynamic Programming int lengthOfLongestSubstring(string s) { int pos[256]; memset(pos, -1, sizeof(pos)); int start = 0, maxlen = 0; for (int i = 0; i < s.length(); i++) { start = max(start, pos[s[i]] + 1); maxlen = max(maxlen, i - start + 1); pos[s[i]] = i; } return max(maxlen, (int)s.length() - start); }
相关文章推荐
- 心中有爱
- 回憶 那時年少(一)
- Codeforces Round #306 (Div. 2)
- 2014由于在myeclipse5.5.1许可证
- [leetcode 9]Palindrome Number
- 网上选课系统设计类图
- 学习java第6天 模仿XP画板(10%)
- java(android)多线程下载,和多线程断点下载
- 梦幻诛仙key的存储过程语句
- Android应用防止截屏
- 一个跨域请求的XSS漏洞再续
- Java - The SortedMap Interface
- Codeforces 550D. Regular Bridge 构造
- Signing key has not been configured
- Visio中交叉线去掉拱形
- 关于软件测试的方法和实现
- SQL的主键和外键约束详解及用途
- VS2010编译jpeg.lib
- Signing key has not been configured
- Ubuntu 中安装GTK