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

最长无重复子数组

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

/**

  • @Author: Mr.huang

  • @Date: 2021/08/10/9:32

  • @Description: NC41 最长无重复子数组 利用双指针做窗口,利用哈希去重

  • /
    public class NC41 {
    public static void main(String[] args) {

      int [] array={1,1,2,2,5};
      int maxLength = getMaxLength(array);
      System.out.println(maxLength);

    }

    public static int getMaxLength(int[] arr){

      int maxLength=0;
      int left=0;
      int right=0;
      Map<Integer,Integer> map=new HashMap<>();
      if (arr==null||arr.length==0){
          return maxLength;
      }
      while (right<arr.length){
          map.put(arr[right], map.getOrDefault(arr[right],0)+1);
          int numInMap= arr[right];
          right++;
          while (map.get(numInMap)>1){
              int numOutMap=arr[left];
              left++;
              map.put(numOutMap,map.getOrDefault(numOutMap,0)-1);
          }
    
          maxLength=Math.max(maxLength,right-left);
      }
      return maxLength;

    }

全部评论

相关推荐

09-17 19:25
已编辑
太原理工大学 游戏测试
叁六玖:公司名发我,我要这个HR带我打瓦
我的秋招日记
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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