vault backup: 2022-09-21 14:35:34
This commit is contained in:
parent
8aa7a6d3bb
commit
2a1004a7c7
|
@ -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<vector<int>> ans;
|
||||
vector<int> 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<vector<int>> combine(int n, int k) {
|
||||
N = n;
|
||||
K = k;
|
||||
backtrack(1);
|
||||
|
||||
return ans;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue