Skip to content

Commit 84b500e

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parent bb658c3 commit 84b500e

File tree

19 files changed

+624
-0
lines changed

19 files changed

+624
-0
lines changed

036_valid_sudoku/valid_sudoku.cc

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
bool isValidSudoku(vector<vector<char>>& board) {
8+
/* check row validity */
9+
for (int i = 0; i < board.size(); i++) {
10+
vector<bool> mark(10);
11+
/* check row validity */
12+
for (int j = 0; j < board.size(); j++) {
13+
if (!valid(board, mark, i, j)) {
14+
return false;
15+
}
16+
}
17+
}
18+
19+
/* check column validity */
20+
for (int j = 0; j < board.size(); j++) {
21+
vector<bool> mark(10);
22+
for (int i = 0; i < board.size(); i++) {
23+
if (!valid(board, mark, i, j)) {
24+
return false;
25+
}
26+
}
27+
}
28+
29+
/* check sub-box validity */
30+
for (int k = 0; k < board.size(); k++) {
31+
int sr = k / 3 * 3;
32+
int sc = (k % 3) * 3;
33+
vector<bool> mark(10);
34+
for (int i = sr; i < sr + 3; i++) {
35+
for (int j = sc; j < sc + 3; j++) {
36+
if (!valid(board, mark, i, j)) {
37+
return false;
38+
}
39+
}
40+
}
41+
}
42+
43+
return true;
44+
}
45+
46+
private:
47+
bool valid(vector<vector<char>>& board, vector<bool>& mark, int i, int j) {
48+
if (board[i][j] != '.') {
49+
int index = board[i][j] - '0';
50+
if (mark[index]) {
51+
return false;
52+
} else {
53+
mark[index] = 1;
54+
}
55+
}
56+
return true;
57+
}
58+
};

037_sudoku_solver/sudoku_solver.cc

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
void solveSudoku(vector<vector<char>>& board) {
8+
int size = board.size();
9+
vector<vector<bool>> rows(size, vector<bool>(10));
10+
vector<vector<bool>> cols(size, vector<bool>(10));
11+
vector<vector<bool>> boxes(size, vector<bool>(10));
12+
13+
for (int i = 0; i < size; i++) {
14+
for (int j = 0; j < size; j++) {
15+
if (board[i][j] != '.') {
16+
int num = board[i][j] - '0';
17+
int idx = i / 3 * 3 + j / 3;
18+
rows[i][num] = true;
19+
cols[j][num] = true;
20+
boxes[idx][num] = true;
21+
}
22+
}
23+
}
24+
25+
dfs(board, 0, rows, cols, boxes);
26+
}
27+
28+
private:
29+
bool valid(int num, int row, int col, int idx, vector<vector<bool>>& rows,
30+
vector<vector<bool>>& cols, vector<vector<bool>>& boxes) {
31+
return !rows[row][num] && !cols[col][num] && !boxes[idx][num];
32+
}
33+
34+
bool dfs(vector<vector<char>>& board, int size, vector<vector<bool>>& rows,
35+
vector<vector<bool>>& cols, vector<vector<bool>>& boxes) {
36+
if (size == 9 * 9) {
37+
return true;
38+
} else {
39+
bool ok = false;
40+
int row = size / 9;
41+
int col = size % 9;
42+
int idx = row / 3 * 3 + col / 3;
43+
if (board[row][col] == '.') {
44+
for (int i = 1; i <= 9; i++) {
45+
if (valid(i, row, col, idx, rows, cols, boxes)) {
46+
/* lock this grid as well as the number */
47+
board[row][col] = i + '0';
48+
rows[row][i] = true;
49+
cols[col][i] = true;
50+
boxes[idx][i] = true;
51+
ok = dfs(board, size + 1, rows, cols, boxes);
52+
if (!ok) {
53+
/* release this grid as well as the number */
54+
rows[row][i] = false;
55+
cols[col][i] = false;
56+
boxes[idx][i] = false;
57+
board[row][col] = '.';
58+
}
59+
}
60+
}
61+
} else {
62+
ok = dfs(board, size + 1, rows, cols, boxes);
63+
}
64+
return ok;
65+
}
66+
}
67+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<vector<int>> combinationSum(vector<int>& candidates, int target) {
8+
vector<int> stack;
9+
vector<vector<int>> res;
10+
dfs(candidates, 0, target, stack, res);
11+
return res;
12+
}
13+
14+
private:
15+
void dfs(vector<int>& candidates, int start, int target, vector<int>& stack, vector<vector<int>>& res) {
16+
if (target < 0) {
17+
return;
18+
} else if (target == 0) {
19+
res.push_back(stack);
20+
} else {
21+
for (int i = start; i < candidates.size(); i++) {
22+
stack.push_back(candidates[i]);
23+
/* The elements in solution can be duplicate for the purpose of the problem */
24+
dfs(candidates, i, target - candidates[i], stack, res);
25+
stack.pop_back();
26+
}
27+
}
28+
}
29+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<vector<int>> combinationSum2(vector<int>& candidates, int target) {
8+
vector<int> stack;
9+
vector<vector<int>> res;
10+
sort(candidates.begin(), candidates.end());
11+
dfs(candidates, 0, target, stack, res);
12+
return res;
13+
}
14+
15+
private:
16+
void dfs(vector<int>& candidates, int start, int target, vector<int>& stack, vector<vector<int>>& res) {
17+
if (target < 0) {
18+
return;
19+
} else if (target == 0) {
20+
res.push_back(stack);
21+
} else {
22+
int last = -1;
23+
for (int i = start; i < candidates.size(); i++) {
24+
if (last != candidates[i]) {
25+
/* No duplicate combinations in different order */
26+
stack.push_back(candidates[i]);
27+
/* i + 1 limits the candidate range in next levels */
28+
dfs(candidates, i + 1, target - candidates[i], stack, res);
29+
stack.pop_back();
30+
}
31+
last = candidates[i];
32+
}
33+
}
34+
}
35+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int firstMissingPositive(vector<int>& nums) {
8+
if (nums.size() == 0) {
9+
return 1;
10+
}
11+
12+
int i = 0;
13+
while (i < nums.size()) {
14+
/* nums[i] should be i+1 and nums[nums[i] - 1] should be nums[i] */
15+
if (nums[i] > 0 && nums[i] != i + 1 && nums[i] - 1 < nums.size() && nums[nums[i] - 1] != nums[i]) {
16+
// Let nums[nums[i] - 1] = nums[i]
17+
swap(nums[i], nums[nums[i] - 1]);
18+
} else {
19+
i++;
20+
}
21+
}
22+
23+
for (i = 0; i < nums.size(); i++) {
24+
if (nums[i] != i + 1) {
25+
break;
26+
}
27+
}
28+
29+
return i + 1;
30+
}
31+
};

