Duplicate问题
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.
我的方法:HashMap,key:每一个不同的nums[i]值, value:这个nums[i]值所对应的所有index的一个list
但是慢在先都装到map里,再对每个key做判断。
class Solution {public boolean containsNearbyDuplicate(int[] nums, int k) {if(nums == null || nums.length == 0){return false;} if(nums.length == 1){return false; }Map<Integer, List<Integer>> map = new HashMap<>(); for(int i=0; i<nums.length; i++){List<Integer> list;if(!map.containsKey(nums[i])){list= new ArrayList<>(); }else{list = map.get(nums[i]); }list.add(i);map.put(nums[i], list); }boolean result = false;for(Integer key : map.keySet()){List<Integer> valList = map.get(key);if(valList.size() != 1){int smallestDiff = getSmalleastIndexDiff(valList);if(smallestDiff <= k){result = true;break;}} }return result;}private int getSmalleastIndexDiff(List<Integer> valList){int result = Integer.MAX_VALUE;for(int i=0; i<valList.size()-1; i++){int diff = Math.abs(valList.get(i) - valList.get(i+1));result = Math.min(result, diff); }return result; }
}
这个是我在leetcode讨论区看到别人的方法,一边装map的过程就可以判断,
HashMap,key:每一个不同的nums[i]值, value:这个nums[i]值所对应的最近的index
class Solution {public boolean containsNearbyDuplicate(int[] nums, int k) { if(nums == null || nums.length == 0){return false;} Map<Integer, Integer> map = new HashMap<>(); for(int i=0; i<nums.length; i++) {if(map.containsKey(nums[i])) {if(Math.abs(i-map.get(nums[i])) <= k){return true;} else{map.put(nums[i], i); //这边需要注意}} else {map.put(nums[i], i);}}return false;}
}
Duplicate问题相关推荐
- LeetCode刷题记录2——217. Contains Duplicate(easy)
LeetCode刷题记录2--217. Contains Duplicate(easy) 目录 LeetCode刷题记录2--217. Contains Duplicate(easy) 题目 语言 思 ...
- Duplicate entry '0' for key 'PRIMARY'_Spring Batch
2019独角兽企业重金招聘Python工程师标准>>> Duplicate entry '0' for key 'PRIMARY'_Spring Batch 在运行一个job成功后, ...
- pandas使用duplicated函数删除dataframe中重复列名称的数据列、默认保留重复数据列中的第一个数据列(removing duplicate columns in dataframe)
pandas使用duplicated函数删除dataframe中重复列名称的数据列.默认保留重复数据列中的第一个数据列(removing duplicate columns in dataframe) ...
- mysql load data on duplicate_带有ON DUPLICATE KEY UPDATE的MySQL LOAD DATA INFILE
慕勒3428872 这些步骤可用于模拟此功能:1)创建一个新的临时表.CREATE TEMPORARY TABLE temporary_table LIKE target_table;2)(可选)从临 ...
- LeetCode 316. Remove Duplicate Letters--贪心--Java,C++,Python解法
题目地址:Number of Longest Increasing Subsequence - LeetCode 做这道题目前建议先做:Longest Increasing Subsequence - ...
- mysql批量insert bug_MySQL Bug insert into on duplicate key update 语法更新 text blob 大字段导致 MySQL crash...
1. 背景 业务执行 SQL 导致 MySQL 进程 Crash,做故障切换后,新的主库又 Crash 了.查看 MySQL 错误日志,发现多次 Crash 时的堆栈相同,如下: Thread poi ...
- RMAN duplicate database到新主机
万变不离其宗 duplicate database 也是一个备份与恢复的过程.网上有 RAMN用于迁移数据库也是这个道理.duplicate database有一个优点,它为duplicated da ...
- [LeetCode]Contains Duplicate III
Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...
- AndroidStudio使用第三方jar包报错(Error: duplicate files during packaging of APK)
http://www.kwstu.com/ArticleView/android_201410252131196692 错误描述: Error: duplicate files during pack ...
- swagger error: Conflicting schemaIds: Duplicate schemaIds detected for types A and B
使用Web API并使用swashbuckle生成swagger文档,我在两个不同的命名空间中定义了两个具有相同名称的不同类.当我在浏览器中打开swagger页面时,它说: 1 Conflicting ...
最新文章
- windows下如何在一台机器上安装两个MYSQL数据库
- 初涉SQL Server性能问题(1/4):服务器概况
- HTTP学习笔记(1)
- 用立异,声东击西——贵阳Flex创客团队航行器吸引世界眼光
- java axis2 教程_webservice的Axis2入门教程java版
- 机器学习-逻辑回归-信用卡检测任务
- Tomcat绑定jre
- ASP.NET MVC3+EF4+Oracle入门实例(一)
- Scriptcase Crack,PHP Web 开发工具
- Python爬取电影天堂指定电视剧或者电影
- animate发布html5教程,Adobe Animate CC 中的 HTML 发布模板 - Adobe Animate 用户指南
- Verilog结构描述
- C语言+深度搜索的的方法解决全排列问题
- 水下传感器网络研究——第5章 路由协议
- 百度网盘ubuntu版deb包安装
- 【运放学习】:虚短、虚短、虚地、深度负反馈
- 【朝花夕拾】Android自定义View篇之(十一)View的滑动,弹性滑动与自定义PagerView...
- 对List进行多维度排序
- Oracle 20c 不再支持特性:传统审计不支持 统一审计(Unified Auditing)成主流
- 八字计算方法 php,php计算四柱(生辰八字)的方法