Skip to content

GH-169: Solve leetcode 101 #182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 14, 2023
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
6 changes: 4 additions & 2 deletions collections/leetcode/README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Leetcode

Leetcode Solutions
**Leetcode Solutions**

* 🟢 Easy
* 🟡 Medium
* 🔴 Hard

**Problems**

<table>
<tr><td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode1'>1</a></td>
<td>2</td>
Expand Down Expand Up @@ -117,7 +119,7 @@ Leetcode Solutions
<td>99</td>
<td>100</td>
<tr>
<td>101</td>
<td>🟢&nbsp;<a href='https://github.com/rain1024/datastructures-algorithms-competitive-programming/tree/main/problems/leetcode101'>101</a></td>
<td>102</td>
<td>103</td>
<td>104</td>
Expand Down
3 changes: 3 additions & 0 deletions collections/leetcode/data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ problems:
- name: 78
languages: cpp
level: medium
- name: 101
languages: cpp
level: easy
- name: 142
languages: cpp
level: medium
Expand Down
2 changes: 2 additions & 0 deletions problems/leetcode101/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
solution
*.dSYM
19 changes: 19 additions & 0 deletions problems/leetcode101/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Leetcode Problem

## Usage

Run program with an example

```
bazel run cpp/main:solution < data/1.in
```

Test program

```
# Run all tests
bazel test --test_output=all tests:solution_test
bazel test --test_output=all --cache_test_results=no tests:solution_test
# Run test with pecific test id
bazel test --test_output=all tests:solution_test --test_arg=1
```
86 changes: 86 additions & 0 deletions problems/leetcode101/cpp/main/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#include "solution.h"

#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

TreeNode* parse_tree(string s) {
// parse tree from string with format
// 1 2 3 4 5 6 null 7
string value = "";
vector<TreeNode*> nodes;
TreeNode* node;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ' ') {
if (value == "null") {
node = nullptr;
} else {
node = new TreeNode(stoi(value));
}
value = "";
nodes.push_back(node);
} else {
value += s[i];
}
}
if (value == "null") {
node = nullptr;
} else {
node = new TreeNode(stoi(value));
}
nodes.push_back(node);
int n = nodes.size();
queue<TreeNode*> q;
TreeNode* head = nodes[0];
q.push(head);
int i = 1;
while (!q.empty() && i < n) {
node = q.front();
q.pop();
node->left = nodes[i];
node->right = nodes[i + 1];
q.push(nodes[i]);
q.push(nodes[i + 1]);
i += 2;
}
return head;
}

int main() {
ios::sync_with_stdio(false);
cin.tie(0);

bool is_use_file = false;

filesystem::path filepath =
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
ifstream file(filepath);

if (is_use_file) {
cin.rdbuf(file.rdbuf()); // redirect cin to file
}

// get input
string s;
getline(cin, s);

cout << "s = " << s << endl;

TreeNode* root = parse_tree(s);

// TreeNode* root = new TreeNode(0);

Solution solution;
bool output = solution.isSymmetric(root);

// print output
cout << output << endl;

return 0;
}
72 changes: 72 additions & 0 deletions problems/leetcode101/cpp/main/solution.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
: val(x), left(left), right(right) {}
};

class Solution {
public:
bool isEquals(vector<TreeNode *> lefts, vector<TreeNode *> rights) {
if (lefts.size() != rights.size()) {
return false;
}
int n = lefts.size();
TreeNode *left, *right;
for (int i = 0; i < lefts.size(); i++) {
left = lefts[i];
right = rights[n - 1 - i];
if (left == nullptr && right == nullptr) {
continue;
} else if (left != nullptr && right != nullptr) {
if (left->val != right->val) {
return false;
}
} else {
return false;
}
}
vector<TreeNode *> left_nexts, right_nexts;
int num_not_nulls = 0;
for (int i = 0; i < lefts.size(); i++) {
left = lefts[i];
right = rights[i];
if (left != nullptr) {
num_not_nulls += 1;
left_nexts.push_back(left->left);
left_nexts.push_back(left->right);
}
if (right != nullptr) {
right_nexts.push_back(right->left);
right_nexts.push_back(right->right);
}
}
if (num_not_nulls > 0) {
return isEquals(left_nexts, right_nexts);
} else {
return true;
}
}

bool isSymmetric(TreeNode *root) {
if (!root) {
return true;
}
return isEquals({root->left}, {root->right});
}
};
1 change: 1 addition & 0 deletions problems/leetcode101/data/1.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 2 3 4 4 3
1 change: 1 addition & 0 deletions problems/leetcode101/data/1.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
1 change: 1 addition & 0 deletions problems/leetcode101/data/2.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1 2 2 null 3 null 3
1 change: 1 addition & 0 deletions problems/leetcode101/data/2.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
9 changes: 9 additions & 0 deletions problems/leetcode101/problem_infos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "Leetcode 78. Subsets",
"link": "https://leetcode.com/problems/subsets/",
"tags": [
"Array",
"Backtracking",
"Bit Manipulation"
]
}
3 changes: 2 additions & 1 deletion tools/craft/templates/leetcode/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
solution
solution
*.dSYM
2 changes: 1 addition & 1 deletion tools/craft/templates/leetcode/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Run program with an example

