博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
219. Contains Duplicate II
阅读量:7114 次
发布时间:2019-06-28

本文共 1005 字,大约阅读时间需要 3 分钟。

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.

Example 1:

Input: nums = [1,2,3,1], k = 3Output: true

Example 2:

Input: nums = [1,0,1,1], k = 1Output: true

Example 3:

Input: nums = [1,2,3,1,2,3], k = 2Output: false

难度:easy

题目:给定一整数数组和一整数K,找出是否存在两个不同的下标使用得nums[i]=nums[j]并且i与j之差的绝对值小于等于k.

思路:hashmap

Runtime: 9 ms, faster than 81.45% of Java online submissions for Contains Duplicate II.

Memory Usage: 41 MB, less than 77.21% of Java online submissions for Contains Duplicate II.

public class Solution {    public boolean containsNearbyDuplicate(int[] nums, int k) {        Map
mii = new HashMap<>(); for (int i = 0; i < nums.length; i++) { if (mii.containsKey(nums[i]) && i - mii.get(nums[i]) <= k) { return true; } mii.put(nums[i], i); } return false; }}

转载地址:http://xbwel.baihongyu.com/

你可能感兴趣的文章
MS ASP.Net Ajax 服务端扩展
查看>>
android102 查询,插入联系人
查看>>
数据库邮件
查看>>
adstrtal.sh报超时错误 ERROR : Timed out( 100000 ): Interrupted Exception
查看>>
一个前端工程师的基本修养
查看>>
ZT:三十个好习惯
查看>>
.Net开发笔记(七)使用组件编程
查看>>
ASP.NET企业开发框架IsLine FrameWork系列之八--AppLogProvider日志框架(下)
查看>>
DataBase异常状态:Recovery Pending,Suspect,估计Recovery的剩余时间
查看>>
一个android版本的rss阅读器--明天补充实现过程,先上图
查看>>
WPF TreeView
查看>>
HTML: 仿写一个财经类静态的网页
查看>>
C#读写config配置文件
查看>>
JavaScript:文本域事件处理
查看>>
关于dctser进程
查看>>
一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇-演示ORM中的查询
查看>>
win7远程登录
查看>>
5.6. DHCP
查看>>
RDIFramework.NET ━ .NET快速信息化系统开发框架 V3.2->Web版本模块管理界面新增模块排序功能...
查看>>
ajax与算法,sql的group处理
查看>>