LeetCode003 Longest Substring Without Repeating Characters
来源:互联网 发布:淘宝退款凭证怎么做 编辑:程序博客网 时间:2024/05/22 00:07
详细见:leetcode.com/problems/longest-substring-without-repeating-characters/
说明:map/dict/数组
Java Solution: github
package leetcode;import java.util.Arrays;public class P003_LongestSubstringWithoutRepeating {public static void main(String[] args) {new Solution1().lengthOfLongestSubstring("mabcdabcdmmm");new Solution1().lengthOfLongestSubstring("mabcd");}/* * 82.35% */static class Solution {int[] map = new int[128]; public int lengthOfLongestSubstring(String s) { if (s == null) return 0; if (s.length() < 2) return s.length(); int[] preIndex = new int[s.length()]; Arrays.fill(map, -1); for (int i = 0; i != preIndex.length; i ++) { int index = (int)s.charAt(i); if (map[index] == -1) { preIndex[i] = -1; map[index] = i; } else { preIndex[i] = map[index]; map[index] = i; } } int i0 = 0, i1 = 0, max = Integer.MIN_VALUE; for (; i1 != preIndex.length; i1 ++) { if (preIndex[i1] >= i0) { if (max < i1 - i0) max = i1 - i0; i0 = preIndex[i1] + 1; } } if (max < i1 - i0) max = i1 - i0; System.out.println(max); return max; }}/* * 将两次O(N)整合成一次O(N) * 86.95% */static class Solution1 {int[] map = new int[128]; public int lengthOfLongestSubstring(String s) { if (s == null) return 0; int len = s.length(); if (len < 2) return len; Arrays.fill(map, -1); int i0 = 0, i1 = 0, max = Integer.MIN_VALUE, preIndex = 0; for (; i1 != len; i1 ++) { int index = (int)s.charAt(i1); if (map[index] == -1) { preIndex = -1; map[index] = i1; } else { preIndex = map[index]; map[index] = i1; } if (preIndex >= i0) { if (max < i1 - i0) max = i1 - i0; i0 = preIndex + 1; } } if (max < i1 - i0) max = i1 - i0; return max; }}}
C Solution: github
/* url: leetcode.com/problems/longest-substring-without-repeating-characters/ 19ms 60.40%*/#include <stdio.h>#include <stdlib.h>#include <string.h>int lengthOfLongestSubstring(char* s) { int m[128]; int i = 0, j = 0; int n = 0, p = 0; int * c = NULL; int max = 0; int v = 0; if (s == NULL || * s == '\0') return 0; for (i = 0; i < 128; i ++) m[i] = -1; n = strlen(s); c = (int *) malloc(sizeof(int) * n); for (i = 0,j = 0; j < n; j ++) { v = *(s + j); p = m[v]; m[v] = j; if (p >= i) { max = max < j - i ? j - i : max; i = p + 1; } } max = max < j - i ? j - i : max; free(c); return max;}int main() { char s[] = "pwwkew"; printf("answer is %d\r\n", lengthOfLongestSubstring(s)); return 0;}
Python Solution: github
#coding=utf-8''' url: leetcode.com/problems/longest-substring-without-repeating-characters/ dict保存上一次字符出现的位置,出现重复,从下一个开始计数 @author: zxwtry @email: zxwtry@qq.com @date: 2017年2月12日 @details: Solution1: AC 102ms 71.02% @details: Solution1: AC 192ms 17.26%'''class Solution1(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if s == None or len(s) == 0: return 0 d,m,u = {},1,0 for i in range(len(s)): p = d.get(s[i]) d[s[i]] = i if p == None: p = -1 elif p >= u: m = max(m, i - u) u = p + 1 m = max(m, len(s) - u) return mclass Solution2(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ if s == None or len(s) == 0: return 0 d, m , u = [-1] * 256, -1, -1 for j in range(len(s)): p = d[ord(s[j])] d[ord(s[j])] = j m = max(m, j - u) u = max(u, p + 1) m = max(m, len(s) - u) return m if __name__ == "__main__": d = "abba" s = Solution2() print(s.lengthOfLongestSubstring(d))
0 0
- leetcode003:Longest Substring Without Repeating Characters
- Longest Substring Without Repeating Characters - leetcode003
- leetcode003 Longest Substring Without Repeating Characters
- Leetcode003-Longest Substring Without Repeating Characters
- LeetCode003 Longest Substring Without Repeating Characters
- Leetcode003. Longest Substring Without Repeating Characters
- Leetcode003 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
- 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
- Struts几种数据类型的xml数据校验
- 解决mybatis问题:The content of element type "mapper" must match "(cache-ref|cache|resultMap*|parameterMa
- 通讯录第四版
- 【Java学习】之 线程(Thread)
- 套接字
- LeetCode003 Longest Substring Without Repeating Characters
- 黑马学习笔记--常用API
- L2-014. 列车调度
- 卷积神经网络
- 使用存储过程在Oracle中来封装分页
- 【HDU 1027 Ignatius and the Princess II】+ 排列组合
- windows里tasklist命令
- 问题 F: Vegetable and Park
- Spark性能优化指南——高级篇