首页 > 其他 > 详细

LeetCode | Longest Substring Without Repeating Characters

时间:2014-04-08 19:51:49      阅读:471      评论:0      收藏:0      [点我收藏+]

题目

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!