vault backup: 2022-07-20 14:25:33

This commit is contained in:
juan 2022-07-20 14:25:33 +08:00
parent bbe5245e86
commit 8561a2c5cf
3 changed files with 229 additions and 0 deletions

View file

@ -24,12 +24,41 @@ tag:#backtrack
___
### Problem
Given two integers `n` and `k`, return _all possible combinations of_ `k` _numbers out of the range_ `[1, n]`.
You may return the answer in **any order**.
#### Examples
**Example 1:**
```
**Input:** n = 4, k = 2
**Output:**
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]
```
**Example 2:**
```
**Input:** n = 1, k = 1
**Output:** [[1]]
```
#### Constraints
**Constraints:**
- `1 <= n <= 20`
- `1 <= k <= n`
### Thoughts
> [!summary]
@ -40,4 +69,38 @@ explanation on the topic.
In my eyes, backtracking means DFS-like recursion and searching in solving problems.
First, it add a option in the combinations, then backtrack it
And when it's done, it gets poped out and try another solution, and backtrack.
### Solution
Backtracking
```cpp
class Solution {
void backtrack(vector<vector<int>> &ans, vector<int> &combs, int n, int nex,
int k) {
if (k == 0) {
ans.push_back(combs);
} else {
// try different solutions
for (int i = nex; i <= n; i++) {
combs.push_back(i);
backtrack(ans, combs, n, i + 1, k - 1);
combs.pop_back();
}
}
}
public:
vector<vector<int>> combine(int n, int k) {
vector<vector<int>> ans = {};
vector<int> combs = {};
backtrack(ans, combs, n, 1, k);
return ans;
}
};
```

View file

@ -0,0 +1,58 @@
# Leetcode Letter-Case-Combination
#### 2022-07-20 14:19
> ##### Algorithms:
> #algorithm #backtrack
> ##### Difficulty:
> #coding_problem #difficulty-medium
> ##### Additional tags:
> #leetcode
> ##### Revisions:
> N/A
##### Related topics:
```expander
tag:#backtrack
```
##### Links:
- [Link to problem](https://leetcode.com/problems/letter-case-permutation/)
___
### Problem
Given a string `s`, you can transform every letter individually to be lowercase or uppercase to create another string.
Return _a list of all possible strings we could create_. Return the output in **any order**.
#### Examples
**Example 1:**
```
**Input:** s = "a1b2"
**Output:** ["a1b2","a1B2","A1b2","A1B2"]
```
**Example 2:**
```
**Input:** s = "3z4"
**Output:** ["3z4","3Z4"]
```
#### Constraints
**Constraints:**
- `1 <= s.length <= 12`
- `s` consists of lowercase English letters, uppercase English letters, and digits.
### Thoughts
> [!summary]
> This is a #backtrack problem.
### Solution

View file

@ -0,0 +1,108 @@
# Leetcode Permutations
#### 2022-07-20 14:16
> ##### Algorithms:
> #algorithm #backtrack
> ##### Difficulty:
> #coding_problem #difficulty-medium
> ##### Additional tags:
> #leetcode
> ##### Revisions:
> N/A
##### Related topics:
```expander
tag:#backtrack
```
##### Links:
- [Link to problem](https://leetcode.com/problems/permutations/)
___
### Problem
Given an array `nums` of distinct integers, return _all the possible permutations_. You can return the answer in **any order**.
#### Examples
**Example 1:**
```
**Input:** nums = [1,2,3]
**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
```
**Example 2:**
```
**Input:** nums = [0,1]
**Output:** [[0,1],[1,0]]
```
**Example 3:**
```
**Input:** nums = [1]
**Output:** [[1]]
```
#### Constraints
**Constraints:**
- `1 <= nums.length <= 6`
- `-10 <= nums[i] <= 10`
- All the integers of `nums` are **unique**.
### Thoughts
> [!summary]
> This is a #backtrack problem
Backtrack problem, see alse [[Leetcode Combinations]]
### Solution
```cpp
class Solution {
void backtrack(vector<vector<int>> &ans, vector<int> &combs, bool unused[],
vector<int> &nums, int n, int k) {
// if the end is hit, append it to ans.
if (k == 0) {
ans.push_back(combs);
} else {
// iterate over the unused vector
for (int i = 0; i < n; i++) {
if (!unused[i]) {
continue;
}
unused[i] = false;
combs.push_back(nums[i]);
backtrack(ans, combs, unused, nums, n, k - 1);
combs.pop_back();
unused[i] = true;
}
}
}
public:
vector<vector<int>> permute(vector<int> &nums) {
// This can be modified from combinations
vector<vector<int>> ans = {};
vector<int> combs = {};
int size = nums.size();
bool unused[size];
for (int i = 0; i < size; i++) {
unused[i] = true;
}
backtrack(ans, combs, unused, nums, size, size);
return ans;
}
};
```