码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode——Longest Substring Without Repeating Characters

时间:2015-07-13 22:01:03      阅读:106      评论: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.

给定一个字符串,找出最长的无反复子串。

	public static int lengthOfLongestSubstring(String s) {
		if (s == null || s.length() == 0)
			return 0;
		int maxlen = 1;
		boolean[] flag = new boolean[256];
		int start = 0;

		for (int i = 0; i < s.length(); i++) {
			char ch = s.charAt(i);
			if (flag[ch]) {
				maxlen = Math.max(maxlen, i - start);
				for (int j = start; j < i; j++) {
					if (s.charAt(j) == ch) {
						start = j + 1;
						break;
					}
					flag[s.charAt(j)] = false;
				}
			} else
				flag[ch] = true;
		}
		return Math.max(maxlen, s.length() - start);
	}


版权声明:本文博客原创文章,博客,未经同意,不得转载。

LeetCode——Longest Substring Without Repeating Characters

标签:

原文地址:http://www.cnblogs.com/yxwkf/p/4643811.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!