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];
    }
};
相关推荐
workflower4 分钟前
使用谱聚类将相似度矩阵分为2类
人工智能·深度学习·算法·机器学习·设计模式·软件工程·软件需求
cwywsx14 分钟前
Linux:进程控制2
linux·运维·算法
真的想上岸啊28 分钟前
c语言第一个小游戏:贪吃蛇小游戏06
c语言·算法·链表
边跑边掩护34 分钟前
LeetCode 648 单词替换题解
算法·leetcode·职场和发展
小森77671 小时前
(七)深度学习---神经网络原理与实现
人工智能·深度学习·神经网络·算法
迷茫不知归路1 小时前
操作系统实验习题解析 上篇
c++·算法·操作系统·实验课设
愚润求学2 小时前
【递归、搜索与回溯】专题一:递归(二)
c++·笔记·算法·leetcode
水水沝淼㵘2 小时前
嵌入式开发学习日志(数据结构--顺序结构单链表)Day19
linux·服务器·c语言·数据结构·学习·算法·排序算法
June`3 小时前
专题四:综合练习( 找出所有子集的异或总和再求和)
c++·算法·深度优先·剪枝
Magnum Lehar3 小时前
3d游戏引擎的Utilities模块实现下
c++·算法·游戏引擎