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;
}
相关推荐
香山上的麻雀10086 分钟前
由 Rust 开发的能大幅降低LLM token消耗的高性能 CLI 代理工具 rtk
开发语言·后端·rust
Fleshy数模7 分钟前
玩转 Python:多线程、装饰器、视觉检测与正则匹配实战
开发语言·python·视觉检测
Felven8 分钟前
B. Make Almost Equal With Mod
数据结构·算法
薛定猫AI8 分钟前
【深度解析】Qwen 3.6 Max Preview:面向智能体编码、视觉推理与 Three.js 前端生成的能力拆解
开发语言·前端·javascript
脆皮炸鸡7559 分钟前
Linux~~基础IO
linux·运维·服务器·经验分享·算法·学习方法
❆VE❆9 分钟前
python实战(一):对接AI大模型并应用
开发语言·人工智能·python·ai
众少成多积小致巨13 分钟前
Android 初始化语言入门
android·linux·c++
colofullove15 分钟前
文本分块策略与预处理
算法
格林威15 分钟前
堡盟Baumer VCX系列工业相机供电与触发:网口(GigE) vs USB3.0
开发语言·人工智能·数码相机·计算机视觉·视觉检测·工业相机·高速相机
yangtuoni15 分钟前
vscode调试C++ python相关配置
c++·vscode·python