Comination
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,
If n = 4 and k = 2, a solution is:
[
[1,2],
[1,3],
[1,4],
[2,4],[2,3],
[3,4],
]
Solution
It's a way to simulate the process of getting 2 elements out of 4,
1. In round one, select 1 from 1...4 as the first number, you have [1]
2. next recursion, select one element from 2...n, you have [1,2], [1, 3], [1,4]
3 back to round one, select 2 from 2...4 as the first number, you have [2]
4. next recursion, select one element from 3..4, you have [2.3], [2,4]
......
No comments:
Post a Comment