列表

详情


JS7. 无重复数组

描述

请补全JavaScript代码,实现一个函数,要求如下:
1. 根据输入的数字范围[start,end]和随机数个数"n"生成随机数
2. 生成的随机数存储到数组中,返回该数组
3. 返回的数组不能有相同元素
注意:
1. 不需要考虑"n"大于数字范围的情况

示例1

输入:

getUniqueNums(2,10,4)

输出:

[4,6,2,8]

原站题解

HTML/CSS/JavaScript 解法, 执行用时: 1686ms, 内存消耗: 77852KB, 提交时间: 2022-02-10

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n    <body>\n\n        <script>\n            const _getUniqueNums = (start,end,n) => {\n                // 补全代码\n             let arr = [];\n              while(arr.length<n){\n                  let v =Math.floor(Math.random()*(end-1)+start)\n                  if(arr.indexOf(v)===-1){\n                      arr.push(v)\n                  }\n              }\n                return arr\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1694ms, 内存消耗: 77876KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n    <body>\n\n        <script>\n            const _getUniqueNums = (start,end,n) => {\n                // 补全代码\n                let arr = []\n                for(let i=0;i<n;i++){\n                    let num = parseInt(Math.random() * (end - start)) + start\n                    while (arr.indexOf(num)!== -1) {\n                        num = parseInt(Math.random() * (end - start)) + start\n                    }\n                    arr.push(num)\n                }\n                return arr\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1746ms, 内存消耗: 77772KB, 提交时间: 2021-12-03

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n    <body>\n\n        <script>\n            const _getUniqueNums = (start,end,n) => {\n                // 补全代码\n                if (n > end - start + 1) return [];\n                let arr = [];\n                for (let i = 0; i < n; i++){\n                    let num;\n                    do {\n                        num = Math.round(Math.random() * (end - start + 1)) + start;\n                    }while (arr.includes(num))\n                        arr.push(num);\n                }\n                return arr;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1747ms, 内存消耗: 77928KB, 提交时间: 2021-12-05

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n    <body>\n\n        <script>\n            const _getUniqueNums = (start,end,n) => {\n                // 补全代码\n                 let arr = [];\n                while( arr.length<n){\n                    let v = Math.floor(Math.random()*(end -1 )+start);\n                    if(arr.indexOf(v) ===-1){\n                        arr.push(v);\n                    }\n                }\n                \n                return arr;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

HTML/CSS/JavaScript 解法, 执行用时: 1748ms, 内存消耗: 77792KB, 提交时间: 2022-02-09

{"css":"","js":"","html":"<!DOCTYPE html>\n<html lang=\"en\">\n    <head>\n        <meta charset=\"UTF-8\">\n    </head>\n    <body>\n\n        <script>\n            const _getUniqueNums = (start, end, n) => {\n                // 补全代码\n                var arr = [];\n                let i = 0;\n                while (i < n) {\n                    let number = parseInt(Math.random() * (end - start) + start);\n                    if (arr.indexOf(number) === -1) {\n                        arr.push(number);\n                        i++;\n                    }\n                }\n                return arr;\n            }\n        </script>\n    </body>\n</html>","libs":[]}

上一题