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

最小的K个数

http://www.nowcoder.com/practice/6a296eb82cf844ca8539b57c23e6e9bf

import java.util.*;


public class Solution {
    /**
     * 
     * @param arr int整型一维数组 the array
     * @return int整型
     */
    public int maxLength(int[] arr) {
        if (arr.length == 0)
            return 0;
        //存的是数字  数字最后一次出现的位置
        HashMap<Integer, Integer> map = new HashMap<>();
        int max = 0;
        for (int i = 0, j = 0; i < arr.length; ++i) {
            //如果当前位置在map里面  
            if (map.containsKey(arr[i])) {
                //j代表的就是左边的界限  
                j = Math.max(j, map.get(arr[i]) + 1);
            }
            //如果不存在添加   如果存在 存的就是数字最后的下标位置了
            map.put(arr[i], i);
            //更新最长无重复子数组
            max = Math.max(max, i - j + 1);
        }
        return max;
    }
}
全部评论

相关推荐

躺尸修仙中:因为很多92的也去卷中小厂,反正投递简历不要钱,面试不要钱,时间冲突就推,不冲突就面试积累经验
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务