LeetCode Array Easy 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.
Example 1:
Input: nums = [,,,], k =
Output: trueExample 2:
Input: nums = [,,,], k =
Output: trueExample 3:
Input: nums = [,,,,,], k =
Output: false
问题描述:给定一个数组和一个整数k 判断是否存在两个重复元素i,j 使i和j的差的绝对值不大于k 如果存在 返回true 否则返回false
思路,这里使用字典Dictionary保存元素和元素的索引。如果能找到元素,则计算当前元素和上一个元素的索引的差是否不大于k
public bool ContainsNearbyDuplicate(int[] nums, int k) {
Dictionary<int,int> dic = new Dictionary<int,int>();
for(int i = ; i < nums.Length; i++){
if(dic.ContainsKey(nums[i])){
if(i - dic[nums[i]]<=k)
return true;
}
dic[nums[i]] = i;
}
return false;
}

LeetCode Array Easy 219. Contains Duplicate II的更多相关文章
- LeetCode Array Easy 217. Contains Duplicate
Description Given an array of integers, find if the array contains any duplicates. Your function sho ...
- LeetCode Array Easy 167. Two Sum II - Input array is sorted
Description Given an array of integers that is already sorted in ascending order, find two numbers s ...
- 【leetcode❤python】 219. Contains Duplicate II
#-*- coding: UTF-8 -*-#遍历所有元素,将元素值当做键.元素下标当做值#存放在一个字典中.遍历的时候,#如果发现重复元素,则比较其下标的差值是否小于k,#如果小于则可直接返回Tru ...
- 219. Contains Duplicate II【easy】
219. Contains Duplicate II[easy] Given an array of integers and an integer k, find out whether there ...
- 【LeetCode】217 & 219 - Contains Duplicate & Contains Duplicate II
217 - Contains Duplicate Given an array of integers, find if the array contains any duplicates. You ...
- 219. Contains Duplicate II - LeetCode
Question 219. Contains Duplicate II Solution 题目大意:数组中两个相同元素的坐标之差小于给定的k,返回true,否则返回false 思路:用一个map记录每 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- (easy)LeetCode 219.Contains Duplicate II
Given an array of integers and an integer k, find out whether there there are two distinct indices i ...
- [LeetCode] 219. Contains Duplicate II 包含重复元素 II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
随机推荐
- 脚本_检测 MySQL 数据库连接数量
#!bin/bash#功能:检测 MySQL数据库连接数量,以满足对 MySQL 数据库的监控需求,查看 MySQL 连接是否正常.#作者:liusingbon#本脚本每 2 秒检测一次 MySQL ...
- java 字符串常量池
- chrome插件研发手册
chrome插件研发手册 一:需求前景 对于研发的小伙伴来说,总会遇到这样的需求,想要通过代码操作已有网站的行为动作,如:自动填充表格内容(表单内容太多,想一键将表单内容填充):自动登录网站(网站登录 ...
- canvas 星星闪烁的效果
代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- JavaScript判断对象是否相等
实现一. var obj = {a:'a'},obj1 = {b:'b'},obj2 = {a:'a'};就是使用JSON.stringify()先把对象转化成字符串,这样就可以啦 console.l ...
- BZOJ5261 Rhyme
传送门 广义后缀自动机= =+ 跟ptx大爷的博客学的 戳我传送 我写的第一种 建立Trie树的写法 bfs建立SAM 为什么是bfs呢 我也不知道(GG) 经过我一番抱大腿+询问 各位大爷说的原因是 ...
- SpringIntegration---MongDB
1.依赖 <dependency> <groupId>org.springframework.integration</groupId> <artifactI ...
- IntelliJ IDEA 代码调式
Mute Breakpoints: 保留所有断点,但是不执行(程序一次性执行). Condition: 条件触发 即当执行到i的值变为5的时候,在断点处暂停.
- C++ 递推法 斐波那契数列 兔子产仔
#include "stdio.h" #include "iostream" int Fibonacci(int n) { int t1, t2; || n = ...
- Linux进程管理工具vmstat,iostat,pmap
一查看内存的工具——vmstat (一)vmstat的介绍 vmstat vmstat是Virtual Memory Statistics(虚拟内存统计)的缩写 利用vmstat命令可以对操作系统的报 ...