Skip to content

Add CI for cpp by Github Actions #1

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 17 commits into from
Feb 20, 2025
  •  
  •  
  •  
19 changes: 19 additions & 0 deletions .github/workflows/ci-cpp.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: C/C++ CI

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Setup Python
uses: actions/[email protected]
- name: make
run: python buildcpp.py
1,017 changes: 1,017 additions & 0 deletions README.md

Large diffs are not rendered by default.

64 changes: 64 additions & 0 deletions buildcpp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import glob
import os

cpps = glob.glob("cpp/*.cpp")
errors = []
for cpp in cpps:
print(cpp)
code = '''
#include <bits/stdc++.h>
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) {}
};
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
ListNode(int x, ListNode *next) : val(x), next(next) {}
};
struct RandomListNode {
int label;
RandomListNode *next, *random;
RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
};
struct Interval {
int start;
int end;
Interval() : start(0), end(0) {}
Interval(int s, int e) : start(s), end(e) {}
};
struct TreeLinkNode {
int val;
TreeLinkNode *left, *right, *next;
TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
};
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
'''
with open(cpp) as f:
code += f.read()
with open("a.cpp", "w") as f:
f.writelines(code)
cmd = "g++ a.cpp -o a.so -shared -fpic -std=c++20"
if 0 != os.system(cmd):
errors.append(cpp)
if len(errors) > 0:
print("Errors in the following files:")
for error in errors:
print(error)
exit(1)
print("All files compiled successfully.")
8 changes: 8 additions & 0 deletions cpp/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
BasedOnStyle: LLVM
IndentWidth: 4
---
Language: Cpp
# Force pointers to the type for C++.
AccessModifierOffset: -4
DerivePointerAlignment: false
PointerAlignment: Left
12 changes: 0 additions & 12 deletions cpp/1.TwoSum.cpp

This file was deleted.

12 changes: 12 additions & 0 deletions cpp/1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
unordered_map<int, int> map;
for (int i = 0; i < nums.size(); ++i) {
if (auto it = map.find(target - nums[i]); it != map.end())
return {it->second, i};
map[nums[i]] = i;
}
return {};
}
};
16 changes: 0 additions & 16 deletions cpp/100.SameTree/recursion-3ms.cpp

This file was deleted.

18 changes: 18 additions & 0 deletions cpp/100.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (p == nullptr || q == nullptr)
return p == q;
return p->val == q->val && isSameTree(p->left, q->left) &&
isSameTree(p->right, q->right);
}
};
18 changes: 0 additions & 18 deletions cpp/1002.FindCommonCharacters.cpp

This file was deleted.

18 changes: 18 additions & 0 deletions cpp/1002.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution { // 8ms 99.37%
public:
vector<string> commonChars(vector<string> &A) {
vector<int> arr(26, numeric_limits<int>::max());
vector<string> res;
for (auto &s : A) {
array<int, 26> cnt = {};
for (auto c : s)
++cnt[c - 'a'];
for (auto i = 0; i < 26; ++i)
arr[i] = min(arr[i], cnt[i]);
}
for (auto i = 0; i < 26; ++i)
while (arr[i] != numeric_limits<int>::max() && arr[i]-- > 0)
res.push_back(string(1, i + 'a'));
return res;
}
};
36 changes: 0 additions & 36 deletions cpp/1008.ConstructBinarySearchTreeFromPreorderTraversal.cpp

This file was deleted.

37 changes: 37 additions & 0 deletions cpp/1008.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
// Runtime: 4 ms, faster than 96.40% of C++ online submissions for Construct
// Binary Search Tree from Preorder Traversal. Memory Usage: 8.7 MB, less
// than 97.42% of C++ online submissions for Construct Binary Search Tree from
// Preorder Traversal.
public:
TreeNode *bstFromPreorder(vector<int> &preorder) {
if (not preorder.size())
return nullptr;
auto r = new TreeNode(preorder[0]);
auto now = r;
vector<TreeNode *> st;
for (int i = 1; i < preorder.size(); ++i) {
auto t = preorder[i];
if (t < now->val) {
st.push_back(now);
now = now->left = new TreeNode(t);
} else {
while (st.size() && t > st.back()->val) {
now = st.back();
st.pop_back();
}
now = now->right = new TreeNode(t);
}
}
return r;
}
};
13 changes: 0 additions & 13 deletions cpp/1009.ComplementofBase10Integer.cpp

This file was deleted.

13 changes: 13 additions & 0 deletions cpp/1009.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution { // 0ms iteration
public:
int bitwiseComplement(int N) {
if (!N)
return 1;
int res = 0, time = 0;
while (N) {
res += (!(N & 1)) << time++;
N >>= 1;
}
return res;
}
};
35 changes: 0 additions & 35 deletions cpp/101.SymmetricTree/iteration.cpp

This file was deleted.

36 changes: 36 additions & 0 deletions cpp/101.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// iteration.cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL)
return 1;
queue<TreeNode*> qe1, qe2;
qe1.push(root->left);
qe2.push(root->right);
while (!qe1.empty() && !qe2.empty()) {
TreeNode* r1 = qe1.front();
TreeNode* r2 = qe2.front();
qe1.pop();
qe2.pop();
if (r1 && r2) {
if (r1->val != r2->val)
return 0;
qe1.push(r1->left);
qe1.push(r1->right);
qe2.push(r2->right);
qe2.push(r2->left);
} else if (r1 != r2)
return 0;
}
return 1;
}
};
Loading
Loading