(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 and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
方法1:暴力解法
代码如下:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
int len=nums.length;
for(int i=0;i<len;i++){
for(int tmp=1;tmp<=k && i+tmp<len;tmp++)
if(nums[i]==nums[i+tmp])
return true;
}
return false;
}
}
运行超时:

方法2:分析:
代码如下:
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<Integer>();
int start = 0, end = 0;
for(int i = 0; i < nums.length; i++){
if(!set.contains(nums[i])){
set.add(nums[i]);
end++;
} else
return true;
if(end - start > k) {
set.remove(nums[start]);
start++;
}
}
return false;
}
}
运行结果:
(easy)LeetCode 219.Contains Duplicate II的更多相关文章
- [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 ...
- [LeetCode] 219. Contains Duplicate II ☆(存在重复元素2)
每天一算:Contains Duplicate II 描述 给出1个整形数组nums和1个整数k,是否存在索引i和j,使得nums[i] == nums[j] 且i和j之间的差不超过k Example ...
- LeetCode 219. Contains Duplicate II (包含重复项之二)
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
- LeetCode 219 Contains Duplicate II
Problem: Given an array of integers and an integer k, find out whether there are two distinct indice ...
- Java for 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 STL
找出是否存在nums[i]==nums[j],使得 j - i <=k 这是map的一个应用 class Solution { public: bool containsNearbyDuplic ...
- Java [Leetcode 219]Contains Duplicate II
题目描述: Given an array of integers and an integer k, find out whether there are two distinct indices i ...
- C#解leetcode 219. Contains Duplicate II
该题用到了.NET 3.5在System.Collections.Generic命名空间中包含一个新的集合类:HashSet<T>的Add()方法,详细信息请看转载:C# HashSet ...
- [LeetCode] 219. Contains Duplicate II 解题思路
Given an array of integers and an integer k, find out whether there are two distinct indices i and j ...
随机推荐
- centos6.5 安装iptables
阿里云默认是没有安装iptables 安装 yum install -t iptables yum install iptables-services 检查iptables服务的状态 service ...
- jquery的$.extend和$.fn.extend作用及区别
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); (1)类级别 类级别你可以理解为拓展jquery类,最明显的例子是$.ajax ...
- mapreduce精简概括--转
mapreduce精简概括 We want to count all the books in the library. You count up shelf #1, I count up shelf ...
- Ruby Numeric
Numeric |-- Float |-- Integer |-- Fixnum |-- Bignum Numeric的基本结构 整数的差异,一般的数字Fixnum就能够处理,即使超过了Fixnum的 ...
- shopex后台上传模板漏洞
看到有人找这个拿SHELL的方法.就本地搭建试了下.很简单的. 首先是WIN下.需要WIN主机IIS解析漏洞. 进入后台.点页面管理.点模板列表.默认模板是紫气东来(ShopEx4.8).点编辑模板. ...
- [Hibernate] - many to many
Hibernate的多对多实现: hibernate.cfg.xml <?xml version="1.0" encoding="UTF-8"?> ...
- 【shell】if语句
单分支: #!/bin/bash rate=$(df -h|grep vg_andon-lv_root |awk '{print $5}'| cut -d "%" -f1) #ec ...
- RESRful API 和 HTTP状态码
一.RESRful API: GET(SELECT):从服务器取出资源(一项或多项). POST(CREATE):在服务器新建一个资源. PUT(UPDATE):在服务器更新资源(客户端提供改变后的完 ...
- AppCan做的图片上传代码
存在AppCan里的网页 index.html <!DOCTYPE html> <html class="um landscape min-width-240px min- ...
- sublime_text3 用户配置
{ "auto_complete_triggers": [ { "characters": "", "selector" ...