219. Contains Duplicate II
来源:互联网 发布:淘宝怎样推广宝贝 编辑:程序博客网 时间:2024/05/19 16:07
219 contains duplicate ii
题目
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
翻译
给定整数数组和整数k,找出数组中是否有两个不同的索引i和j,使得nums [i] = nums [j],i和j之间的绝对差值最多为k。
解题思路
该题的难度在于如果暴力搜索会存在超时问题,经过分析暴力搜索时间在n2,故而使用map来进行解决,把时间复杂度降低到了nlogn,map的实现结构为平衡二叉树,复习了一下二叉搜索树和平衡二叉树,理解了map为什么搜索时间复杂度在logn
public static boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer,Integer> map=new HashMap<Integer,Integer>(); for(int i=0;i<nums.length;i++){ if(map.containsKey(nums[i])){ int j=map.get(nums[i]); if(i - j <= k) return true; map.put(nums[i], i); }else{ map.put(nums[i], i); } } return false;}
欢迎加入中科院开源软件自习室:631696396
阅读全文
0 0
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 219. Contains Duplicate II
- 集合Map类的详解--Java基础082
- 【搜狐】根据成绩计 算出至少需要发多少奖金才能让所有的组满意
- Android GPS应用开发
- C++第二课----编程引用与空间开辟malloc和free
- Java闰年的判断 两种方法
- 219. Contains Duplicate II
- 在JAVA servlet中返回不同类型的AJAX response
- 安装tensorflow时候出了一个坑,特此记录
- 安卓T9九键输入法界面
- 基于Java RMI和Swing的三子棋游戏
- java 字符 字节 与 编码
- Java判断数字为几位数的两种方法
- Java生肖年的判断
- jupyter