```
bazel run src/main:solution < data/1.in
bazel run cpp/main:solution < data/1.in
```

Test program
Expand Down
7 changes: 7 additions & 0 deletions tools/craft/templates/leetcode/cpp/main/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_cc//cc:defs.bzl", "cc_binary")

cc_binary(
name = "solution",
srcs = ["solution.cpp", "solution.h"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <map>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

Expand All @@ -16,7 +17,7 @@ int main() {
bool is_use_file = false;

filesystem::path filepath =
filesystem::current_path().parent_path().parent_path() / "data" / "2.in";
filesystem::current_path().parent_path().parent_path() / "data" / "1.in";
ifstream file(filepath);

if (is_use_file) {
Expand Down
11 changes: 11 additions & 0 deletions tools/craft/templates/leetcode/cpp/tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_test")

cc_test(
name = "solution_test",
srcs = ["solution_test.cpp"],
deps = [
"//problems/codeforcesAA/src/main:solution",
"@com_google_googletest//:gtest_main",
],
data = ["data"]
)
97 changes: 97 additions & 0 deletions tools/craft/templates/leetcode/cpp/tests/solution_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>
#include <string_view>
#include <vector>

#include "gtest/gtest.h"

namespace fs = std::filesystem;

using namespace std;

std::string TEST_ID = "";

vector<string> split(string s, string delimiter) {
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
string token;
vector<string> res;

while ((pos_end = s.find(delimiter, pos_start)) != string::npos) {
token = s.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delim_len;
res.push_back(token);
}

res.push_back(s.substr(pos_start));
return res;
}

std::string GetHello(std::string_view in) {
if (in.size() == 0) {
return std::string("hello, word");
} else {
return std::string("Hello, ") + in.data();
}
}

string ReadFile(const std::string &filename) {
std::ifstream f(filename);
string s((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
return s;
}

vector<string> RunProgram(string fileid) {
vector<string> v;
string program = "problems/codeforcesAA/src/main/solution";
string test_folder = "problems/codeforcesAA/tests/data/";
string command = program + " < " + test_folder + fileid + ".in > output.txt";
std::system(command.c_str());
string actual = ReadFile("output.txt");
string expectedOutputFile = test_folder + fileid + ".out";
string expected = ReadFile(expectedOutputFile);
v.push_back(actual);
v.push_back(expected);
return v;
}

TEST(RunTest, AllTestCases) {
if (TEST_ID != "") {
std::cout << "TEST_ID = " << TEST_ID << std::endl;
} else {
std::cout << "RUN ALL TESTS" << std::endl;
}

std::string test_data_folder = "problems/codeforcesAA/tests/data";
for (const auto &entry : fs::directory_iterator(test_data_folder)) {
string filename = split(entry.path(), "/").back();
vector<string> v = split(filename, ".");
string fileid = v[0];
string extension = v[1];
if (extension != "in"){
continue;
}
if (TEST_ID != ""){
if (fileid != TEST_ID){
continue;
}
}
// Run Test Case
vector<string> output = RunProgram(fileid);
string actual = output[0];
string expected = output[1];
string message = "❌ FAIL CASE: " + fileid;
ASSERT_EQ(actual, expected) << message;
}
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
if (argc == 2) {
TEST_ID = argv[1];
}
return RUN_ALL_TESTS();
}