当前位置: 首页 > news >正文

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] and nums[j] is at most t and the difference between i and j is at most k.


就是给定1个数组nums,以及索引最大间隔k,和值最大间隔t,在nums数组内,找出是否存在num[i],和nums[j],其中i,j ∈[0,nums.Length-1],使得 abs(i-j) <= k 并且 abs(nums[i]-nums[j]) <= t


思路:
1. 遍历nums数组,使用BST来存nums[i]在k范围内所有可达的数
2. 使用SortedSet<T>这个数据结构来存放从i到k范围内的每个数,由于SortedSet的内部实现是平衡树
(http://stackoverflow.com/questions/3262947/is-there-a-built-in-binary-search-tree-in-net-4-0),因此它的增删查都是log(n),所以算上外循环,总体复杂度是n*log(n)
3. 确定要查找的边界:
min = nums[i] - t; // 可取的最小值
max = nums[i] + t; // 可取的最大值


4. 如果sortedSet的长度大于 k,删除第一个,只需要维护长度为k的数据就可以了




实现代码:







public class Solution {
    public bool ContainsNearbyAlmostDuplicate(int[] nums, int k, int t) {
    	if(k < 1 || t < 0){
			return false;
		}
		
		if(nums == null || nums.Length == 1)
    	{
    		return false;
    	}
		
		var map = new SortedSet<int>();
		
        for(var i = 0 ;i < nums.Length; i++)
		{
		    if(nums[i] > Int32.MaxValue){
				return false;
			}
			
			if(map.Count > k){
				map.Remove(nums[i-k-1]);
			}
			
			var max = nums[i] + t;
			if(nums[i] > 0 && max < 0){
				max = Int32.MaxValue;
			}
			
			var min = nums[i] - t;
			
			var found = map.GetViewBetween(min,max);
			if(found.Count > 0){
				return true;
			}


			map.Add(nums[i]);
       }
	   
       return false;
    }
}


相关文章:

  • LeetCode -- Combination Sum
  • MySQL添加用户
  • LeetCode -- Candy
  • Leet -- Plus One
  • Leet -- Generate Parentheses
  • LeetCode -- Distinct Subsequences
  • LeetCode -- SpiralOrder
  • Windows 2003下成功配置IIS+Php+Mysql+Zend Optimizer+GD库+Phpmyadmin
  • LeetCode -- WordBreak II
  • Azure 证书配置错误: The service configuration file does not provide the certificate identification
  • Linux精彩一句话最新版
  • LeetCode -- Count Complete Tree Node
  • LeetCode -- Isomorphic Strings
  • LeetCode -- Balanced Binary Tree
  • Linux系统信息查看命令大全
  • 收藏网友的 源程序下载网
  • [笔记] php常见简单功能及函数
  • Android组件 - 收藏集 - 掘金
  • Debian下无root权限使用Python访问Oracle
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • MyEclipse 8.0 GA 搭建 Struts2 + Spring2 + Hibernate3 (测试)
  • PHP 7 修改了什么呢 -- 2
  • springMvc学习笔记(2)
  • 产品三维模型在线预览
  • 给初学者:JavaScript 中数组操作注意点
  • 基于Android乐音识别(2)
  • 思否第一天
  • 推荐一个React的管理后台框架
  • 微信小程序填坑清单
  • 学习笔记:对象,原型和继承(1)
  • 用Visual Studio开发以太坊智能合约
  • 容器镜像
  • ###51单片机学习(1)-----单片机烧录软件的使用,以及如何建立一个工程项目
  • (175)FPGA门控时钟技术
  • (MIT博士)林达华老师-概率模型与计算机视觉”
  • (ZT) 理解系统底层的概念是多么重要(by趋势科技邹飞)
  • (转)平衡树
  • (转载)虚函数剖析
  • (轉)JSON.stringify 语法实例讲解
  • .NET 8 编写 LiteDB vs SQLite 数据库 CRUD 接口性能测试(准备篇)
  • .NET连接MongoDB数据库实例教程
  • .sh 的运行
  • @Autowired标签与 @Resource标签 的区别
  • @RequestBody与@ResponseBody的使用
  • [ C++ ] STL---仿函数与priority_queue
  • [<事务专题>]
  • [Android] Amazon 的 android 音视频开发文档
  • [Android开源]EasySharedPreferences:优雅的进行SharedPreferences数据存储操作
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据
  • [BZOJ1178][Apio2009]CONVENTION会议中心
  • [BZOJ2850]巧克力王国
  • [C#] 基于 yield 语句的迭代器逻辑懒执行
  • [codeforces] 25E Test || hash
  • [HOW TO]如何在iPhone应用程序中发送邮件
  • [JavaEE]线程的状态与安全