leecode 解题总结:220. Contains Duplicate III

#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <unordered_map>
using namespace std;
/*
问题:
Given an array of integers, find out whether there are two distinct indices i and j 
in the array such that the absolute difference between nums[i] and nums[j] is at most t 
and the absolute difference between i and j is at most k.

分析:也就是说要寻找任意元素对应的下标i和j,满足i和j的差的绝对值<=k,两个数值的差值<=t
暴力破解:确定起始位置beg,在beg+k的范围中不断计算两个数的差值是否<=t。
时间复杂度:极端地,k=n,那么对于每个起始点,要遍历所有剩余节点,时间复杂度为O(n^2)
考虑到如果排序后,计算的起始点值为v,则另一个数值v-k,v+k,通过二分法定位到这个上下区间,
然后判定下标差值是否符合。总的时间复杂度为:
排序: O(NlogN),寻找,O(logN),遍历

输入:
3(数组元素个数) 2(k) 3(t)
7 1 3
1 2 3
1
3 2 3
1 5 10
2 1 2147483647
-1 2147483647
2 2 4
-3 3
输出:
true
false
false
false
false
关键:
1 被两数相减溢出坑了。面对简单的题目的加减法一定要考虑溢出
报错:Input:[-1,2147483647] 1 2147483647
Output:true
Expected:false

2还是溢出
//简单的题目:两个int数据相加减,需要先转化为long long,结果也要是long long,否则溢出
value1 = (long long)nums.at(i);
value2 = (long long)nums.at(j);
result = llabs(value1 - value2);
Input:
[2147483647,-2147483647]
1
2147483647
Output:
true
Expected:
false

3果然超时
参考这位:http://blog.csdn.net/Jeanphorn/article/details/46625729 的解法
题目是滑动窗口问题。
|nums(i) - nums(j)| <= t 【1】
|nums(i)/t - nums(j)/t| <= 1 【2】
所以nums(i)/t 属于{ nums(j)/t , nums(j)/t - 1 , nums(j)/t + 1 } 
只需要以 另key = nums(i)/t 作为键,以 nums(i)作为值存储,然后当前元素nums[i]和dict[key],dict[key-1]
dict[key+1]比较即可。
另外一旦 i >= k,删除窗口之外的键值,确保下标在合理范围
			//删除窗口以外的键值
			if(i >= k)
			{
				dict.erase( nums.at(i - k) / max(1, t) );
			}
		}
*/

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        if(nums.empty() || k <= 0 || t < 0 || 1 == nums.size())
		{
			return false;
		}
		int size = nums.size();
		unordered_map<int , long long> dict;
		for(int i = 0 ; i < size ; i++)
		{
			//注意t不能为0
			int key = nums.at(i) / max(1 ,t);
			if( ( dict.find(key) != dict.end() && llabs((long long)nums.at(i) - dict.at(key) ) <= t ) || 
				( dict.find(key - 1) != dict.end() && llabs((long long)nums.at(i) - dict.at(key - 1) ) <= t ) || 
				( dict.find(key + 1) != dict.end() && llabs((long long)nums.at(i) - dict.at(key + 1) ) <= t ) )
			{
				return true;
			}

			//找不到,需要插入键值对: nums(i) / max(1, t) ,存入的值是当前元素
			dict[key] = nums.at(i) ;

			//删除窗口以外的键值
			if(i >= k)
			{
				dict.erase( nums.at(i - k) / max(1, t) );
			}
		}
		return false;
    }
};

void print(vector<int>& result)
{
	if(result.empty())
	{
		cout << "no result" << endl;
		return;
	}
	int size = result.size();
	for(int i = 0 ; i < size ; i++)
	{
		cout << result.at(i) << " " ;
	}
	cout << endl;
}

void process()
{
	 vector<int> nums;
	 int value;
	 int num;
	 Solution solution;
	 vector<int> result;
	 int maxIndexDiff;
	 int maxValueDiff;
	 while(cin >> num >> maxIndexDiff >> maxValueDiff)
	 {
		 nums.clear();
		 for(int i = 0 ; i < num ; i++)
		 {
			 cin >> value;
			 nums.push_back(value);
		 }
		 bool result = solution.containsNearbyAlmostDuplicate(nums , maxIndexDiff , maxValueDiff);
		 if(result)
		 {
			 cout << "true" << endl;
		 }
		 else
		 {
			 cout << "false" << endl;
		 }
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值