落花随流水

文章 评论 浏览 30

落花随流水 2022-05-04 13:56:27

用�hash来存区间

/**
 * 随机生成一个长度为 10 的整数类型的数组,
 * 例如 [2, 10, 3, 4, 5, 11, 10, 11, 20]
 * 将其排列成一个新数组,要求新数组形式如下,
 * 例如 [[2, 3, 4, 5], [10, 11], [20]]。
 */

/**  得到一个两数之间的随机整数,包括两个数在内 */
const getRandomIntInclusive = (min, max) =>
  Math.floor(Math.random() * (max - min + 1) + min);

// 生成数组
let arr = Array.from({ length: 10 }, () => getRandomIntInclusive(0, 20));
// 排序
arr = arr.sort((a, b) => a - b);

const intArr = Array.from(new Set([...arr]));
const map = new Map();
intArr.map((i) => {
  const index = Math.floor(i / 10);
  map.has(index) ? map.set(index, [...map.get(index), i]) : map.set(index, [i]);
});

let resArr = [];
for (const [k, v] of map) {
  resArr.push(v);
}
console.log('resArr: ', resArr);

第 67 题:随机生成一个长度为 10 的整数类型的数组

落花随流水 2022-05-04 02:38:20
      let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
      let _arr = []

      function sortPoke() {
        while (arr.length > 0) {
          // 选择抽取哪张牌
          if (arr.length % 2 == 1) {
            _arr.push(arr.pop())
          } else {
            _arr.push(arr.shift())
          }
        }

        return _arr.reverse()
      }

      console.log(sortPoke())
      // [7, 6, 8, 5, 9, 4, 10, 3, 11, 2, 12, 1, 13]

不知道对不对,还请大神指教纠错。

第 126 题:扑克牌问题

更多

推荐作者

櫻之舞

文章 0 评论 0

弥枳

文章 0 评论 0

m2429

文章 0 评论 0

野却迷人

文章 0 评论 0

我怀念的。

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文