042_trapping_rain_water/trap_water.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int trap(vector<int>& height) {
8+
/* In fact if we find the relative higher bar position and then the
9+
* water level of the position would be determined by the opposite side.
10+
*/
11+
int res = 0;
12+
int left = 0, left_max = 0;
13+
int right = height.size() - 1, right_max = 0;
14+
while (left < right) {
15+
if (height[left] < height[right]) {
16+
if (height[left] > left_max) {
17+
left_max = height[left];
18+
} else {
19+
res += left_max - height[left];
20+
}
21+
left++;
22+
} else {
23+
if (height[right] > right_max) {
24+
right_max = height[right];
25+
} else {
26+
res += right_max - height[right];
27+
}
28+
right--;
29+
}
30+
}
31+
32+
return res;
33+
}
34+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
string multiply(string num1, string num2) {
8+
string res(num1.length() + num2.length(), '0');
9+
for (int i = num2.length() - 1; i >= 0; i--) {
10+
int j, carry = 0;
11+
for (j = num1.length() - 1; j >= 0; j--) {
12+
carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0');
13+
res[i + j + 1] = carry % 10 + '0';
14+
carry /= 10;
15+
}
16+
res[i + j + 1] = carry + '0';
17+
}
18+
19+
int i;
20+
for (i = 0; i < res.length() - 1; i++) {
21+
if (res[i] != '0') {
22+
break;
23+
}
24+
}
25+
return res.substr(i);
26+
}
27+
};

045_jump_game_ii/jump_game.cc

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
int jump(vector<int>& nums) {
8+
int steps = 0;
9+
int lo = 0, hi = 0;
10+
while (hi < nums.size() - 1) {
11+
int right = 0;
12+
for (int i = lo; i <= hi; i++) {
13+
// right > hi for nums[i] > 0
14+
right = max(i + nums[i], right);
15+
}
16+
// [lo, hi] is the next location range
17+
lo = hi + 1;
18+
hi = right;
19+
steps++;
20+
}
21+
22+
return steps;
23+
}
24+
};

046_permutations/permutations.cc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<vector<int>> permute(vector<int>& nums) {
8+
vector<vector<int>> res;
9+
vector<int> stack;
10+
vector<bool> used(nums.size());
11+
dfs(nums, used, stack, res);
12+
return res;
13+
}
14+
15+
private:
16+
void dfs(vector<int>& nums, vector<bool>& used, vector<int>& stack, vector<vector<int>>& res) {
17+
if (stack.size() == nums.size()) {
18+
res.push_back(stack);
19+
} else {
20+
for (int i = 0; i < nums.size(); i++) {
21+
if (!used[i]) {
22+
used[i] = true;
23+
stack.push_back(nums[i]);
24+
dfs(nums, used, stack, res);
25+
stack.pop_back();
26+
used[i] = false;
27+
}
28+
}
29+
}
30+
}
31+
};

047_permutations_ii/permutations.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<vector<int>> permuteUnique(vector<int>& nums) {
8+
vector<vector<int>> res;
9+
vector<int> stack;
10+
vector<bool> used(nums.size());
11+
sort(nums.begin(), nums.end());
12+
dfs(nums, used, stack, res);
13+
return res;
14+
}
15+
16+
private:
17+
void dfs(vector<int>& nums, vector<bool>& used, vector<int>& stack, vector<vector<int>>& res) {
18+
if (stack.size() == nums.size()) {
19+
res.push_back(stack);
20+
} else {
21+
for (int i = 0; i < nums.size(); i++) {
22+
if (!used[i]) {
23+
if (i > 0 && !used[i - 1] && nums[i - 1] == nums[i]) {
24+
/* In case that duplicate permutation with same elemements */
25+
/* Used marks allow same elements in different DFS levels */
26+
continue;
27+
}
28+
used[i] = true;
29+
stack.push_back(nums[i]);
30+
dfs(nums, used, stack, res);
31+
stack.pop_back();
32+
used[i] = false;
33+
}
34+
}
35+
}
36+
}
37+
};

0 commit comments

Comments
 (0)