From 2a1004a7c7fc2bf58d381ea5fcc8630cc95dd5dc Mon Sep 17 00:00:00 2001 From: juan Date: Wed, 21 Sep 2022 14:35:34 +0800 Subject: [PATCH] vault backup: 2022-09-21 14:35:34 --- OJ notes/pages/Leetcode Combinations.md | 33 ++++++++++++++++++- .../Leetcode Implement-Queue-Using-Stacks.md | 8 +++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/OJ notes/pages/Leetcode Combinations.md b/OJ notes/pages/Leetcode Combinations.md index 2b751bb..2c0b30d 100644 --- a/OJ notes/pages/Leetcode Combinations.md +++ b/OJ notes/pages/Leetcode Combinations.md @@ -16,7 +16,7 @@ > > ##### Revisions: > -> N/A +> 2022-09-21 ##### Related topics: @@ -110,3 +110,34 @@ public: } }; ``` + +Alternative solution using private values +```cpp +class Solution { + vector> ans; + vector combs = {}; + int N, K; + void backtrack(int cur) { + if (combs.size() == K) { + // we reached the end, should push to the answer array + ans.push_back(combs); + return; + } else if (cur <= N) { + combs.push_back(cur); + backtrack(cur + 1); + + combs.pop_back(); + backtrack(cur + 1); + } + } + +public: + vector> combine(int n, int k) { + N = n; + K = k; + backtrack(1); + + return ans; + } +}; +``` diff --git a/OJ notes/pages/Leetcode Implement-Queue-Using-Stacks.md b/OJ notes/pages/Leetcode Implement-Queue-Using-Stacks.md index 8baa81c..50b65a0 100644 --- a/OJ notes/pages/Leetcode Implement-Queue-Using-Stacks.md +++ b/OJ notes/pages/Leetcode Implement-Queue-Using-Stacks.md @@ -16,13 +16,13 @@ > > ##### Revisions: > -> N/A +> 2022-09-21 ##### Related topics: ##### Links: -- [Link to problem](https://leetcode.com/problems/implement-queue-using-stacks/submissions/) +- [Link to problem](https://leetcode.com/problems/implement-queue-using-stacks/) --- @@ -52,15 +52,19 @@ Implement the `MyQueue` class: ``` **Output** +``` [null, null, null, 1, 1, false] +``` **Explanation** +``` MyQueue myQueue = new MyQueue(); myQueue.push(1); // queue is: [1] myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue) myQueue.peek(); // return 1 myQueue.pop(); // return 1, queue is [2] myQueue.empty(); // return false +``` #### Constraints