题解 | #集合的所有子集(一)#

集合的所有子集(一)

http://www.nowcoder.com/practice/c333d551eb6243e0b4d92e37a06fbfc9

 * 
 * @param A int整型一维数组 
 * @return int整型二维数组
 */
function subsets( A ) {
    // write code here
    if(A.length == 0)    return [];
    A.sort(function(a,b){return a- b;})
    let res = [],tmp = [];
    res.push([]);
    for(let i = 1;i <= A.length;i++){
        sub(A,res,tmp,0,i);
    }
    return res;
}
function sub( A,res,tmp,start,num ){
    if(tmp.length === num){
        res.push(tmp.slice());
        return;
    }
    for(let i = start;i < A.length;i++){
        tmp.push(A[i]);
        sub(A,res,tmp,i + 1,num)
        tmp.pop();
    }
}
module.exports = {
    subsets : subsets
};

alt

全部评论

相关推荐

零OFFER战士:另一个版本查看图片
点赞 评论 收藏
分享
05-11 20:45
门头沟学院 Java
有担当的灰太狼又在摸...:零帧起手查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务