xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

permutations and combinations in js All In One

permutations and combinations in js All In One

js 中的排列组合

概念

permutation 排列, 如果次序重要就叫排列, 排列是一种有序的组合。✅
combination 组合, 如果次序不重要就叫组合

两种基本排列

可重复:如,密码锁的密码,可以是 666
不可重复:如,密码锁的密码,可以是 369

有两种组合(次序不重要):

可重复:如, 口袋里的硬币 (5角, 5角, 1元,1元)
不可重复:如, 彩票号码,30选7 (1,3,5,7,9,11,13)

https://www.shuxuele.com/combinatorics/combinations-permutations.html

阶乘!(一种不可重复的排列✅)

例子:4! 是 4 x 3 x 2 x 1 的简写

https://www.shuxuele.com/numbers/factorial.html

彩票

https://www.shuxuele.com/data/lottery.html

七乐彩采用组合式玩法,从01—30共30个号码中选择7个号码组合为一注投注号码。

https://www.cwl.gov.cn/fcpz/yxjs/qlc/

demos


/* 

permutations & combinations
排列 & 组合

https://leetcode.com/problems/3sum/

给定一个数字数组,找出有三个元素为一组构成的所有不重复的子数字数组!
*/

// const arr  = [1,2,3]
const arr  = [1,2,3,4,5]

const arrs = [];
const map = new Map();

for (let i = 0; i < (arr.length - 2); i++) {
  for (let j = i + 1; j < (arr.length - 1); j++) {
    for (let k = i + 2; k < arr.length; k++) {
      const value = [arr[i], arr[j], arr[k]];
      const key = value.sort((a, b) => a - b > 0 ? 1 : -1).join('');
      // 排序,序列化,去除重复组合,如 134 & 143
      if(!map.has(key)) {
        // TypeError: map.get is not a function or its return value is not iterable
        // map.set(key, [...map.get(key), value]);
        map.set(key, value);
        if(j !== k) {
          // 去除重复数字,如 133
          arrs.push(value);
        }
      } else {
        map.set(key, [...map.get(key), value]);
      }
    }
  }
}

console.log(`arrs =`, arrs)

/* 

$ node ./permutations.js
arrs = [
  [ 1, 2, 3 ], [ 1, 2, 4 ],
  [ 1, 2, 5 ], [ 1, 3, 4 ],
  [ 1, 3, 5 ], [ 1, 4, 5 ],
  [ 2, 3, 4 ], [ 2, 3, 5 ],
  [ 2, 4, 5 ], [ 3, 4, 5 ]
]

*/


// // const arr  = [1,2,3]
// const arr  = [1,2,3,4,5]

// const arrs = [];

// for (let i = 0; i < [arr.length - 2]; i++) {
//   for (let j = i + 1; j < [arr.length - 1]; j++) {
//     for (let k = i + 2; k < arr.length; k++) {
//       let f = arr[i];
//       let s = arr[j];
//       let t = arr[k];
//       // console.log(`[f,s,t] =`, [f,s,t], `?`, f,s,t)
//       // arrs.push([f,s,t]);
//       if(s !== t) {
//         arrs.push([f,s,t]);
//       }
//     }
//   }
// }

// console.log(`arrs =`, arrs)

image

test 验证

组合与排列计算器

image

Combinations without repetition (n=5, r=3)
Using Items: 1,2,3,4,5

List has 10 entries.
[1,2,3],[1,2,4],[1,2,5],[1,3,4],[1,3,5],[1,4,5],[2,3,4],[2,3,5],[2,4,5],[3,4,5]

https://www.shuxuele.com/combinatorics/combinations-permutations-calculator.html

refs

https://www.cnblogs.com/xgqfrms/p/16557404.html

https://www.cnblogs.com/xgqfrms/p/16389706.html



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(8)  评论(3编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2023-04-30 如何通过 API 查看 cnblogs 博客访问量和博客积分与排名 All In One
2023-04-30 Python 3 alias All In One
2023-04-30 Raspberry Pi GPIO pins All In One
2022-04-30 rollup.js & vue.js All In One
2022-04-30 基于网络质量的自适应服务 All In One
2022-04-30 Chrome DevTools console All In One
2021-04-30 CORS & preflight request All In One
点击右上角即可分享
微信分享提示