Skip to content

Commit ec7eead

Browse files
committed
[Function add]
1. Add leetcode solutions in C++ version.
1 parent 9e29029 commit ec7eead

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

leetcode/216. Combination Sum III.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,28 @@ class Solution {
101101
}
102102
}
103103
```
104+
105+
### C++ version
106+
* Method 1:recursion
107+
```objectivec
108+
class Solution {
109+
vector<vector<int>> res_;
110+
void dfs(vector<int>& temp, int target, int index, int sum, int k){
111+
if(temp.size() == k && sum == target){
112+
res_.emplace_back(temp);
113+
}else if(sum < target && temp.size() < k){
114+
for(int i = index; i <= 9; ++i){
115+
temp.emplace_back(i);
116+
dfs(temp, target, i + 1, sum + i, k);
117+
temp.pop_back();
118+
}
119+
}
120+
}
121+
public:
122+
vector<vector<int>> combinationSum3(int k, int n) {
123+
vector<int> temp;
124+
dfs(temp, n, 1, 0, k);
125+
return res_;
126+
}
127+
};
128+
```

leetcode/46. Permutations.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,28 @@ class Solution {
9898
}
9999
}
100100
```
101+
102+
### C++ Version
103+
* Method 1:
104+
```objectivec
105+
class Solution {
106+
vector<vector<int>> res_;
107+
void dfs(int begin, int size, vector<int>& nums){
108+
if(begin >= size){
109+
res_.push_back(nums);
110+
return;
111+
}else{
112+
for(int i = begin; i < size; ++i){
113+
swap(nums[begin], nums[i]);
114+
dfs(begin + 1, size, nums);
115+
swap(nums[begin], nums[i]);
116+
}
117+
}
118+
}
119+
public:
120+
vector<vector<int>> permute(vector<int>& nums) {
121+
dfs(0, nums.size(), nums);
122+
return res_;
123+
}
124+
};
125+
```

leetcode/47. Permutations II.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,35 @@ class Solution {
7676
}
7777
}
7878
}
79+
```
80+
81+
### C++ version
82+
* Method 1:
83+
```objectivec
84+
class Solution {
85+
vector<vector<int>> res_;
86+
void dfs(int index, vector<bool>& visited, vector<int>& temp, vector<int>& nums){
87+
if(temp.size() == nums.size()) res_.emplace_back(temp);
88+
else{
89+
int size = nums.size();
90+
for(int i = 0; i < size; ++i){
91+
if (i > 0 && nums[i] == nums[i - 1] && !visited[i - 1]) continue;
92+
if(visited[i]) continue;
93+
visited[i] = true;
94+
temp.emplace_back(nums[i]);
95+
dfs(i + 1, visited, temp, nums);
96+
temp.pop_back();
97+
visited[i] = false;
98+
}
99+
}
100+
}
101+
public:
102+
vector<vector<int>> permuteUnique(vector<int>& nums) {
103+
vector<bool> visited(nums.size(), false);
104+
vector<int> temp;
105+
sort(nums.begin(), nums.end());
106+
dfs(0, visited, temp, nums);
107+
return res_;
108+
}
109+
};
79110
```

leetcode/784. Letter Case Permutation.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,26 @@ Note:
5757
}
5858
```
5959

60+
### C++ version
61+
* Method 1:
62+
```objectivec
63+
class Solution {
64+
vector<string> res_;
65+
void dfs(string s, int index, string temp){
66+
if(index == s.length()) res_.emplace_back(temp);
67+
else{
68+
if(!isalpha(s[index])){
69+
dfs(s, index + 1, temp + s[index]);
70+
}else{
71+
dfs(s, index + 1, temp + (char)tolower(s[index]));
72+
dfs(s, index + 1, temp + (char)toupper(s[index]));
73+
}
74+
}
75+
}
76+
public:
77+
vector<string> letterCasePermutation(string S) {
78+
dfs(S, 0, "");
79+
return res_;
80+
}
81+
};
82+
```

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy