数组乱序
两种随机法
shuffle0 = (str) => {
return Array.from(str).sort(() => Math.random() - 0.5).join('')
}
shuffle1 = (str) => {
const arr = str.split('')
for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
return arr.join('')
}
LeetCode解
export class Solution {
constructor(nums) {
this.nums = nums
}
reset() {
return this.nums
}
shuffle() {
const str = this.nums
const arr = Array.isArray(str) ? [...str] : Array.from(str)
for (var j, x, i = arr.length; i; j = parseInt(Math.random() * i), x = arr[--i], arr[i] = arr[j], arr[j] = x);
return arr
}
}
shuffle0
shuffle1
Lazy Sudoku
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
7
8
9