Leetcode:Longest Substring Without Repeating Characters 解题报告
2014-12-27 18:41
507 查看
Longest Substring Without Repeating Characters
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.public int lengthOfLongestSubstring(String s) { if (s == null) { return 0; } int max = 0; // suppose there are only ASCII code. int[] lastIndex = new int[128]; for (int i = 0; i < 128; i++) { lastIndex[i] = -1; } int len = s.length(); int l = 0; for (int r = 0; r < len; r++) { char c = s.charAt(r); if (lastIndex[c] >= l) { l = lastIndex[c] + 1; } // replace the last index of the character c. lastIndex[c] = r; // replace the max value. max = Math.max(max, r - l + 1); } return max; }
View Code
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/string/LengthOfLongestSubstring.java
相关文章推荐
- [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 OJ 之 Longest Substring Without Repeating Characters 解题报告
- leetcode解题方案--003--LongestSubstringWithoutRepeatingCharacters
- leetcode解题报告3. Longest Substring Without Repeating Characters
- leetcode Longest Substring Without Repeating Characters解题记录
- Longest Substring Without Repeating Characters解题报告
- LeetCode解题-#3-Longest Substring Without Repeating Characters
- [Leetcode]解题文档-Longest Substring Without Repeating Characters
- LeetCode——Longest Substring Without Repeating Characters
- Leetcode-3 Longest Substring Without Repeating Characters 最长无重复字节字符串
- Leetcode03---Longest Substring Without Repeating Characters
- LeetCode 3 Longest Substring Without Repeating Characters
- LeetCode--Longest Substring Without Repeating Characters
- 【LeetCode】Longest Substring Without Repeating Characters
- leetcode---Longest Substring Without Repeating Characters