题目
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.
分析
在遍历过程中,记录各个字符最后出现位置,如果子串出现重复字符,就移动子串的起始位置。
代码
import java.util.Arrays; public class LongestSubstringWithoutRepeatingCharacters { public int lengthOfLongestSubstring(String s) { int max = 0; int start = 0; int end = 0; int[] loc = new int[255]; Arrays.fill(loc, -1); while (end < s.length()) { int c = s.charAt(end); start = loc[c] >= start ? loc[c] + 1 : start; loc[c] = end; max = Math.max(max, ++end - start); } return max; } }
LeetCode | Longest Substring Without Repeating Characters,布布扣,bubuko.com
LeetCode | Longest Substring Without Repeating Characters
原文:http://blog.csdn.net/perfect8886/article/details/23184013