Contains Duplicate III —— LeetCode
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] and nums[j] is at most t and the difference between i and j is at most k.
题目大意:给定一个数组,找出是否存在两个不同下标的值相差<=t,下标i和j相差<=k。
解题思路:题目没说数组有序,那就得按照无序处理,可以排序,然后从头遍历;或者用个BST之类的有序数据结构,遍历数组的时候重建一下,重建的过程中判断是否有合法的解,有就返回true,否则重建完之后返回false。
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if(nums==null||nums.length==0||k<=0){
return false;
}
TreeSet<Integer> tree = new TreeSet<>();
for(int i=0;i<nums.length;i++){
Integer big = tree.floor(nums[i]+t);//floor返回集合里<=指定元素的最大值
Integer small = tree.ceiling(nums[i]-t);//celing返回集合里>=指定元素的最小值
if((big!=null&&big>=nums[i])||(small!=null&&small<=nums[i])){
return true;
}
if(i>=k){
tree.remove(nums[i-k]);
}
tree.add(nums[i]);
}
return false;
}
}
Contains Duplicate III —— LeetCode的更多相关文章
- Contains Duplicate III -leetcode
Contains Duplicate III Given an array of integers, find out whether there are two distinct indices i ...
- 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 ...
- [LeetCode] Contains Duplicate III 包含重复值之三
Given an array of integers, find out whether there are two distinct indices i and j in the array suc ...
- 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 ...
- Java for LeetCode 220 Contains Duplicate 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
Description: Given an array of integers, find out whether there are two distinct indices i and j in ...
随机推荐
- 客户端session与服务端session
会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...
- Asp.net主题(theme)和皮肤(skin)的使用
asp.net 的服务器端控件提供了多种样式的设计,如果对每个控件都单独设置,是比较繁琐的事情,所以微软也提供了针对这些服务器端控件的样式管理,其实也可以通过 css来控制部分服务器端控件的样式,比如 ...
- jni使用
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 简介 详解 JNI 元素 JNI函数实战 AndroidmkApplicationmk Androidmk Applicat ...
- Android开发需要注意的地方
1.理解运用商场概略 开发者对商场状况的理解与APP的胜利紧密相连,往常,AppStore和GooglePlay能够说是挪动运用最为丰厚的运用生态,像苹果的下载计算表单会记载抢手运用的下载 ...
- C语言中,如何通过socket得到对端IP地址
struct sockaddr_in clientaddr1; memset(&clientaddr1, 0x00, sizeof(clientaddr1)); socklen_t nl=si ...
- ubuntu12.04安装QQ2013
1.下载Longene QQ2013SP6 http://pan.baidu.com/s/1hq83fWo 2.安装 1)如果之前安装过旧版本需要先卸载(通过dpkg -l | grep qq查看). ...
- C蛮的全栈之路-序章 技术栈选择与全栈工程师
目录 C蛮的全栈之路-序章 技术栈选择与全栈工程师C蛮的全栈之路-node篇(一) 环境布置C蛮的全栈之路-node篇(二) 实战一:自动发博客 博主背景 985院校毕业,至今十年C++开发工作经验, ...
- 浅谈html5某些新元素的用途
大家都知道html是一种前端网页语言,从出现到现在已经经历了很多的版本了,但是随着html的不断发展,现在的html5已经不再是单一的前端页面语言了,html,javascript,css不再单纯的只 ...
- login-登录
<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Inse ...
- 谷歌地图实现车辆轨迹移动播放(google map api)
开发技术:jquery,js baidu map api,json,ajax QQ1310651206 谷歌地图(google map api)实现车辆轨迹移动播放(google map api)