首页 > 其他 > 详细

Longest Substring Without Repeating Characters

时间:2015-05-18 02:05:05      阅读:165      评论: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 class Solution {
    public int lengthOfLongestSubstring(String s) {
    	if (s.length() == 0) {
    		return 0;
    	}
        int arr[] = new int[256];
        Arrays.fill(arr, -1);
        int slow = 0;
        int fast = 1;
        int max = 1;
        arr[s.charAt(0)] = 0;
        while (fast < s.length()) {
        	if (arr[s.charAt(fast)] >= slow) {
        		slow = arr[s.charAt(fast)]+1;
        	} 
        	max = Math.max(max, fast-slow+1);
        	arr[s.charAt(fast)] = fast;
        	fast++;
        }
        return max;
    }
}

?

Longest Substring Without Repeating Characters

原文:http://hcx2013.iteye.com/blog/2211934

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