diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/combinations.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/combinations.cpp')
-rw-r--r-- | other/combinations.cpp | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/other/combinations.cpp b/other/combinations.cpp new file mode 100644 index 0000000..94d4bfc --- /dev/null +++ b/other/combinations.cpp @@ -0,0 +1,39 @@ +// 77. Combinations +#include "leetcode.h" + +/* + * given two integers 'n' and 'k', return all possible combinations of 'k' + * numbers chosen from the range '[1, n]'. you may return the answer in any + * order + */ + +class Solution { +public: + vvd combine(int n, int k) { + vvd ans; + int i = 0; + vector<int> p(k, 0); + while (i >= 0) { + p[i]++; + if (p[i] > n) + --i; + else if (i == k - 1) + ans.push_back(p); + else + i++, p[i] = p[i - 1]; + } + return ans; + } +}; + +int main() { + Solution obj; + for (auto i : obj.combine(4, 2)) + for (auto j : i) + printf("%d ", j); // expect: [[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]] + printf("\n"); + for (auto i : obj.combine(1, 1)) + for (auto j : i) + printf("%d ", j); // expect: [[1]] + printf("\n"); +} |