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

最小的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;
    }
}
全部评论

相关推荐

刘湘_passion:太强了牛肉哥有被激励到
点赞 评论 收藏
分享
在debug的柠檬精很迷人:好消息:现在HR挑三拣四 15年后 HR跪着求要简历 坏消息:被挑的是这代人,到时候求人的也是这代人。真好。
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务