[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 ...
随机推荐
- 慎用 new、delete
C++ STL 为我们提供了一套容器.在多数情况下,这套容器已足够让我们使用.所以,需要我们自己去用 new/new []/delete/delete [] 来管理内存的必要性并不是很大.此外,自己管 ...
- poj 1466 Girls and Boys 二分图的最大匹配
Girls and Boys Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://poj.org/problem?id=1466 Descripti ...
- Comparable<T> 和 Comparator<T>
相同点: Comparable<T> 和 Comparator<T>都是接口 不同点: 两者声明的方法不同.前者是compareTo()方法,后者是compare()方法. C ...
- 不错的SDL源码分析
SDL源码分析 1:初始化(SDL_Init()) 2:窗口(SDL_Window) 3:渲染器(SDL_Renderer) 4:纹理(SDL_Texture) 5:更新纹理(SDL_UpdateT ...
- linux(系统centos6.5)常用命令总结
ls -al 列出当前目录下的所有文件和子目录 用户在登录Linux时由/etc/passwd文件来决定要使用哪个shell,用户使用的shell被列于每行的末尾(/bin/bash) ls -F在 ...
- 32.NET中加密解密基本概念
对消息的接收方来说,安全的交流方式需要同时满足3个条件: 1.完整性:消息在传输途中没有被篡改过,即消息是完好无损的. 2.保密性:接收放可以理解或解密来自发送方的信息.(不保证第三方无法获得,但保证 ...
- Oracle rac架构和原理
Oracle RAC Oracle Real Application Cluster (RAC,实时应用集群)用来在集群环境下实现多机共享数据库,以保证应用的高可用性:同时可以自动实现并行处理 ...
- python笔记1-用python解决小学生数学题
前几天有人在群里给小编出了个数学题: 假设你有无限数量的邮票,面值分别为6角,7角,8角,请问你最大的不可支付邮资是多少元? 小编掰着手指头和脚趾头算了下,答案是:1.7元 那么问题来了?为啥是1.7 ...
- go omitempty 忽略类型
nil false 0 每个结构字段的编码可以通过结构字段标签中“json”键下存储的格式字符串来定制.格式字符串给出字段的名称,可能后跟逗号分隔的选项列表.名称可能为空,以指定选项而不覆盖默认字段名 ...
- [翻译] Haneke(处理图片缓存问题)
Haneke https://github.com/hpique/Haneke A lightweight zero-config image cache for iOS. 轻量级0配置图片缓存. H ...