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的更多相关文章

  1. [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 ...

  2. python中的迭代器&&生成器&&装饰器

    迭代器iterator 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束. 迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外, ...

  3. 用 150 行 Python 代码写的量子计算模拟器

    简评:让你更轻松地明白,量子计算机如何遵循线性代数计算的. 这是个 GItHub 项目,可以简单了解一下. qusim.py 是一个多量子位的量子计算机模拟器(玩具?),用 150 行的 python ...

  4. 第4天:scipy库

    一.SciPy库概述 1.numpy提供向量和矩阵的相关操作,高级计算器 2.SciPy在统计.优化.插值.数值积分.视频转换等,涵盖基础科学计算相关问题. (额,对统计和概率,数理完全一窍不通) 3 ...

  5. authenticate验证的流程

    from django.contrib.auth import authenticate # 默认的第一个加密算法 class PBKDF2PasswordHasher(BasePasswordHas ...

  6. 动态规划法(八)最大子数组问题(maximum subarray problem)

    问题简介   本文将介绍计算机算法中的经典问题--最大子数组问题(maximum subarray problem).所谓的最大子数组问题,指的是:给定一个数组A,寻找A的和最大的非空连续子数组.比如 ...

  7. Intel DAAL AI加速 ——传统决策树和随机森林

    # file: dt_cls_dense_batch.py #===================================================================== ...

  8. DBSCAN聚类︱scikit-learn中一种基于密度的聚类方式

    一.DBSCAN聚类概述 基于密度的方法的特点是不依赖于距离,而是依赖于密度,从而克服基于距离的算法只能发现"球形"聚簇的缺点. DBSCAN的核心思想是从某个核心点出发,不断向密 ...

  9. 数据结构( Pyhon 语言描述 ) — — 第3章:搜索、排序和复杂度分析

    评估算法的性能 评价标准 正确性 可读性和易维护性 运行时间性能 空间性能(内存) 度量算法的运行时间 示例 """ Print the running times fo ...

随机推荐

  1. Struts2的概念

    Struts2的概念 Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2作为控制器(Controller)来建立模型与视图的数 ...

  2. bzoj 2300 动态维护上凸壳(不支持删除)

    新技能GET. 用set保存点,然后只需要找前趋和后继就可以动态维护了. /************************************************************** ...

  3. Codeforces Round #222 (Div. 1) B. Preparing for the Contest 二分+线段树

    B. Preparing for the Contest 题目连接: http://codeforces.com/contest/377/problem/B Description Soon ther ...

  4. jmeter3.3—插件管理器的安装

    一.介绍JMeter Plugins 一直以来, JMeter Plugins 为我们提供了很多高价值的JMeter插件,比如: 用于服务器性能监视的 PerfMon Metrics Collecto ...

  5. mysql数据库cup飙升处理思路

    1.先top查看是那一个进程,哪个端口占用CPU多. 2.show processeslist查看是否由于大量并发,锁引起的负载问题. 3.否则,查看慢查询,找出执行时间长的sql:explain分析 ...

  6. mysql关联查询和联合查询

    一.内联方式 1.传统关联查询 "select * from students,transcript where students.sid=transcript.sid and transc ...

  7. Android开发资料

    Android      [开发环境及工具] [控件使用及示例源码] [热更新] [开源组件] [系统内核] [错误及调试]   来自  http://www.howcode.cn ,内容不错,学习中 ...

  8. Linux下分割、合并文件——dd和cat

    功能说明:读取,转换并输出数据. 语 法:dd [bs=<字节数>][cbs=<字节数>][conv=<关键字>][count=<区块数>][ibs=& ...

  9. 普天通信JavaEE开发岗面试题

    1 EJB中有几种Bean,叙述有状态Bean和无状态Bean的差别. 答:EJB中有Session Bean,Entity Bean,以及 Message Driven Bean.这两种的 Sess ...

  10. 【从零学习openCV】IOS7人脸识别实战

    前言 接着上篇<IOS7下的人脸检測>,我们顺藤摸瓜的学习怎样在IOS7下用openCV的进行人脸识别,实际上非常easy,因为人脸检測部分已经完毕,剩下的无非调用openCV的方法对採集 ...