Datawhale|LeetCodeTencent Task14(lc215,217, 230)

215. Kth Largest Element in an Array

Description

在这里插入图片描述

Analysis & Solution

寻找数组中第k大的元素,经典问题,可以接用快速排序的思路解决。快速排序中每次选取一个pivot,并在本轮遍历中将pivot放到其最终所在位置,即pivot左侧元素都比它小,右侧元素都比它大。这里每次进行和快速排序相同的操作找到pivot,若pivot为k,pivot就是第k小(n-k大)的元素,若pivot>k,则在pivot左侧递归地查找,否则在pivot右侧递归地查找。

Code

class Solution {
public:
    int quickSelect(vector<int>& a, int l, int r, int index) {
        int q = randomPartition(a, l, r);
        if (q == index) {
            return a[q];
        } else {
            return q < index ? quickSelect(a, q + 1, r, index) : quickSelect(a, l, q - 1, index);
        }
    }

    inline int randomPartition(vector<int>& a, int l, int r) {
        int i = rand() % (r - l + 1) + l;
        swap(a[i], a[r]);
        return partition(a, l, r);
    }

    inline int partition(vector<int>& a, int l, int r) {
        int x = a[r], i = l - 1;
        for (int j = l; j < r; ++j) {
            if (a[j] <= x) {
                swap(a[++i], a[j]);
            }
        }
        swap(a[i + 1], a[r]);
        return i + 1;
    }

    int findKthLargest(vector<int>& nums, int k) {
        srand(time(0));
        return quickSelect(nums, 0, nums.size() - 1, nums.size() - k);
    }
};

217. Contains Duplicate

Description

在这里插入图片描述

Analysis & Solution

本题目方法比较多
(1)对数组排序,判断相邻元素是否存在相同的情况。
(2)创建哈希表,将元素值作为key放入,若某个键值已有元素,说明存在重复元素。

Code

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        for (int i = 0; i < n - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                return true;
            }
        }
        return false;
    }
};

class Solution {
public:
    bool containsDuplicate(vector<int>& nums) {
        unordered_set<int> s;
        for (int x: nums) {
            if (s.find(x) != s.end()) {
                return true;
            }
            s.insert(x);
        }
        return false;
    }
};


230. Kth Smallest Element in a BST

Description

在这里插入图片描述

Analysis & Solution

二叉搜索树任一结点左子树中节点都比它小,右子树中元素都比它大。因此二叉搜索树的中序遍历得到有序数组。求第k小的元素,即查找其中序遍历的第k个元素。

Code

class Solution {
public:
    int kthSmallest(TreeNode* root, int k) {
        if (!root) {
            return -1;
        }

        int numOfLeftNodes = getNumOfNodes(root->left);
        int numOfRightNodes = getNumOfNodes(root->right);
        if (numOfLeftNodes == k - 1) {
            return root->val;
        } else if (numOfLeftNodes >= k) {
            return kthSmallest(root->left, k);
        } else {
            return kthSmallest(root->right, k - numOfLeftNodes - 1);
        }

    }

private:
    int getNumOfNodes(TreeNode* root) {
        if (!root) {
            return 0;
        }

        return getNumOfNodes(root->left) + getNumOfNodes(root->right) + 1;
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值