[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 ...
随机推荐
- ngx_lua配置及应用
一.说明 这里不对lua语言本身及其编译器运行环境等做介绍,以下所有介绍前提对lua相关有所了解. 二.ngx_lua介绍 原理 ngx_lua将Lua嵌入Nginx,可以让Nginx执行Lua脚本, ...
- 【漏洞预警】CVE-2017-8464 震网三代漏洞复现
早在6月13日,微软发布补丁修复编号为CVE-2017-8464的漏洞,本地用户或远程攻击者可以利用该漏洞生成特制的快捷方式,并通过可移动设备或者远程共享的方式导致远程代码执行,追溯到以前,NSA就承 ...
- bzoj 2460 拟阵+判线性相关
/************************************************************** Problem: 2460 User: idy002 Language: ...
- Codeforces Round #361 (Div. 2) D. Friends and Subsequences 二分
D. Friends and Subsequences 题目连接: http://www.codeforces.com/contest/689/problem/D Description Mike a ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) A. Bear and Game 水题
A. Bear and Game 题目连接: http://www.codeforces.com/contest/673/problem/A Description Bear Limak likes ...
- 快速定位问题 Request无法获取参数
比如说最近开发甲修改了iframe标签的src,开发乙在设置src的时候传入了2个参数,通过iframe标签链接到这个页面时,开发乙调试时发现没有拿到任何参数值.然后开发乙百度了一下,发现iframe ...
- [Database] Redis 随笔
Redis 随笔 1. 特点 非关系数据库 non-relational database 内存数据库 高性能 主从复制 可持久化存储 发布与订阅 支持脚本 2. 数据类型5种 STRING 可以是字 ...
- windows控制台程序——关于UNICODE字符的总结(转)
前言:从Windows NT/2000开如,Windows系统已经是一个标准的UNICODE系统,系统内部所有字符串存储及操作均使用UNICODE编码.因此Win32 API都是UNICODE版本的, ...
- 版本号控制-git(二)
上次文章给大家介绍了Git的一些基本知识(http://www.cnblogs.com/jerehedu/p/4582398.html).并介绍了使用git init初始化化版本号库.使用git ad ...
- 我所遭遇过的游戏中间件--Kynapse
我所遭遇过的游戏中间件--Kynapse Autodesk Kynapse游戏中间件是一款面向游戏开发.非玩家控制角色实时模拟的领先的人工智能解决方案.Kynapse具有先进的路径查找功能,比如三维路 ...