您的位置:首页 > 大数据 > 人工智能

leecode 解题总结:220. Contains Duplicate III

2017-02-22 21:28 302 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: