Skip to content

Commit 94dd29a

Browse files
authored
Monorepo with blaze (#2)
1 parent 9c03fc4 commit 94dd29a

File tree

12 files changed

+141
-54
lines changed

12 files changed

+141
-54
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@
3333
*.exe
3434
*.out
3535
*.app
36+
37+
# Ignore all bazel-* symlinks. There is no full list since this can change
38+
# based on the name of the directory bazel is cloned into.
39+
bazel-*

BUILD

Whitespace-only changes.

WORKSPACE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
2+
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
3+
4+
http_archive(
5+
name = "com_google_googletest",
6+
urls = ["https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip"],
7+
strip_prefix = "googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5",
8+
)
9+
10+
http_archive(
11+
name = "rules_cc",
12+
urls = ["https://github.com/bazelbuild/rules_cc/archive/40548a2974f1aea06215272d9c2b47a14a24e556.zip"],
13+
strip_prefix = "rules_cc-40548a2974f1aea06215272d9c2b47a14a24e556",
14+
)
15+
16+
git_repository(
17+
name = "com_google_absl",
18+
remote = "https://github.com/abseil/abseil-cpp.git",
19+
tag = "20200225.2",
20+
)

bazel-leetcode

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/private/var/tmp/_bazel_charlie/e7928186a18aa82e5e16642fa33bc2a2/execroot/__main__

cpp/0001_two_sum/0001_two_sum.cc

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "0001_two_sum.h"
12
#include <iostream>
23
#include <unordered_map>
34
#include <vector>
@@ -6,29 +7,18 @@ using std::cout;
67
using std::unordered_map;
78
using std::vector;
89

9-
class Solution {
10-
public:
11-
vector<int> twoSum(vector<int> &nums, int target) {
12-
unordered_map<int, int> umap;
13-
for (int i = 0; i < nums.size(); i++) {
14-
const int current = nums[i];
10+
vector<int> Solution::twoSum(vector<int> &nums, int target) {
11+
unordered_map<int, int> umap;
12+
for (int i = 0; i < nums.size(); i++) {
13+
const int current = nums[i];
1514

16-
if (umap.count(current) > 0) {
17-
return {i, umap[current]};
18-
}
19-
20-
const int diff = target - nums[i];
21-
umap[diff] = i;
15+
if (umap.count(current) > 0) {
16+
return {i, umap[current]};
2217
}
2318

24-
return {};
19+
const int diff = target - nums[i];
20+
umap[diff] = i;
2521
}
26-
};
2722

28-
int main() {
29-
vector<int> nums = {2, 7, 11, 15};
30-
auto result = Solution().twoSum(nums, 9);
31-
for (auto n : result) {
32-
cout << n << " ";
33-
}
23+
return {};
3424
}

cpp/0001_two_sum/0001_two_sum.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <vector>
2+
3+
using std::vector;
4+
5+
class Solution {
6+
public:
7+
vector<int> twoSum(vector<int> &nums, int target);
8+
};

cpp/0001_two_sum/0001_two_sum_test.cc

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
//#include <gtest/gtest.h>
2-
#include <0001_two_sum>
1+
#include "cpp/0001_two_sum/0001_two_sum.h"
2+
#include <gtest/gtest.h>
3+
#include <vector>
34

5+
using std::vector;
46

7+
TEST(TwoSumTest, Example1) {
8+
vector<int> nums = {2, 7, 11, 15};
9+
const vector<int> mock = Solution().twoSum(nums, 9);
10+
11+
const vector<int> solution = {1, 0};
12+
for (int i = 0; i < mock.size(); ++i) {
13+
EXPECT_EQ(solution[i], mock[i]);
14+
}
15+
}
Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
1+
#include "0200_number_of_islands.h"
12
#include <iostream>
23
#include <vector>
34

45
using std::cout;
56
using std::vector;
67

7-
class Solution {
8-
private:
9-
void dfs(vector<vector<char>> &grid, size_t row, size_t column) {
10-
if (row >= grid.size() || column >= grid[row].size() ||
11-
grid[row][column] == '0') {
12-
return;
13-
}
14-
15-
grid[row][column] = '0';
16-
dfs(grid, row + 1, column);
17-
dfs(grid, row, column + 1);
18-
dfs(grid, row - 1, column);
19-
dfs(grid, row, column - 1);
8+
void dfs(vector<vector<char>> &grid, size_t row, size_t column) {
9+
if (row >= grid.size() || column >= grid[row].size() ||
10+
grid[row][column] == '0') {
11+
return;
2012
}
2113

22-
public:
23-
int numIslands(vector<vector<char>> &grid) {
24-
int islands = 0;
25-
for (int row = 0; row < grid.size(); row++) {
26-
for (int column = 0; column < grid[row].size(); column++) {
27-
if (grid[row][column] == '1') {
28-
islands++;
29-
dfs(grid, row, column);
30-
}
14+
grid[row][column] = '0';
15+
dfs(grid, row + 1, column);
16+
dfs(grid, row, column + 1);
17+
dfs(grid, row - 1, column);
18+
dfs(grid, row, column - 1);
19+
}
20+
21+
int Solution::numIslands(vector<vector<char>> &grid) {
22+
int islands = 0;
23+
for (int row = 0; row < grid.size(); row++) {
24+
for (int column = 0; column < grid[row].size(); column++) {
25+
if (grid[row][column] == '1') {
26+
islands++;
27+
dfs(grid, row, column);
3128
}
3229
}
33-
34-
return islands;
3530
}
36-
};
37-
38-
int main() {
39-
vector<vector<char>> grid = {{'1', '1', '1', '1', '0'},
40-
{'1', '1', '0', '1', '0'},
41-
{'1', '1', '0', '0', '0'},
42-
{'0', '0', '0', '0', '0'}};
4331

44-
cout << Solution().numIslands(grid);
32+
return islands;
4533
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <vector>
2+
3+
using std::vector;
4+
5+
class Solution {
6+
public:
7+
int numIslands(vector<vector<char>> &grid);
8+
}
9+
;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include "cpp/0200_number_of_islands/0200_number_of_islands.h"
2+
#include <gtest/gtest.h>
3+
#include <vector>
4+
5+
using std::vector;
6+
7+
TEST(NumberOfIslands, Example1) {
8+
vector<vector<char>> grid = {{'1', '1', '1', '1', '0'},
9+
{'1', '1', '0', '1', '0'},
10+
{'1', '1', '0', '0', '0'},
11+
{'0', '0', '0', '0', '0'}};
12+
13+
const int result = Solution().numIslands(grid);
14+
15+
EXPECT_EQ(result, 1);
16+
}
17+
18+
TEST(NumberOfIslands, Example2) {
19+
vector<vector<char>> grid = {{'1', '1', '0', '0', '0'},
20+
{'1', '1', '0', '0', '0'},
21+
{'0', '0', '1', '0', '0'},
22+
{'0', '0', '0', '1', '1'}};
23+
24+
const int result = Solution().numIslands(grid);
25+
26+
EXPECT_EQ(result, 3);
27+
}

0 commit comments

Comments
 (0)