- 浏览: 183416 次
- 性别:
- 来自: 济南
文章分类
最新评论
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 difference between i and j is at most k.
给定一个数组判断数组中是否存在两个相同的数,它们之间的差最大为k。我们借助HashMap, key中存放数组的元素nums[i],value中存放对应的下标i。如果遇到相同的key,就对比当前元素的下标i与key中value的值与k的大小,如果i - get(key) 小于等于k就说明存在,返回true。代码入下:
给定一个数组判断数组中是否存在两个相同的数,它们之间的差最大为k。我们借助HashMap, key中存放数组的元素nums[i],value中存放对应的下标i。如果遇到相同的key,就对比当前元素的下标i与key中value的值与k的大小,如果i - get(key) 小于等于k就说明存在,返回true。代码入下:
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { if(nums == null || nums.length < 2) return false; HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>(); for(int i = 0; i < nums.length; i++) { if(hm.containsKey(nums[i])) { if(i - hm.get(nums[i]) <= k) return true; } hm.put(nums[i], i); } return false; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 929Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 672Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
java入门 java_leetcode题解之219_Contains_Duplicate_II
python python_leetcode题解之219_Contains_Duplicate_II.py
加油站 leetcode leetcode_java prepare for jobhunting. java version of Leetcode. easy 55 about ...II ...4.Contains Duplicate II 5.Contains Duplicate 07/02/2015 1.Valid Parentheses 2.Maximum
python python_leetcode题解之217_Contains_Duplicate.py
《手稿_V1.111》探讨了一个在C++编程环境下解决LeetCode中的数据结构问题,具体题目是“Contains Duplicate II”。这个问题涉及到数组、滑动窗口和哈希映射等概念,要求在给定整数数组`nums`和一个整数`k`的情况下,...
python python_leetcode题解之220_Contains_Duplicate_III.py
题目描述的是一个典型的编程问题,来源于LeetCode上的"Contains Duplicate II",要求在给定的整数数组`nums`中找到两个不同索引`i`和`j`,使得`nums[i] = nums[j]`,且`|i - j| 。这里`k`是给定的整数,表示索引的...
在给定的代码片段中,我们讨论了一个名为“手稿_V1.22”的文件,该文件涉及到在C++中解决LeetCode上的一个问题——“Contains Duplicate II”(问题编号219)。这个问题要求我们判断一个整数数组`nums`中是否存在两...
462 | [Minimum Moves to Equal Array Elements II](https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/) | [C++](./C++/minimum-moves-to-equal-array-elements-ii.cpp) [Python](./Python/...
信息熵,是随机变量或整个系统的不确定性。熵越大,随机变量或系统的不确定性就越大。 相对熵,用来衡量两个取值为正的函数或概率分布之间的差异。 交叉熵,用来衡量在给定的真实分布下,使用非真实分布所指定的策略...
duplicate(滑动窗口), 454. 4sum 数组 leetcode: 基本排序算法 插入, 归并 -> 递归 和 迭代 快排 -> 随机 三路 leetcode75 数组遍历的为了减少时间复杂的的方法 对撞指针 leetcode: 167 two sum, 125回文串, 344 ...
BETA-家庭助理社区附加组件 警告! 这是一个测试版存储库 此Home Assistant加载项存储库包含加载项的beta版本。 他们可能会随时停止工作。 它们可能会对您的系统产生负面影响。 该存储库是为以下目的创建的: ...
java lru leetcode leetcode-java leetcode刷题笔记 已做题目列表 1.Two Sum 3.Longest Substring Without Repeating Characters ...II ...II ...217.Contains Duplicate 263.Ugly Number 264.Ugly Number II
II #136 Single Number #150 Evaluate Reverse Polish Notation #169 Majority Element #171 Excel Sheet Column Number #217 Contains Duplicate #226 Invert Binary Tree #237 Delete Node in a Linked List #238 ...
leetcode 浇花力扣解决方案 简单的 #0001 - Two Sum #0007 - Reverse Integer ...Contains Duplicate #0242 - Valid Anagram #0243 - Shortest Word Distance #0246 - Strobogrammatic Number #0263 -
Contains Duplicate (E) 48. Rotate Image (M) -> 2 73. Set Matrix Zeroes (M) 1. Two Sum (E) 167. Two Sum II - Input array is sorted (E) 653. Two Sum IV - Input is a BST (E) -> 2 26. Remove Duplicates ...
this suite contains utilities to clean registry, temporary files on your disks, erase your application and internet browser history, cache and cookies. you can control startup programs that load ...
0603rl - duplicate 0805-100V 0805CL 0805cl - duplicate 0805RL 1206 1206CL 1206RL 1210 1812 2512 3225[1210] 6332[2512] BR_RS25XX C822j823聚丙烯 CAP3 CAP11-8 CAP22.5*10 CD1 CD1B CD1S CD2 CD2.5-6.3 CY1 ...
“contains_duplicate.py” - 检查整数列表是否包含任何重复项的快速算法。 “contains_nearby_duplicate.py” - 检查整数列表是否包含距离 k 内的任何重复项的快速算法。 "coin_arrangement.py" - 给定 n,算法找到...