[Algorithm] Print All Subsets of a Set
Let's say given a number of array, you should print out, all the subet of this array.
Example: [1, 2]
Output:
> ""
> 1
> 2
> 1,2
The number of subset should be 2^n...
function print_set(subset) {
if (subset.length === ) {
console.log('empty');
}
console.log(subset.filter(Boolean).join(','));
} function all_subsets(given_array) { function helper(given_array, subset, i) {
if (given_array.length === ) {
print_set([]);
} if (i === given_array.length) {
print_set(subset);
return;
} // in case of not include the current item
subset[i] = null
console.log(`set i: ${i} to null`);
helper(given_array, subset, i + );
// in case of inlcude the current item
subset[i] = given_array[i]
console.log(`set i: ${i} to ${given_array[i]}`);
helper(given_array, subset, i + )
}
let subset = new Array(given_array.length);
helper(given_array, subset, );
} const data = [, ];
all_subsets(data);
/**
set i: 0 to null
set i: 1 to null
""
set i: 1 to 2
2
set i: 0 to 1
set i: 1 to null
1
set i: 1 to 2
1
*/
[Algorithm] Print All Subsets of a Set的更多相关文章
- [Algorithm] Print 2-D array in spiral order
The idea to solve the problem is set five variable, first direction, we need to switch direction aft ...
- python中的迭代器&&生成器&&装饰器
迭代器iterator 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外, ...
- 用 150 行 Python 代码写的量子计算模拟器
简评:让你更轻松地明白,量子计算机如何遵循线性代数计算的. 这是个 GItHub 项目,可以简单了解一下. qusim.py 是一个多量子位的量子计算机模拟器(玩具?),用 150 行的 python ...
- 第4天:scipy库
一.SciPy库概述 1.numpy提供向量和矩阵的相关操作,高级计算器 2.SciPy在统计.优化.插值.数值积分.视频转换等,涵盖基础科学计算相关问题. (额,对统计和概率,数理完全一窍不通) 3 ...
- authenticate验证的流程
from django.contrib.auth import authenticate # 默认的第一个加密算法 class PBKDF2PasswordHasher(BasePasswordHas ...
- 动态规划法(八)最大子数组问题(maximum subarray problem)
问题简介 本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...
- Intel DAAL AI加速 ——传统决策树和随机森林
# file: dt_cls_dense_batch.py #===================================================================== ...
- DBSCAN聚类︱scikit-learn中一种基于密度的聚类方式
一.DBSCAN聚类概述 基于密度的方法的特点是不依赖于距离,而是依赖于密度,从而克服基于距离的算法只能发现"球形"聚簇的缺点. DBSCAN的核心思想是从某个核心点出发,不断向密 ...
- 数据结构( Pyhon 语言描述 ) — — 第3章:搜索、排序和复杂度分析
评估算法的性能 评价标准 正确性 可读性和易维护性 运行时间性能 空间性能(内存) 度量算法的运行时间 示例 """ Print the running times fo ...
随机推荐
- poj 1509
求一个字符串在旋转置换群下最小字典表示. 用的是后缀数组(后缀自动机还是再听听jason_yu讲讲吧,关于right集合的部分还有问题) 最小表示法的思想很有好(判断两个对象在某一置换群划分下,是否等 ...
- TSQL update 简单应用小总结
UPDATE 有两种基本的格式.一种是用静态数据来修改表,另一种是用其他表中的数据来修改表.下面是第一种格式: UPDATE #famousjaycees SET jc = 'Jhony cash', ...
- 华为S5300系列交换机V100R006SPH017VRP热补丁
S5300_V100R006SPH017.pat 附件: 链接:https://pan.baidu.com/s/16lrNMykatXR3_5xKBc2zuw 密码:rt1l
- POJ 1330 Nearest Common Ancestors (LCA,倍增算法,在线算法)
/* *********************************************** Author :kuangbin Created Time :2013-9-5 9:45:17 F ...
- WindowsPhone&Windows8.1&Windows8&Unity3d 填坑日记
近期的游戏开发大体上接近尾声,总结了不少关于Unity3d面向Windows几大平台开发时遇到的各种坑以及怎样填坑的经验.总的来说,Windows8.1 Windows8/RT以及WindowsPho ...
- Detecting Underlying Linux Distro
If you are the owner of the system, then you know which Linux is installed and running. This article ...
- axure8.1可用授权码
Licensee: University of Science and Technology of China (CLASSROOM)Key: DTXRAnPn1P65Rt0xB4eTQ+4bF5IU ...
- java基础学习总结——接口
一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如“金丝猴是一种动物”,金丝猴从动物这个类继承,同时“金丝猴是一种值钱的东西”,金丝猴从“值钱的东西”这个类继承,同时“金丝猴 ...
- 常用NFS mount选项介绍
通过NFS挂接远程主机的文件系统时,使用一些不同的选现可以使得mount比较简单易用.这些选项可以在mount命令中使用,也可以在/etc/fstab和autofs中设定. 以下是NFS mount ...
- MVC自定义编辑视图,DateTime类型属性显示jQuery ui的datapicker
实现的效果为:在编辑视图中,对DateTime类型的属性,显示jQuery UI的datepicker.效果如下: Student.cs public class Student { ...