Contains Duplicate I & III
Contains Duplicate I
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
public boolean containsDuplicate(int[] nums) {
Set<Integer> distinct = new HashSet<>();
for(int num : nums) {
if(distinct.contains(num)) {
return true;
}
distinct.add(num);
}
return false;
}
Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] andnums[j] is at most t and the difference between i and j is at most k.
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null||nums.length<||k<||t<)
return false;
TreeSet<Long> set = new TreeSet<Long>();
for(int i=; i<nums.length; i++){
long curr = (long) nums[i];
long leftBoundary = (long) curr-t;
long rightBoundary = (long) curr+t+; //right boundary is exclusive, so +1
SortedSet<Long> sub = set.subSet(leftBoundary, rightBoundary);
if(sub.size()>)
return true;
set.add(curr);
if(i>=k){ // or if(set.size()>=k+1)
set.remove((long)nums[i-k]);
}
}
return false;
}
}
Contains Duplicate I & III的更多相关文章
- [LeetCode] Contains Duplicate(II,III)
Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your funct ...
- Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- Contains Duplicate,Contains Duplicate II,Contains Duplicate III
217. Contains Duplicate Given an array of integers, find if the array contains any duplicates. Your ...
- Contains Duplicate III -leetcode
Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 220. Contains Duplicate III
题目: Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- [LeetCode] 220. Contains Duplicate III 包含重复元素 III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- [Leetcode] Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
随机推荐
- MySQL数据库服务器整体规划(思路与步骤)
MySQL数据库服务器整体规划(思路与步骤) 参考资料: http://blog.51cto.com/zhilight/1630611 我们在搭建MySQL数据库服务器的开始阶段就合理的规划,可以避免 ...
- Storm常用的类
BaseRichSpout (消息生产者)BaseBasicBolt (消息处理者)TopologyBuilder (拓扑的构建器)Values (将数据存放到values ,发送到下个组件)Tupl ...
- 图论分支-Tarjan初步-割点和割边
所谓割点(顶)割边,我们引进一个概念 割点:删掉它之后(删掉所有跟它相连的边),图必然会分裂成两个或两个以上的子图. 割边(桥):删掉一条边后,图必然会分裂成两个或两个以上的子图,又称桥. 这样大家就 ...
- C#设计模式(5)——建造者模式
1.建造者模式介绍 在软件开发中,有时我们要创建一个复杂的对象,这个对象由几个子部件按一定的步骤组合而成,这时候我们就可以使用建造者模式了.说到建造者我们首先想到的是盖房子,盖房子简单的说有三个步骤: ...
- 谈谈关于PHP连接数据库的两种方法(PDO&Mysqli)
前言:在我们之前学习sql语句的时候都是停留在黑窗口的,怎样才能让mysql与程序代码发生联系呢?此时PDO和Mysqli应运而生,为了解决这个问题 (一)开启其中(pdo或者mysqli)的php扩 ...
- Emacs 快速指南(中文翻译)
Emacs 快速指南 目录 1. 小结(SUMMARY) 2. 基本的光标控制(BASIC CURSOR CONTROL) 3. 如果 EMACS 失去响应(IF EMACS STOPS RESP ...
- ` ` ` ` ` ` ` `
字符实体 说明 这是我们使用最多的空格,也就是按下space键产生的空格.在HTML中,如果你用空格键产生此空格,空格是不会累加的(只算1个).要使用html实体表示才可累加. 占据的宽度正好 ...
- Java实现OPC通信
1.PLC和OPC 使用的PLC:西门子的S7 300,具体型号如下图 使用的OPC server软件: 模拟仿真用的 MatrikonOPCSimulation(50M),百度网盘,密码: mcur ...
- Mysql查看表的建表语句
已查询Test的建表语句为例: SHOW CREATE TABLE TEST
- .Net版微信支付
一. 案例介绍 这里模拟一个实际业务场景,进行介绍微信支付,业务功能包括:登录.注册.充值.查看充值记录. 页面图: 二. 概要设计 1.数据库设计 这里数据库包括两张表:用户表和订单表. ...