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];
    }
};
相关推荐
爱吃芒果的蘑菇34 分钟前
Python读取获取波形图波谷/波峰
python·算法
晨曦学习日记1 小时前
Leetcode239:滑动窗口最大值,双端队列的实现!
数据结构·c++·算法
CoovallyAIHub1 小时前
无人机图像+深度学习:湖南农大团队实现稻瘟病分级检测84%准确率
深度学习·算法·计算机视觉
2zcode1 小时前
基于Matlab自适应阈值分割算法的图像处理研究
图像处理·算法·matlab
阿群今天学习了吗1 小时前
RNN、LSTM、Transformer推荐博文
人工智能·笔记·python·学习·算法
菥菥爱嘻嘻1 小时前
力扣面试150(42/150)
算法·leetcode·职场和发展
Morriser莫1 小时前
动态规划Day5学习心得
算法·动态规划
饭碗的彼岸one2 小时前
重生之我在10天内卷赢C++ - DAY 2
linux·开发语言·c++·笔记·算法·vim
Cando学算法2 小时前
AtCoder Beginner Contest 416(ABCDE)
c++·算法
zzywxc7873 小时前
随着人工智能技术的飞速发展,大语言模型(Large Language Models, LLMs)已经成为当前AI领域最引人注目的技术突破。
人工智能·深度学习·算法·低代码·机器学习·自动化·排序算法