Skip to content

Commit ec78446

Browse files
author
Liang Wang
committed
update leetcode
1 parent 4913750 commit ec78446

File tree

5 files changed

+178
-0
lines changed

5 files changed

+178
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
class Solution {
3+
public:
4+
vector<int> constructArray(int n, int k) {
5+
vector<int> res;
6+
res.push_back(1);
7+
bool to_right = true;
8+
for (int delta = k; delta >= 1; --delta) {
9+
if (to_right) {
10+
res.push_back(res.back() + delta);
11+
} else {
12+
res.push_back(res.back() - delta);
13+
}
14+
to_right = !to_right;
15+
}
16+
for (int i = k + 2; i <= n; ++i) {
17+
res.push_back(i);
18+
}
19+
return res;
20+
}
21+
};

leetcode/JudgePoint24.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
class Solution {
2+
private:
3+
bool check(vector<double>& nums) {
4+
if (nums.size() == 1) {
5+
return abs(nums[0] - 24.0) < 1e-6;
6+
}
7+
int sz = nums.size();
8+
bool ok = false;
9+
for (int i = 0; i < sz; ++i) {
10+
for (int j = i + 1; j < sz; ++j) {
11+
vector<double> n_nums;
12+
for (int k = 0; k < sz; ++k) {
13+
if (k != i && k != j) {
14+
n_nums.push_back(nums[k]);
15+
}
16+
}
17+
// a + b
18+
n_nums.push_back(nums[i] + nums[j]);
19+
ok = ok || check(n_nums);
20+
n_nums.pop_back();
21+
// a - b
22+
n_nums.push_back(nums[i] - nums[j]);
23+
ok = ok || check(n_nums);
24+
n_nums.pop_back();
25+
// b - a
26+
n_nums.push_back(nums[j] - nums[i]);
27+
ok = ok || check(n_nums);
28+
n_nums.pop_back();
29+
// a * b
30+
n_nums.push_back(nums[i] * nums[j]);
31+
ok = ok || check(n_nums);
32+
n_nums.pop_back();
33+
// a / b
34+
if (abs(nums[j]) > 1e-6) {
35+
n_nums.push_back(nums[i] / nums[j]);
36+
ok = ok || check(n_nums);
37+
n_nums.pop_back();
38+
}
39+
// b / a
40+
if (abs(nums[i]) > 1e-6) {
41+
n_nums.push_back(nums[j] / nums[i]);
42+
ok = ok || check(n_nums);
43+
n_nums.pop_back();
44+
}
45+
if (ok) return true;
46+
}
47+
}
48+
return false;
49+
}
50+
public:
51+
bool judgePoint24(vector<int>& nums) {
52+
vector<double> d_nums;
53+
for (auto& n : nums) {
54+
d_nums.push_back(1.0 * n);
55+
}
56+
return this->check(d_nums);
57+
}
58+
};

leetcode/MaximumSwap.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
private:
3+
int compute(const vector<int>& digits) {
4+
int res = 0;
5+
for (auto& d : digits) {
6+
res = res * 10 + d;
7+
}
8+
return res;
9+
}
10+
public:
11+
int maximumSwap(int num) {
12+
vector<int> digits;
13+
int res = num;
14+
while (num > 0) {
15+
digits.push_back(num % 10);
16+
num = num / 10;
17+
}
18+
reverse(digits.begin(), digits.end());
19+
int sz = digits.size();
20+
for (int i = 0; i < sz; ++i) {
21+
for (int j = i + 1; j < sz; ++j) {
22+
if (digits[i] >= digits[j]) {
23+
continue;
24+
}
25+
swap(digits[i], digits[j]);
26+
int n_num = this->compute(digits);
27+
res = max(res, n_num);
28+
swap(digits[i], digits[j]);
29+
}
30+
}
31+
return res;
32+
}
33+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
private:
14+
set<int> s;
15+
void dfs(TreeNode* root) {
16+
if (NULL == root) {
17+
return;
18+
} else {
19+
this->s.insert(root->val);
20+
dfs(root->left);
21+
dfs(root->right);
22+
}
23+
}
24+
public:
25+
int findSecondMinimumValue(TreeNode* root) {
26+
if (root == NULL || root->left == NULL) {
27+
return -1;
28+
}
29+
s.clear();
30+
dfs(root);
31+
if (s.size() < 2) {
32+
return -1;
33+
}
34+
auto it = s.begin();
35+
++it;
36+
return *it;
37+
}
38+
};

leetcode/TrimBinarySearchTree.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
TreeNode* trimBST(TreeNode* root, int low, int high) {
15+
if (root == NULL) {
16+
return NULL;
17+
}
18+
if (root->val < low) {
19+
return trimBST(root->right, low, high);
20+
} else if (root->val > high) {
21+
return trimBST(root->left, low, high);
22+
} else {
23+
root->left = trimBST(root->left, low, high);
24+
root->right = trimBST(root->right, low, high);
25+
return root;
26+
}
27+
}
28+
};

0 commit comments

Comments
 (0)