码迷,mamicode.com
首页 > 其他好文 > 详细

[Twitter] All possible pickings

时间:2015-01-16 08:46:43      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:np   twitter   

Question: 

Given n sets of choices: (1, 2, 3), (a, b, c), (i, ii, iii).

You pick one element from each set of choices. Generate all possible pickings. 

http://www.glassdoor.com/Interview/Given-n-sets-of-choices-1-2-3-2-3-4-4-5-You-pick-one-element-from-each-set-of-choices-Generate-all-possibl-QTN_218899.htm


private List<List<String>> generateAllPickings(String[][] pool)
{
    List<List<String>> result = new ArrayList<>();
    help(pool, 0, new ArrayList<String>(), result);
    return result;
}

private void help(String[][] pool, 
                  int group,
                  List<String> cur,
                  List<List<String>> result)
{
    if (cur.size() == pool.length)
    {
        result.add(new ArrayList<String>(cur));
    }
    if (group >= pool.length)
    {
        return;
    }
    
    // Now we are looking at pool[group], put each elements into the cur
    for (int i = 0 ; i < pool[group].length ; i ++)
    {
        cur.add(pool[group][i]);
        help(pool, group + 1, cur, result);
        cur.remove(cur.size() - 1);
    }
}


[Twitter] All possible pickings

标签:np   twitter   

原文地址:http://7371901.blog.51cto.com/7361901/1604604

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!