Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2014年12月30日D5 #35

Open
businiaowa opened this issue Dec 29, 2014 · 5 comments
Open

2014年12月30日D5 #35

businiaowa opened this issue Dec 29, 2014 · 5 comments
Labels

Comments

@businiaowa
Copy link

 /*
 *返回arr的所有长度为size的子数组的组合
 * 如arr = [1,2,3,4], size = 2
 * return [[1,2], [1,3], [1,4], [2,3], [2,4], [3,4]]
 *
 * 再如arr = [1,2,3,4], size = 3
 * return [[1,2,3], [1,2,4],[1,3,4] [2,3,4]];
 */
 function power( arr, size) {
    //TODO
 }
@liuyutong
Copy link

 function power(arr, size) {
    var r = [];
    function _(t, a, n) {
        if (n === 0) {
            r[r.length] = t;
            return t;
        }
        for (var i = 0, l = a.length - n; i <= l; i++) {
            var b = t.slice();
            b.push(a[i]);
            _(b, a.slice(i + 1), n - 1);
        }
    }
    _([], arr, size);
    return r;
 }

 alert(power([1,2,3,4], 2).join('\n'));

@businiaowa businiaowa changed the title 2014年12月29日D5 2014年12月30日D5 Dec 29, 2014
@businiaowa
Copy link
Author

function power( arr, size ) {
    var first,
        subPower,
        i, len,
        ret = [];

    if( arr.length === size ) {
        return arr;
    }

    if( size === 1 ) {
        for( i = 0, len = arr.length; i < len; i++ ) {
            ret.push( [arr[i]] );
        }
        return ret;
    }

    while ( arr.length >= size ) {  
        if( arr.length === size ) {
            ret.push( arr );
            break;
        } else {
            first = arr.shift();
            subPower = power( arr.slice(0), size - 1 );
            for( i = 0, len = subPower.length; i < len; i++ ) {
                subPower[i].push(first);
                ret.push( subPower[i] );
            }
        }
    }   
    return ret;
}

上我的答案

@think2011
Copy link
Collaborator

 function power(arr, size) {
    var results = [];

    // 每次截取一个值
    // 循环被截取的数组
    // 每次生成size要求的数组,并将被截取的值放到数组的开头,并保存到集合
    // 递归调用直到数组被清空为止
    (function (arr) {
        var current = arr.shift();
        for(var i = 0; i < arr.length; i++) {
            var result = arr.slice(i, i+(size-1));
            result.unshift(current);

            result.length === size && results.push(result);
        }

        if(arr.length) { arguments.callee(arr) }
    })(arr);

    return results;
 }


 // 测试用例
 console.log(power([1,2,3,4], 2)); // [ [ 1, 2 ], [ 1, 3 ], [ 1, 4 ], [ 2, 3 ], [ 2, 4 ], [ 3, 4 ] ]
 console.log(power([1,2,3,4], 3)); // [ [ 1, 2, 3 ], [ 1, 3, 4 ], [ 2, 3, 4 ] ]

咦,我写的跟题目要求的结果不是一致的.. 不过不想改了.. (~﹃~)~zZ

@witoad
Copy link

witoad commented Jan 16, 2015

var a=[1,2,3,4];

function u(a,i){
var result=[];
var re=[];
function pu(c,d){
var f=d.slice();
c.push(f);
}
function foo(b,j){
if(j>3) {;if(result.length==2){pu(re,result);}}
else {
result.push(b[j]);
foo(b,j+1);
result.pop(b[j]);
foo(b,j+1);
}
}
foo(a,i);
return re
}
u(a,0)

@nunnly nunnly added 难度5 and removed question labels Jan 23, 2015
@lzzwoodtree
Copy link

想到了递归就是没想到怎么个递归法,哈哈,向1楼学习

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants