leetcode之Longest Substring Without Repeating Characters
来源:互联网 发布:锐志轮毂数据 编辑:程序博客网 时间:2024/05/28 06:07
刚开始没有啥别的办法用了一个最笨的方法,当然是时间超时。。。。
int lengthOfLongestSubstring(char* s) { int length; int *index = (int *)malloc(sizeof(int) * strlen(s)); for (int i = 0; i < strlen(s); i++) for (int j = i+1; j < strlen(s); j++){ if (s[i] == s[j]){ index[i] = j - i; break; } } for (int i = 0; i < strlen(s); i++) for (int j = i+1; j <= index[i] + i; j++){ if (index[i] > index[j]) index[i] = index[j]; } length = index[0]; for (int i = 1; i < strlen(s); i++){ if (index[i] > length) length = index[i]; } return length;}
下面是我学习到的东西,就是在一次用了hashtable这个思想,这个思想在处理字符串的时候尤为重要,可以把查询的速度降到hashtable[256]
的使用,因为assci字符对应的数字只有256个,那么可以用这个数组记录每个字符的下标。
下面是找到最大没有重复字符字串的基本思想:
窗口思想,维护一个可变换大小的窗口,窗口里面是s的字串,我们要做的就是保证窗口里面的字符不能有重复的,用算法实现起来就是两个指针,分别在s的的不同位置,一前一后,这个窗口的大小就是字串的长度,在开始的时候两个指针应该都在s的0的位置,然后开始移动前面的指针,也就是扩大窗口,并且同时检测新加入的字符有没有重复,这个通过hashtable实现,如果出现则计算此时窗口的大小,然后确定最大长度,然后将窗口的左边界向前移动,移动到越过第一个重复的字符,(比如“abcdfcef”,当窗口的右边界到达第二个c的时候,左边界就可以移动到一个c后面的d上)就这样重复即可。
int lengthOfLongestSubstring(char* s) { if (s == NULL || s[0] == '\0') return 0; int hashTable[256]; memset(hashTable, 0, sizeof(hashTable)); int i, j, length; i = 0; j = 0; length = 0; while (s[i]){ if (hashTable[s[i]]){ if (length < i - j) length = i - j; while (hashTable[s[i]]){ hashTable[s[j]] = 0; j ++; } } hashTable[s[i]] = 1; i++; } if (length < i - j) length = i - j; return length;}
下面是通过python字符串实现的,比较简单,原理也可以说和上面的大致相同,但是是通过操作字符串实现的,不断变换字串实现的。
class Solution: # @param {string} s # @return {integer} def lengthOfLongestSubstring(self, s): result = "" i = 0 length = 0 while i < len(s): if result.find(s[i]) != -1: if length < len(result): length = len(result) result = result[result.find(s[i])+1:] result = '%s%s' % (result, s[i]) i = i + 1 if length < len(result): length = len(result) return length
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
- java 静态变量与方法实例变量与方法的内存占用
- JavaScript事件
- LeetCode OJ 之 Power of Two (2的n次方)
- Andorid 应用程序资源
- 弄。。
- leetcode之Longest Substring Without Repeating Characters
- 嵌入式linux之按键驱动程序
- 斐波那契数列
- 八种Docker容器开发模式
- 建立ejs模板node网站的步骤
- 关于树的某些操作
- 技术需要沉淀
- google相关包引入报错
- Android 混淆打包详解