是否包含重复元素2
来源:互联网 发布:薛之谦女装品牌淘宝店 编辑:程序博客网 时间:2024/06/03 17:42
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 difference between i and j is at most k.
大概意思就是一个整数数组,和一个整数k,数组中有两个下标不同 i 和j ,但是值却相同nums[i]=nums[j];i 和 j 之间的差小于等于k。
如果存在这样的两个元素返回true。如果不存在返回false
public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < nums.length; i++) {map.put(nums[i], i);if (map.size() < i+1) {//map集合元素个数小于存入的个数,说明有重复值for (int j = 0; j < i; j++) {//遍历重复值之前的数组,找到下标if (nums[j] == nums[i]) {if (i-j <= k) {//比较两个相同元素的下标与k的大小关系return true;}}}} } return false; }}
0 0
- 是否包含重复元素2
- 是否包含重复元素
- Contains Duplicate 数组中是否包含重复元素
- Contains Duplicate III 是否包含重复元素系列3
- 219. Contains Duplicate II 包含重复元素2
- 判断数组是否包含元素
- 判断数组元素是否重复
- 判断数组元素是否重复
- SDUT 离散题目2 判断一个集合元素是否重复
- 包含重复元素的全排列
- 217. Contains Duplicate 包含重复元素
- 判断数组中是否包含重复数字
- 查询是否包含字段重复的数据
- 如何判断字符串是否包含重复字符
- 判断数组中是否包含重复数字
- 检验字符串中是否包含重复字符
- Contains Duplicate II 是否包含重复
- 测试字符串中是否包含重复字符
- 编译lua.c:80:31: fatal error: readline/readline.h: 没有那个文件或目录
- AFNetworking 网络错误提示data转换字符串
- vba
- button设置可以变换的背景及其容易遇到的问题解决方法
- 51nod 正整数分组
- 是否包含重复元素2
- 【易语言界面开发系列教程之(EX_UI使用系列教程 ——1-8节)】
- 二叉搜索树的下一个结点
- Could not get BatchedBridge, make sure your bundle is packaged correctly
- 【JZOJ4710】Value
- 算术表达式的语法分析及语义分析程序设计 —— LR分析法、输出三元式(续)
- B-Tree 、B+树、B*树
- 集群环境下的图片服务器搭建
- 基于opencv的物体定位