#71 不重复数字


  • 0
    administrators

    编写一个 JavaScript 函数 uniqueNums,该函数有一个参数 n(一个不大 31 的整数),其返回值是一个数组,该数组内是 n 个随机且不重复的整数,且整数取值范围是 [2, 32]

    请你完成 uniqueNums 的编写。


  • 2

    试一下这个:

    const uniqueNums = (n) => 
      [...(new Array(31)).keys()]
        .map((i) => i + 2)
        .sort(() => Math.random() - Math.random())
        .slice(0, n)
    

  • 1

    也不一定要随机洗牌的。。。

    const uniqueNums = (n) => {
      const ret = []
      for (let i = 0; i < n; i++) {
        let rand = -1
        do rand = 2 + Math.random() * 31 | 0; while(ret.includes(rand))
        ret.push(rand)
      }
      return ret
    }
    

  • 1

    当然也可以更加暴力一点

    const vlist = [
      [12, 32, 31, 4, 24, 14, 15, 7, 26, 16, 2, 28, 25, 23, 18, 5, 17, 8, 27, 13, 6, 21, 30, 22, 29, 20, 9, 19, 10, 3, 11],
      [13, 28, 23, 19, 5, 25, 16, 20, 22, 24, 30, 21, 17, 9, 31, 14, 7, 2, 11, 32, 15, 3, 12, 4, 29, 10, 26, 6, 8, 18, 27],
      [22, 21, 32, 19, 30, 4, 9, 16, 24, 23, 31, 11, 26, 2, 7, 18, 14, 29, 8, 5, 3, 13, 27, 12, 28, 25, 15, 10, 20, 6, 17],
      [2, 10, 12, 5, 28, 21, 24, 22, 6, 11, 15, 29, 9, 8, 32, 31, 13, 4, 19, 30, 25, 3, 23, 18, 14, 27, 26, 17, 7, 16, 20],
      [24, 9, 5, 7, 29, 22, 11, 30, 12, 21, 26, 18, 20, 6, 8, 31, 27, 15, 14, 23, 19, 2, 25, 10, 13, 32, 16, 17, 4, 28, 3],
      [2, 17, 27, 21, 8, 22, 31, 25, 14, 16, 11, 23, 29, 18, 9, 19, 12, 3, 20, 15, 4, 6, 28, 5, 32, 30, 13, 7, 24, 26, 10],
      [12, 18, 31, 22, 19, 21, 16, 25, 27, 3, 9, 32, 24, 6, 11, 7, 13, 10, 15, 23, 5, 2, 14, 29, 20, 26, 30, 4, 17, 8, 28],
      [13, 2, 10, 29, 7, 19, 5, 22, 6, 20, 16, 15, 12, 23, 14, 30, 28, 9, 27, 32, 25, 24, 31, 26, 3, 21, 4, 18, 8, 11, 17],
      [15, 24, 18, 25, 3, 17, 8, 20, 10, 5, 29, 27, 21, 4, 13, 32, 22, 14, 26, 31, 7, 12, 19, 9, 28, 6, 30, 11, 16, 2, 23],
      [22, 29, 32, 5, 15, 13, 24, 25, 30, 20, 27, 12, 2, 28, 9, 14, 4, 18, 17, 7, 23, 16, 6, 31, 10, 8, 19, 3, 26, 11, 21],
      [22, 11, 21, 6, 3, 14, 27, 12, 13, 5, 30, 28, 20, 8, 4, 26, 32, 18, 10, 23, 29, 16, 24, 2, 25, 19, 9, 7, 15, 17, 31],
      [3, 14, 4, 24, 27, 26, 25, 31, 16, 13, 28, 10, 17, 2, 12, 29, 5, 11, 18, 15, 32, 6, 30, 21, 20, 7, 9, 22, 8, 19, 23],
      [26, 23, 20, 28, 4, 25, 18, 3, 11, 6, 21, 24, 12, 8, 19, 27, 29, 2, 14, 5, 32, 15, 7, 31, 9, 22, 16, 30, 13, 10, 17],
      [3, 7, 14, 17, 4, 12, 25, 10, 31, 22, 13, 15, 28, 23, 21, 24, 5, 11, 20, 6, 16, 30, 2, 29, 9, 26, 18, 8, 19, 32, 27],
      [8, 27, 18, 5, 31, 16, 24, 19, 15, 6, 17, 25, 2, 22, 30, 21, 23, 3, 20, 11, 29, 4, 9, 13, 7, 28, 26, 32, 10, 12, 14],
      [14, 29, 4, 23, 27, 26, 5, 2, 3, 32, 10, 21, 16, 19, 15, 25, 18, 22, 11, 24, 8, 13, 28, 30, 12, 17, 6, 20, 31, 9, 7],
      [26, 14, 5, 6, 10, 16, 27, 25, 24, 8, 4, 31, 7, 18, 15, 9, 22, 30, 28, 13, 23, 21, 12, 2, 3, 32, 20, 17, 29, 19, 11],
      [28, 4, 2, 29, 15, 7, 27, 22, 17, 25, 18, 20, 30, 23, 13, 24, 14, 21, 26, 6, 9, 19, 10, 5, 32, 31, 3, 8, 12, 11, 16],
      [21, 2, 31, 13, 20, 28, 4, 5, 23, 14, 22, 25, 30, 7, 18, 16, 17, 8, 11, 15, 24, 12, 19, 6, 3, 10, 9, 26, 27, 32, 29],
      [8, 16, 4, 23, 13, 19, 29, 21, 25, 15, 22, 6, 5, 18, 10, 20, 26, 12, 17, 24, 27, 9, 7, 31, 14, 30, 28, 32, 11, 3, 2],
      [9, 6, 5, 27, 30, 29, 31, 18, 20, 32, 15, 28, 24, 19, 11, 8, 10, 3, 7, 16, 22, 26, 21, 23, 12, 2, 14, 17, 4, 25, 13],
      [5, 20, 22, 12, 27, 28, 7, 31, 6, 29, 26, 24, 15, 9, 14, 18, 23, 25, 10, 32, 8, 30, 19, 3, 13, 4, 2, 16, 17, 21, 11],
      [20, 12, 29, 3, 32, 9, 14, 13, 7, 23, 10, 25, 11, 18, 22, 15, 30, 19, 27, 28, 21, 2, 17, 5, 31, 26, 16, 4, 24, 8, 6],
      [17, 19, 23, 21, 15, 16, 26, 27, 11, 13, 28, 30, 29, 4, 20, 14, 22, 24, 8, 3, 32, 5, 18, 9, 10, 7, 31, 12, 25, 2, 6],
      [29, 7, 25, 27, 28, 13, 4, 14, 23, 9, 24, 30, 26, 22, 2, 31, 11, 12, 10, 8, 19, 16, 32, 15, 3, 18, 21, 17, 20, 5, 6]
    ]
    const uniqueNums = (n, offset = Math.random() * 31 | 0, data = vlist[Math.random() * vlist.length | 0]) => [...(new Array(n)).keys()].map(x => (x + offset) % 31).map(i => data[i])
    

  • 3
    管理员

    es6 Set思路:

    const uniqueNums = (n) => {
      let set = new Set()
      while(set.size < n) {
        set.add(Math.floor(2 + Math.random()*31))
      }
      return [...set]
    }
    

  • 0

    @陈小俊 set可以直接用size的


  • 0
    管理员

    @CodeHz 忘了这茬了


  • 0

    const uniqueNums = (n) => {
      //构建一个[2,32]的数组
      let nums = []
      for(let i = 2; i <= 32; i++) {
        nums.push(i)
      }
      //随机从数组中取n个数
      let ret = []
      for(let j = 0; j < n; j++) {
        let k = ~~(Math.random() * nums.length)
        ret.push(nums[k])
        nums.splice(k, 1)
      }
      return ret
    }
    

    前端表单验证工具 https://github.com/WLDragon/SMValidator


  • 0

    const uniqueNums = (n) => {
      let a = []
      let b = []
      // 创建一个[2, 32]的数组
      for (let i = 2; i <= 32; i++) {
        a.push(i)
      }
      // 随机互换位置打乱数组
      for (let i = 0; i < 31; i++) {
        let x = Math.round(Math.random() * 30)
        let y = Math.round(Math.random() * 30)
        ;[a[x], a[y]] = [a[y], a[x]]
      }
      for (let i = 0; i < n; i++) {
        b.push(a[i])
      }
      return b
    }
    

  • 0

    const uniqueNums = n => {
      if (n > 31) return false;
      let arr = Array.from(new Array(31), (_, i) => i + 2);
      arr.sort(() => {
        return Math.random() > 0.5 ? 1 : -1;
      })
      return arr.slice(0, n)
    }
    

  • 1

    const uniqueNums = (n) => {
      let arr = [];
      while(arr.length !== n) {
        let randomNum = Math.round(Math.random() * 30 + 2);
        if(arr.indexOf(randomNum) === -1) {
          arr.push(randomNum);
        }
      }
      return arr;
    }
    

    非常普通的方法


  • 0

    非常麻烦的方法

    const uniqueNums = (n) => {
      let resultSet = new Set()
      while(resultSet.size<n){
        let randomNum = Math.random()*10;
        if(randomNum.toFixed(2)>=5){
          randomNum = randomNum-5
        }
        if(randomNum.toFixed(2)<1){
          randomNum = randomNum + 1
        }
        randomNum = Math.pow(2,randomNum)
        randomNum = randomNum.toFixed(0)
        resultSet.add(randomNum - 0)
      }
      return [...resultSet]
    }
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待