题解 | #无重复数组#

无重复数组

https://www.nowcoder.com/practice/d2fa3632268b41df9bc417b74802ad8c

<html lang="en">
    <head>
        <meta charset="UTF-8">
    </head>
    <body>

        <script>
            const _getUniqueNums = (start,end,n) => {
                // 补全代码
                let hash = new Array(end+1).fill(0)
   			    let sub = end - start + 1
    			let result = []
    			for (let i = 0; i < n; i++) {
      				let t = Math.floor(Math.random()* sub) + start
     				 while(hash[t]){
       					 t = Math.floor(Math.random() *sub) + start
     				 }
      			hash[t] = 1
     			 result.push(t)
    			}
                return result
            }
        </script>
    </body>
</html>
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务