From 8b3960e9762441246de2320252aceca505076e2c Mon Sep 17 00:00:00 2001 From: zjeff <1572754810@qq.com> Date: Wed, 21 Sep 2022 19:17:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E6=B4=97=E7=89=8C?= =?UTF-8?q?=E7=AE=97=E6=B3=95=E4=B8=BAO(n)=E7=9A=84=E6=B4=97=E7=89=8C?= =?UTF-8?q?=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/App.tsx b/src/App.tsx index 136dd02..c99002c 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -83,9 +83,19 @@ const makeScene: (level: number, icons: Icon[]) => Scene = (level, icons) => { return scene; }; +// o(n) 时间复杂度的洗牌算法 +const fastShuffle: (arr: T[]) => T[] = (arr) => { + const res = arr.slice(); + for (let i = 0; i < res.length; i++) { + const idx = (Math.random() * res.length) >> 0; + [res[i], res[idx]] = [res[idx], res[i]]; + } + return res; +}; + // 洗牌 const washScene: (level: number, scene: Scene) => Scene = (level, scene) => { - const updateScene = scene.slice().sort(() => Math.random() - 0.5); + const updateScene = fastShuffle(scene); const offsetPool = [0, 25, -25, 50, -50].slice(0, 1 + level); const range = [ [2, 6],