Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目:
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,找出是否存在下标i,j使得nums[i] = nums[j],同时i,j的差值小于等于k。
分析:
遍历数组,使用map存储每个值最近一次出现的下标。
代码:
public class Solution {public boolean containsNearbyDuplicate(int[] nums, int k) {Map<Integer,Integer> map=new HashMap<>();for(int i=0;i<nums.length;i++){Integer value=map.get(nums[i]);if(value!=null&&i-value<=k){return true;}else{map.put(nums[i],i);}}return false;}
}
Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]相关推荐
- Leet Code OJ 14. Longest Common Prefix [Difficulty: Easy]
题目: Write a function to find the longest common prefix string amongst an array of strings. 翻译: 写一个函数 ...
- Leet Code OJ 118. Pascal's Triangle [Difficulty: Easy]
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- Leet Code OJ 88. Merge Sorted Array [Difficulty: Easy]
题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...
- Leet Code OJ 110. Balanced Binary Tree [Difficulty: Easy]
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- Leet Code OJ 326. Power of Three [Difficulty: Easy]
题目: Given an integer, write a function to determine if it is a power of three. Follow up: Could you ...
- Leet Code OJ 172. Factorial Trailing Zeroes [Difficulty: Easy]
题目: Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in ...
- Leet Code OJ 206. Reverse Linked List [Difficulty: Easy]
题目: Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursiv ...
- Leet Code OJ 38. Count and Say [Difficulty: Easy]
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 11 ...
- Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 思路分析: 题意是将二叉树所有左右子数 ...
最新文章
- 数值计算领域的“圣经”,图灵出了新版本 | 11月书讯
- “面试不败计划”:各大公司Java后端开发面试题总结
- Python教程:跳出多层循环for、while
- python CMAKE的使用
- spark 应用程序性能优化:12 个优化方法
- app把信息添加到mysql_如何将数据从iphone app上传到mysql数据库
- warning C4996: 'fopen': This function or variable may be unsafe.(_CRT_SECURE_NO_WARNINGS)
- Python实现图像直方图均衡化算法
- 防火墙配置文件iptables详解
- VC++ 只运行一个程序实例
- 项目管理10大模板Excel版(可直接套用)
- C++ 获取鼠标点击位置
- apipost自动化测试工具
- 关于DBC文件的格式解析(转)
- c语言交通处罚单系统主函数,C语言课程设计交通处罚单管理系统.doc
- C# 中取绝对值的函数
- TQ2440内核linux2.6.28移植
- github官网进不去解决方案
- Java实现mds降维_降维算法MDS
- 安装Office365后版本信息显示为Office2016