-
Notifications
You must be signed in to change notification settings - Fork 634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
leetcode384:打乱数组(洗牌算法) #74
Comments
sisterAn
changed the title
蚂蚁金服/字节跳动/Bigo/网易:洗牌算法
蚂蚁金服&字节跳动&Bigo&网易leetcode384:打乱数组(洗牌算法)
Jun 30, 2020
sisterAn
changed the title
蚂蚁金服&字节跳动&Bigo&网易leetcode384:打乱数组(洗牌算法)
leetcode384:打乱数组(洗牌算法)
Jun 30, 2020
浅拷贝数组,利用random()方法重制数组角标 /**
* @param {number[]} nums
*/
var Solution = function(nums) {
this.nums = nums;
};
/**
* Resets the array to its original configuration and return it.
* @return {number[]}
*/
Solution.prototype.reset = function() {
return this.nums;
};
/**
* Returns a random shuffling of the array.
* @return {number[]}
*/
Solution.prototype.shuffle = function() {
let num = this.nums.slice();
for(let i = 0;i < num.length;i++){
let index = Math.floor((i+1)* Math.random());
[num[index],num[i]] = [num[i],num[index]]
}
return num;
}; |
function solution(arr) {
let tempArr = arr.slice()
for (let i = 0; i < tempArr.length; i++) {
let index = Math.floor(Math.random() * (tempArr.length - i) + i)
let temp = tempArr[i]
tempArr[i] = tempArr[index]
tempArr[index] = temp
}
return tempArr
}``` |
sort(() => Math.random() - 0.5) 真正乱排序: /**
* @param {number[]} nums
*/
var Solution = function(nums) {
this.origin = nums
};
/**
* Resets the array to its original configuration and return it.
* @return {number[]}
*/
Solution.prototype.reset = function() {
return this.origin
};
/**
* Returns a random shuffling of the array.
* @return {number[]}
*/
Solution.prototype.shuffle = function() {
let copyArr = [...this.origin];
let len = copyArr.length;
for(let i = len - 1; i >=0; i-- ) {
let index = ~~(Math.random() * (i+1));
[copyArr[index], copyArr[i]] = [copyArr[i], copyArr[index]];
}
return copyArr;
};
/**
* Your Solution object will be instantiated and called as such:
* var obj = new Solution(nums)
* var param_1 = obj.reset()
* var param_2 = obj.shuffle()
*/ |
function fun(arr){
const res = []
while(arr.length){
let index = Math.floor(Math.random()*arr.length)
res.push(arr.splice(index,1)[0])
}
return res
} |
解答:Fisher-Yates 洗牌算法let Solution = function(nums) {
this.nums = nums
};
Solution.prototype.reset = function() {
return this.nums
};
Solution.prototype.shuffle = function() {
let res = [...this.nums]
let n = res.length
for(let i = n-1; i >= 0; i--) {
let randIndex = Math.floor(Math.random() * (i + 1))
swap(res, randIndex, i)
}
return res
};
let swap = function(arr, i, j) {
const temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
} 复杂度分析:
|
var Solution = function(nums) {
this.arr = nums;
};
Solution.prototype.reset = function() {
return this.arr;
};
Solution.prototype.shuffle = function() {
// 拷贝数组
let nums = this.arr.slice();
let len = nums.length;
for(let i = 0;i<len;i++) {
let ran = Math.random()*len | 0;
[nums[i], nums[ran]] = [nums[ran], nums[i]];
}
return nums;
}; |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
打乱一个没有重复元素的数组。
示例:
附赠leetcode可测试链接:leetcode384:打乱数组
The text was updated successfully, but these errors were encountered: