题解 | #最长无重复子数组#

最长无重复子数组

http://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4

pl指针指向找到相同的字母的下一位
例如:abcba
pl:1 5

import java.util.*;


public class Solution {
    /**
     * 
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public int maxLength (int[] arr) {
        // write code here
        HashSet set = new HashSet<Integer>();
        int pl = 0;                       
        int re = 0;                       
        for(int i = 0;i<arr.length;i++) {
            if (!set.contains(arr[i]))set.add(arr[i]);
            else {
                re = re>set.size()?re:set.size();
                for(;pl<i;pl++)
                {
                    set.remove(arr[pl]);
                    if (arr[pl] == arr[i])break;
                }
                set.add(arr[i]);
                pl++;
            }
        }
        return re>set.size()?re:set.size();
    }
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务