首页 > 代码库 > [Twitter] All possible pickings
[Twitter] All possible pickings
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。