Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@
*.exe
*.out
*.app

# Ignore all bazel-* symlinks. There is no full list since this can change
# based on the name of the directory bazel is cloned into.
bazel-*
Empty file added BUILD
Empty file.
20 changes: 20 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

http_archive(
name = "com_google_googletest",
urls = ["https://github.com/google/googletest/archive/609281088cfefc76f9d0ce82e1ff6c30cc3591e5.zip"],
strip_prefix = "googletest-609281088cfefc76f9d0ce82e1ff6c30cc3591e5",
)

http_archive(
name = "rules_cc",
urls = ["https://github.com/bazelbuild/rules_cc/archive/40548a2974f1aea06215272d9c2b47a14a24e556.zip"],
strip_prefix = "rules_cc-40548a2974f1aea06215272d9c2b47a14a24e556",
)

git_repository(
name = "com_google_absl",
remote = "https://github.com/abseil/abseil-cpp.git",
tag = "20200225.2",
)
1 change: 1 addition & 0 deletions bazel-leetcode
30 changes: 10 additions & 20 deletions cpp/0001_two_sum/0001_two_sum.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "0001_two_sum.h"
#include <iostream>
#include <unordered_map>
#include <vector>
Expand All @@ -6,29 +7,18 @@ using std::cout;
using std::unordered_map;
using std::vector;

class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target) {
unordered_map<int, int> umap;
for (int i = 0; i < nums.size(); i++) {
const int current = nums[i];
vector<int> Solution::twoSum(vector<int> &nums, int target) {
unordered_map<int, int> umap;
for (int i = 0; i < nums.size(); i++) {
const int current = nums[i];

if (umap.count(current) > 0) {
return {i, umap[current]};
}

const int diff = target - nums[i];
umap[diff] = i;
if (umap.count(current) > 0) {
return {i, umap[current]};
}

return {};
const int diff = target - nums[i];
umap[diff] = i;
}
};

int main() {
vector<int> nums = {2, 7, 11, 15};
auto result = Solution().twoSum(nums, 9);
for (auto n : result) {
cout << n << " ";
}
return {};
}
8 changes: 8 additions & 0 deletions cpp/0001_two_sum/0001_two_sum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <vector>

using std::vector;

class Solution {
public:
vector<int> twoSum(vector<int> &nums, int target);
};
15 changes: 13 additions & 2 deletions cpp/0001_two_sum/0001_two_sum_test.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
//#include <gtest/gtest.h>
#include <0001_two_sum>
#include "cpp/0001_two_sum/0001_two_sum.h"
#include <gtest/gtest.h>
#include <vector>

using std::vector;

TEST(TwoSumTest, Example1) {
vector<int> nums = {2, 7, 11, 15};
const vector<int> mock = Solution().twoSum(nums, 9);

const vector<int> solution = {1, 0};
for (int i = 0; i < mock.size(); ++i) {
EXPECT_EQ(solution[i], mock[i]);
}
}
52 changes: 20 additions & 32 deletions cpp/0200_number_of_islands/0200_number_of_islands.cc
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
#include "0200_number_of_islands.h"
#include <iostream>
#include <vector>

using std::cout;
using std::vector;

class Solution {
private:
void dfs(vector<vector<char>> &grid, size_t row, size_t column) {
if (row >= grid.size() || column >= grid[row].size() ||
grid[row][column] == '0') {
return;
}

grid[row][column] = '0';
dfs(grid, row + 1, column);
dfs(grid, row, column + 1);
dfs(grid, row - 1, column);
dfs(grid, row, column - 1);
void dfs(vector<vector<char>> &grid, size_t row, size_t column) {
if (row >= grid.size() || column >= grid[row].size() ||
grid[row][column] == '0') {
return;
}

public:
int numIslands(vector<vector<char>> &grid) {
int islands = 0;
for (int row = 0; row < grid.size(); row++) {
for (int column = 0; column < grid[row].size(); column++) {
if (grid[row][column] == '1') {
islands++;
dfs(grid, row, column);
}
grid[row][column] = '0';
dfs(grid, row + 1, column);
dfs(grid, row, column + 1);
dfs(grid, row - 1, column);
dfs(grid, row, column - 1);
}

int Solution::numIslands(vector<vector<char>> &grid) {
int islands = 0;
for (int row = 0; row < grid.size(); row++) {
for (int column = 0; column < grid[row].size(); column++) {
if (grid[row][column] == '1') {
islands++;
dfs(grid, row, column);
}
}

return islands;
}
};

int main() {
vector<vector<char>> grid = {{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'}};

cout << Solution().numIslands(grid);
return islands;
}
9 changes: 9 additions & 0 deletions cpp/0200_number_of_islands/0200_number_of_islands.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <vector>

using std::vector;

class Solution {
public:
int numIslands(vector<vector<char>> &grid);
}
;
27 changes: 27 additions & 0 deletions cpp/0200_number_of_islands/0200_number_of_islands_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "cpp/0200_number_of_islands/0200_number_of_islands.h"
#include <gtest/gtest.h>
#include <vector>

using std::vector;

TEST(NumberOfIslands, Example1) {
vector<vector<char>> grid = {{'1', '1', '1', '1', '0'},
{'1', '1', '0', '1', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '0', '0', '0'}};

const int result = Solution().numIslands(grid);

EXPECT_EQ(result, 1);
}

TEST(NumberOfIslands, Example2) {
vector<vector<char>> grid = {{'1', '1', '0', '0', '0'},
{'1', '1', '0', '0', '0'},
{'0', '0', '1', '0', '0'},
{'0', '0', '0', '1', '1'}};

const int result = Solution().numIslands(grid);

EXPECT_EQ(result, 3);
}
21 changes: 21 additions & 0 deletions cpp/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package(default_visibility = ["//visibility:public"])
load("@rules_cc//cc:defs.bzl", "cc_test")

cc_library(
name="exercises",
hdrs=glob(["**/**/*.h"]),
srcs = glob(
["**/**/*.cc"],
exclude = ["**/**/*_test.cc"]
),
visibility = ["//visibility:public"],
)

cc_test(
name = "tests",
srcs = glob(["**/**/*_test.cc"]),
deps = [
":exercises",
"@com_google_googletest//:gtest_main"
],
)
8 changes: 8 additions & 0 deletions typescript/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
exports_files(["tsconfig.json"])

ts_library(
name = "leetcode_typescript",
srcs = glob(["**/*.ts"]),
deps = [
],
)