Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a90b53d

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent 6b360fe commit a90b53d

File tree

5 files changed

+145
-2
lines changed

5 files changed

+145
-2
lines changed

‎0099_recover_binary_search_tree/recover_bst.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ static void dfs(struct TreeNode *node, struct TreeNode **prev,
1818

1919
dfs(node->left, prev, p1, p2, wrong);
2020

21-
/* We must use pointer to pointer for previous object in recursion */
21+
/* We must use pointer to pointer to previous object for backward recursion */
2222
if (*prev != NULL && node->val < (*prev)->val) {
2323
(*wrong)++;
2424
if (*wrong == 1) {
2525
*p1 = *prev;
26-
/* p2 should be recorded here in some cases */
26+
/* p2 frist to be recorded here */
2727
*p2 = node;
2828
} else if (*wrong == 2) {
29+
/* update p2 location */
2930
*p2 = node;
3031
}
3132
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
/**
6+
* Definition for a binary tree node.
7+
* struct TreeNode {
8+
* int val;
9+
* TreeNode *left;
10+
* TreeNode *right;
11+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
12+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
13+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
14+
* };
15+
*/
16+
class Solution {
17+
public:
18+
void recoverTree(TreeNode* root) {
19+
dfs(root);
20+
int tmp = p0_->val;
21+
p0_->val = p1_->val;
22+
p1_->val = tmp;
23+
}
24+
25+
private:
26+
int wrong_ = 0;
27+
TreeNode *prev_ = nullptr;
28+
TreeNode *p0_ = nullptr;
29+
TreeNode *p1_ = nullptr;
30+
31+
void dfs(TreeNode* root) {
32+
if (root == nullptr || wrong_ == 2) {
33+
return;
34+
}
35+
36+
dfs(root->left);
37+
if (prev_ != nullptr && prev_->val > root->val) {
38+
if (++wrong_ == 1) {
39+
p0_ = prev_;
40+
// p1 first to be recorded here
41+
p1_ = root;
42+
} else if (wrong_ == 2) {
43+
// update p1 location
44+
p1_ = root;
45+
}
46+
}
47+
prev_ = root;
48+
dfs(root->right);
49+
}
50+
};

‎0322_coin_change/Makefile‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O1 -o test coin_change.c

‎0322_coin_change/coin_change.c‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
5+
int coinChange(int* coins, int coinsSize, int amount)
6+
{
7+
int i, j;
8+
int *dp = malloc((amount + 1) * sizeof(int));
9+
for (i = 1; i <= amount; i++) {
10+
/* INT_MAX */
11+
dp[i] = amount + 1;
12+
}
13+
14+
dp[0] = 0;
15+
for (i = 1; i <= amount; i++) {
16+
for (j = 0; j < coinsSize; j++) {
17+
if (i - coins[j] >= 0) {
18+
int tmp = 1 + dp[i - coins[j]];
19+
dp[i] = tmp < dp[i] ? tmp : dp[i];
20+
}
21+
}
22+
}
23+
24+
return dp[amount] == amount + 1 ? -1 : dp[amount];
25+
}
26+
27+
int main(int argc, char **argv)
28+
{
29+
if (argc < 3) {
30+
fprintf(stderr, "Usage: ./test 11 1 2 5");
31+
exit(-1);
32+
}
33+
34+
int amount = atoi(argv[1]);
35+
int i, size = argc - 2;
36+
int *coins = malloc(size * sizeof(int));
37+
for (i = 0; i < size; i++) {
38+
coins[i] = atoi(argv[i + 2]);
39+
}
40+
printf("%d\n", coinChange(coins, size, amount));
41+
42+
return 0;
43+
}

‎0322_coin_change/coin_change.cc‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int coinChange(vector<int>& coins, int amount) {
8+
#if 1
9+
vector<int> dp(amount + 1, amount + 1);
10+
dp[0] = 0;
11+
for (int i = 1; i <= amount; i++) {
12+
for (int coin : coins) {
13+
if (i - coin >= 0) {
14+
dp[i] = min(dp[i], 1 + dp[i - coin]);
15+
}
16+
}
17+
}
18+
return dp[amount] == amount + 1 ? -1 : dp[amount];
19+
#else
20+
// BFS solution is slow...
21+
queue<int> q;
22+
unordered_set<int> s;
23+
int step = 0;
24+
q.push(amount);
25+
while (!q.empty()) {
26+
int size = q.size();
27+
for (int i = 0; i < size; i++) {
28+
if (q.front() == 0) {
29+
return step;
30+
}
31+
for (int coin : coins) {
32+
int n = q.front() - coin;
33+
if (n >= 0) {
34+
if (s.count(n) == 0) {
35+
s.insert(n);
36+
q.push(n);
37+
}
38+
}
39+
}
40+
q.pop();
41+
}
42+
step++;
43+
}
44+
return -1;
45+
#endif
46+
}
47+
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /