LeetcCode #220 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] andnums[j] is at mostt and the difference betweeni andj is at mostk.

        本题的Tags是BST,可写BST有点麻烦,其实比较简单的一个思路是,可以定义一个结构体,分别保存nums中元素的值val和下标index,然后对此结构体构成的数组根据val的大小排序,这样就很方便地同时计算  k  值和 t  值了。

       实际上用C++写时,vector中元素的地址值本身就可以作下标用,所以也就没必要再去定义这个结构体了。由此,第一次写好的代码如下:

bool cmp_ptr(int *a, int *b){
    return *a < *b;
}

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {

      const int n = nums.size();
      if(n <= 1 || k == 0) return false;

      vector<int*> num_ptrs(n);
      for(int i = 0; i < n; i++)  num_ptrs[i] = &nums[i];
      sort(num_ptrs.begin(), num_ptrs.end(), cmp_ptr);

      for(int i = 0; i < n; i++){
          for(int j = i + 1; j < n; j++){
              if(*num_ptrs[j] - *num_ptrs[i] > t) break;                    //the difference between nums[i] and nums[j] is at most t
              if(abs(num_ptrs[j] - num_ptrs[i]) <= k) return true;          //the difference between i and j is at most k
          }
      }

       return false;
    }
};


       本地测试没问题后,提交上去,又来了一个在最后一个case上WA!



      不过WA的这个case给了我充足的提示:

              显然是*num_ptrs[j] - *num_ptrs[i] > t 这个判断条件 在INT_MAX - (-1)时溢出了。

       还好leetcode在WA时都给出出错的测试用列,不然根本不可能找到自己代码是在哪个地方死的。

修改后的代码如下,时间是20ms。

bool cmp_ptr(int *a, int *b){
    return *a < *b;
}

class Solution {
public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {

      const int n = nums.size();
      if(n <= 1 || k == 0) return false;

      vector<int*> num_ptrs(n);
      for(int i = 0; i < n; i++)  num_ptrs[i] = &nums[i];
      sort(num_ptrs.begin(), num_ptrs.end(), cmp_ptr);

      for(int i = 0; i < n; i++){
          for(int j = i + 1; j < n; j++){
              if(*num_ptrs[j] > *num_ptrs[i] + t) break;                    //the difference between nums[i] and nums[j] is at most t
              if(abs(num_ptrs[j] - num_ptrs[i]) <= k) return true;          //the difference between i and j is at most k
          }
      }

       return false;
    }
};

另外在Discuss里发现有人用set做了个滑窗,这个思路也挺好,代码量要少很多。不过相对于上一个方法,效率要低些,AC的时间是48ms。

class Solution {
  public:
    bool containsNearbyAlmostDuplicate(vector<int>& nums, int k, int t) {
        set<int> window; // set is ordered automatically 
        for (int i = 0; i < nums.size(); i++) {
            if (i > k) window.erase(nums[i-k-1]); // keep the set contains nums i j at most k
            // -t <= x - nums[i] <= t;
            auto pos = window.lower_bound(nums[i] - t); // x >= nums[i] - t
            if (pos != window.end() && *pos - nums[i] <= t) // x <= nums[i] + t
                   return true;
            window.insert(nums[i]);
        }
        return false;
    }
};


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值