5.日常算法

1. 面试题 17.14. 最小K个数

题目来源

设计一个算法,找出数组中最小的k个数。以任意顺序返回这k个数均可。

示例:

输入: arr = [1,3,5,7,2,4,6,8], k = 4

输出: [1,2,3,4]

方法一:堆

c 复制代码
class Solution {
public:
    vector<int> smallestK(vector<int>& arr, int k) {
        // topk问题------找最小的k个数建立大堆
        vector<int> ret;
        priority_queue<int> heap(arr.begin(), arr.begin() + k);
        for (int i = k; i < arr.size(); i++){
            if (!heap.empty()){
                int top = heap.top();
                if (arr[i] < top){
                    heap.pop();
                    heap.push(arr[i]);
                }
            }
        }
        for (int i = 0; i < k; i++){
            ret.push_back(heap.top());
            heap.pop();
        }
        reverse(ret.begin(), ret.end());
        return ret;
    }
};

方法二:排序

c 复制代码
class Solution {
public:
    vector<int> smallestK(vector<int>& arr, int k) {
        // 使用排序
        sort(arr.begin(), arr.end());
        return vector<int>(arr.begin(), arr.begin() + k);
    }
};

方法三:快排

c 复制代码
class Solution {
public:
    void Sort(vector<int> &arr, int left, int right){
        // 三路划分+随机去基准值
        if (left >= right) return;
        int l = left - 1, r = right + 1;
        int cur = left;
        int key = arr[rand() % (right - left + 1) + left];
        while (cur < r){
            if (arr[cur] < key){
                swap(arr[cur], arr[l + 1]);
                l++;
                cur++;
            }else if (arr[cur] > key){
                swap(arr[cur], arr[r - 1]);
                // 这里不需要进行cur++,因为缓过来的数还没进行判断
                r--;
            }else{
                cur++;
            }
        }
        Sort(arr, left, l);
        Sort(arr, r, right);
    }
    vector<int> smallestK(vector<int>& arr, int k) {
        // 使用排序
        Sort(arr, 0, arr.size() - 1);
        return vector<int>(arr.begin(), arr.begin() + k);
    }
};

2. LCR 154. 复杂链表的复制

题目来源

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

方法一:拼接+拆分

c 复制代码
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if (head == nullptr) return nullptr;
        // 1. 将新赋值的节点都追加到原节点的next下
        Node* cur = head;
        while (cur != nullptr){
            Node* next = cur->next;
            Node* newNode = new Node(cur->val);
            cur->next = newNode;
            newNode->next = next;
            cur = next;
        }
        // 新创建的randow就是原节点的random的next节点
        cur = head;
        while (cur != nullptr){
            Node* newcur = cur->next;
            if (cur->random == nullptr){
                newcur->random = nullptr;
            }else{
                newcur->random = cur->random->next;
            }
            cur = cur->next->next;
        }
        // 将原节点与新节点断开
        Node* ret = head->next;
        cur = head;
        while (cur){
            Node* newcur = cur->next;
            Node* next = newcur->next;
            if (next != nullptr) newcur->next = next->next;
            cur->next = next;
            cur = next;
        }
        return ret;
    }
};

方法二:哈希

c 复制代码
class Solution {
public:
    Node* copyRandomList(Node* head) {
        if (head == nullptr) return nullptr;
        unordered_map<Node*, Node*> hash;
        Node* cur = head;
        while (cur){
            hash[cur] = new Node(cur->val);
            cur = cur->next;
        }

        cur = head;
        while (cur){
            hash[cur]->next = hash[cur->next];
            hash[cur]->random = hash[cur->random];
            cur = cur->next;
        }
        return hash[head];
    }
};
相关推荐
智者知已应修善业5 小时前
【求中位数】2024-1-23
c语言·c++·经验分享·笔记·算法
地平线开发者6 小时前
PTQ 量化数值范围与优化
算法·自动驾驶
sali-tec6 小时前
C# 基于halcon的视觉工作流-章68 深度学习-对象检测
开发语言·算法·计算机视觉·重构·c#
测试人社区-小明6 小时前
智能弹性伸缩算法在测试环境中的实践与验证
人工智能·测试工具·算法·机器学习·金融·机器人·量子计算
罗西的思考7 小时前
【Agent】MemOS 源码笔记---(5)---记忆分类
人工智能·深度学习·算法
qq_4335545410 小时前
C++数位DP
c++·算法·图论
AshinGau10 小时前
Softmax 与 交叉熵损失
神经网络·算法
似水এ᭄往昔10 小时前
【C++】--AVL树的认识和实现
开发语言·数据结构·c++·算法·stl
栀秋66610 小时前
“无重复字符的最长子串”:从O(n²)哈希优化到滑动窗口封神,再到DP降维打击!
前端·javascript·算法
xhxxx10 小时前
不用 Set,只用两个布尔值:如何用标志位将矩阵置零的空间复杂度压到 O(1)
javascript·算法·面试