leetcode题解日练--2016.8.27

###不给自己任何借口

今日题目:

1、判断数组是否包含重复元素|||; tag:BST

2、两个数组的交集 tag:二分|哈希表|两指针|排序

今日摘录:

一见钟情,明明是见色起意.
日久生情,不过是权衡利弊.
连白头到老,
都只是习惯使然.

——安逸《这咬人的爱》

220. Contains Duplicate III | Difficulty: Medium

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.

tag:BST
题意:从一个整形数组中,找是否存在两个序号i,j满足|nums[i]-nums[j]|<=t,|i-j|<=k

思路:首先需要一个滑动窗口,每次只在这k+1个元素中去进行查找,对于每次判断的数nums[i],首先找是否存在一个x,使得x>=nums[i]-t –> nums[i]-x<=t,如果找到符合条件的x,再看是否符合nums[i]-x>=-t 。如果依然符合则说明找到题设条件。
1、

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

结果:48ms

349. Intersection of Two Arrays | Difficulty: Easy

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].

Note:
Each element in the result must be unique.
The result can be in any order.

tag:二分|哈希表|两指针|排序

题意:求两个数组的交集,重复多次只算一次

思路:
1、将其中一个数组存入集合中,然后遍历第二个数组,如果第二个数组中的元素在第一个数组中能找到,则加入最终的结果并在集合中移除该元素。

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        unordered_set<int>m(nums1.begin(),nums1.end());
        vector<int>result;
        for(auto a:nums2)
        {
            if (m.count(a))
            {
                result.push_back(a);
                m.erase(a);
            }
        }
        return result;
    }
};

结果:8ms

2、对两个数组进行排序,然后利用两个指针。

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(),nums1.end());
        sort(nums2.begin(),nums2.end());
        int p1=0,p2=0;
        vector<int> res;
        while(p1<nums1.size() && p2<nums2.size())
        {
            if(nums1[p1]<nums2[p2]) p1++;
            else if(nums1[p1]>nums2[p2]) p2++;
            else
            {
                if(res.empty() || res.back()!=nums1[p1])
                    res.push_back(nums1[p1]);
                p1++;
                p2++;
            }
        }
        return res;
    }
};

结果:12ms

3、对其中一个数组排序,然后对另一个数组中的所有元素在其中进行二分查找,如果找到了就加入集合中,最后将结果从集合复制到向量即可。

class Solution {
public:
    bool BinarySearch(vector<int>nums,int target)
    {
        int len = nums.size();
        int l = 0,r = len-1;
        while(l<=r)
        {
            int mid = l+(r-l)/2;
            if(nums[mid]<target)    l = mid+1;
            else if(nums[mid]>target)   r = mid-1;
            else    return true;   
        }
        return false;
    }

    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(),nums1.end());
        set<int> res;
        for(auto num:nums2)
        {
            if(BinarySearch(nums1,num))
            {
                res.insert(num);
            }
        }
        vector<int> result;
        for(auto i:res)
            result.push_back(i);
        return result;
    }
};

结果:12ms

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值