美文网首页
Contains Duplicates I,II,III.

Contains Duplicates I,II,III.

作者: 怪味儿果叔 | 来源:发表于2017-01-06 11:41 被阅读0次

Three similar questions.

  • 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.
bool containsDuplicate(vector<int>& nums){
  unordered_map<int,int> hash;
  for(int i = 0; i < nums.size(); i++){
    hash[nums[i]]++;
    if(hash[nums[i]] > 1) return true;
  }
  return false;
}

  • Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the absolute difference between i and j is at most k.

No.1

bool containsNearbyDuplicate(vector<int>& nums, int k) {
  unordered_map<int,int> hash;
  bool exist = false;
  for(int i = 0; i < nums.size() && !exist; i++){
    if(hash[nums[i]] != i+1 && hash[nums[i]] > 0) exist = hash[nums[i]]-1+k >= i;
    hash[nums[i]] = i+1;
  }
  return exist;
}

No.2

bool containsNearbyDuplicate(vector<int>& nums, int k) {
  unordered_set<int> hash;
  for(int i = 0; i < nums.size(); i++){
    if(hash.find(nums[i]) != hash.end() ) return true;
    hash.insert(nums[i]);
    if(i >= k) hash.erase(nums[i-k]);
  }
  return false;
}

  • 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.
    No.1 Not a good solution, just a start. This method did not consider the case when t is very large as the time complexity is O(N*t).
bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t){
  unordered_map<int,int> hash;
  bool exist = false;
  for(int i = 0; i < nums.size() && !exist; i++){
    int temp = nums[i] - t;
    while(hash[temp] == 0 && temp < nums[i]) temp++;
    if(temp < nums[i]) exist = (hash[temp]-1+k >= i);
    if(temp == nums[i] && hash[nums[i]] != i+1 && hash[nums[i]] > 0) exist = hash[nums[i]]-1+k >= i;
    temp = nums[i]+t;
    while(!exist && temp > nums[i] && hash[temp] == 0) temp--;
    if(!exist && temp > nums[i]) exist = (hash[temp]-1+k >= i);
    hash[nums[i]] = i+1;
  }
  return exist;
}

No.3

bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t){
  if(t<0) return false;
  unordered_map<int,int> hash;
  int bucket = t+1;
  for(int i = 0; i < nums.size(); i++){
    int index = nums[i] < 0 ? (nums[i]/bucket - 1) : nums[i]/bucket;
    if(hash.find(index) != hash.end()) return true;
    if(hash.find(index-1) != hash.end() && abs(nums[i] - hash[index-1]) < bucket) return true;
    if(hash.find(index+1) != hash.end() && abs(nums[i] - hash[index+1]) < bucket) return true;
    hash[index] = nums[i];
    if(i >= k){
      index = nums[i-k] < 0 ? (nums[i-k]/bucket - 1) : nums[i-k]/bucket;
      hash.erase(hash.find(index));
    }
  }
  return false;
}

相关文章

  • Contains Duplicates I,II,III.

    Three similar questions. Given an array of integers, find...

  • 总结

    I. Python基础 II. Django框架 III. 爬虫框架 IV. 人工智能 一.数据挖掘 二.Jupy...

  • 2017 目标

    1.体重控制I. 保证一周三次水中有氧II.早晨空腹有氧III.每日睡前拉伸跟紧致下巴2.早起I.吃早饭II.搽防...

  • ios监控

    一.日志监控 I.自己写API调用 II.定义宏替换 III.使用官方提供的框架 ASL apple system...

  • irregular verbs(不规则动词)

    I. A-A-A型(动词原型、过去式、过去分词同形) II. A-A-B型(动词原型和过去式同形) III. A-...

  • CentOS安装Nginx

    1、安装相关组件 2、编译安装Nginx 3、添加环境变量i.打开文件 ii.最后添加 iii.重载资源 4、启动...

  • JS中继承的写法

    继承的两种写法 i.Prototype 写法 ii.Class写法 iii.两种方法的区别 两种方法都能实现继承,...

  • 2017.12.24 周日 提升核心自信4

    系统化提升自信的方法 I.什么是自信?它受什么影响? II.自卑形成的过程 III.如何提升自信 【开场词】 不自...

  • 构建应用的12个因素

    I. 基准代码 一份基准代码,多份部署 II. 依赖 显式声明依赖关系 III. 配置 在环境中存储配置 IV. ...

  • 12-Factor

    12-Factor I. 基准代码一份基准代码,多份部署II. 依赖显式声明依赖关系III. 配置在环境中存储配置...

网友评论

      本文标题:Contains Duplicates I,II,III.

      本文链接:https://www.haomeiwen.com/subject/aijtbttx.html