Shuffle an Array随机与取样--力扣101算法题解笔记

9.5Shuffle an Array随机与取样

题目描述

给定一个数组,要求实现两个指令函数,第一个函数"shuffle"可以随机打乱这个数组,第二个函数"reset"可以恢复原来的顺序

输入输出样例

Input :nums = 1, 2, 3, actions: "shuffle", "shuffle", "reset"

Output:\[2,1,3, 3,2,1,1,2,3]

题解

采用景点的Fisher-Yates洗牌算法,原理是通过随机交换位置来实现随机打乱,有正向和反向两种写法,实现很简单。

cpp 复制代码
#include <iostream>
#include <vector>
using namespace std;

class Solution {
    vector<int> origin;
public:
    Solution(vector<int> nums) :origin(std::move(nums)) {}

    vector<int> reset() {
        return origin;
    }

    vector<int> shuffle() {
        if (origin.empty())  return {};
        vector<int> shuffled(origin);
        int n = origin.size();
        //反向洗牌
        for (int i = n - 1; i >= 0; --i) {
            swap(shuffled[i], shuffled[rand() % (i + 1)]);
        }
        //正向洗牌(跟反向效果一样)
        //for (int i = 0; i < n; ++i) {
        //    int pos = rand() % (n - i);
        //    swap(shuffled[i], shuffled[i + pos]);
        //}
        return shuffled;
    }
};

void printVector(const vector<int>& vec) {
    cout << "[";
    for (int i = 0; i < vec.size(); ++i) {
        cout << vec[i];
        if (i != vec.size() - 1) cout << ",";
    }
    cout << "]" << endl;
}

int main() {
    // 1. 初始化原始数组(对应样例输入nums = [1,2,3])
    vector<int> nums = { 1, 2, 3 };

    // 2. 创建Solution对象(注意:Solution solution(); 是函数声明,不是对象创建!)
    Solution solution(nums);

    // 3. 设置随机数种子(否则每次运行shuffle结果都一样)
    srand((unsigned)time(NULL));

    // 4. 模拟样例操作:shuffle → shuffle → reset
    cout << "第一次shuffle结果:";
    printVector(solution.shuffle()); // 输出随机打乱的数组(如[2,1,3])

    cout << "第二次shuffle结果:";
    printVector(solution.shuffle()); // 输出另一个随机打乱的数组(如[3,2,1])

    cout << "reset结果:";
    printVector(solution.reset());   // 输出原始数组[1,2,3]

    return 0;
}
相关推荐
爱吃香菜的初学者25 分钟前
十四.Linux——消息队列
linux·运维·开发语言
迅猛龙办公室39 分钟前
python实现倒序输出数字
开发语言·python
h39745 小时前
DirectShow过滤器开发-视频源过滤器
c++·windows·音视频·directshow
夏幻灵7 小时前
JavaScript this 面试:五种绑定规则、优先级与常见面试陷阱
开发语言·javascript·面试
程序猿编码7 小时前
榨干 RTX5090 算力!Qwen3 专用单卡推理引擎,手写C++/CUDA 算子实现 MTP 推测解码
开发语言·c++·大模型推理·qwen3·多模态推理
西飘客7 小时前
vs 怎么根据dll 生成lib文件
c++·qt
bkspiderx8 小时前
Qt 插件机制:动态扩展应用功能的核心框架
开发语言·qt·元数据·qt 插件·qpluginloader
程序员老陆8 小时前
深入理解 C++ thread_local:线程私有存储的正确打开方式
开发语言·c++·程序设计
沐晓时光9 小时前
C语言入门,深入理解指针(3)
c语言·开发语言
信奥卷王9 小时前
[GESP202609 六级] 分树规划
算法