Comments (7)
随机打乱数组:
function shuffle(arr) {
arr.sort(() => {
return Math.random() - 0.5
})
}
from frontend-interview.
- 现在有一个二维数组,数组长度和子数组长度均不可知,问:从每一个子数组里面取一个数成一个数组,要求组合不能重复,问一共有多少个组合,请设计一个函数把所有可能出现的组合以数组的形式输出。
这个方法的适用范围为:二维数组中子数组的每个元素都是数字、重新组合不考虑前后顺序。
如果子数组中可以有字符串,那么 removeSame()
可能不适用。因为若其中一个子数组为 [1, '1'] ,hash[1]
和 hash['1']
会被认为相同。
function reCombine(array) {
if (array.length > 1) {
let recombinedArray = []
for(let i = 0; i < array[0].length; i++) {
for(let j = 0; j < array[1].length; j++) {
recombinedArray.push([].concat(array[0][i], array[1][j]))
}
}
recombinedArray = removeSame(recombinedArray)
array.splice(0, 2, recombinedArray)
reCombine(array)
}
return array[0]
}
function removeSame(array) {
let newArray = [], hash = {}
for (let i = 0; i < array.length; i++) {
if (!hash[array[i]]) {
hash[array[i]] = true
newArray.push(array[i])
}
}
return newArray
}
from frontend-interview.
随机打乱数组:
function obfs(arr) {
return arr.slice().reduce(acc => [...acc, ...arr.splice(parseInt(Math.random() * arr.length, 10), 1)], []);
}
console.log(obfs([1, 2, 3, 4, 5]));
from frontend-interview.
- 如何随机打乱一个js数组
/*
* Returns a new array after disordered.
*
* @param {array} array - The original array needs to be disordered.
* @returns {array} newArray - A new array after disordered.
*
* @author Caijialinxx
*/
function disorderArray(array) {
let newArray = []
let usedIndexs = [] // Put the index of item which had been pushed into newArray. It helps to avoid adding repeated item.
let validCount = 0 // The count of valid pushing into newArray.
top:
while(validCount < array.length) {
let index = Math.floor(Math.random()*array.length)
if(usedIndexs.indexOf(index) > -1) {
continue top
} else {
usedIndexs.push(index)
}
validCount++
newArray.push(array[index])
}
return newArray
}
from frontend-interview.
function disorder(arry) {
return arry.sort(function(a,b) {
return Math.random() - Math.random();
})
}
from frontend-interview.
function disorder(arry) {
return arry.sort(function(a,b) {
return Math.random() - 0.5;
})
}
from frontend-interview.
const combine = (arr) => {
let arr1 = [[]],
arr2 = [];
for(list of arr) {
let l = new Set(list);
for(n of l) {
for(a of arr1) {
arr2.push([...a, n]);
}
}
arr1 = arr2;
arr2 = [];
}
return arr1;
}
from frontend-interview.
Related Issues (20)
- JS 作用域链相关问题 HOT 5
- JS 作用域、原型链 HOT 3
- 重复声明两个函数会怎样? HOT 4
- ES5中“严格模式”与“非严格模式”的区别? HOT 3
- ajax请求序列化问题 HOT 5
- 循环/闭包/setTimeout/Promise 综合 HOT 1
- 服务端如何区分不同的用户 HOT 4
- 使用ES6实现该方法 HOT 13
- dom操作相关 HOT 7
- 什么是FP?与OOP的关系?
- 为何会出现浏览器兼容性问题?如何解决? HOT 2
- 前端性能优化 HOT 2
- JavaScript 分组 HOT 13
- 自适应和响应式有什么区别和联系
- 如何实现深拷贝? HOT 1
- 面试题汇总
- jrg interview questions HOT 7
- 实现一个深拷贝 HOT 8
- 宏任务微任务题 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from frontend-interview.