Skip to content

Commit

Permalink
chore: use shell script to format before commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aucker committed Jun 11, 2024
1 parent ef649fd commit 31dbfd3
Show file tree
Hide file tree
Showing 15 changed files with 68 additions and 61 deletions.
40 changes: 20 additions & 20 deletions daily/c/1171_remove_zero_sum_consecutive_nodes_from_linked_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@

struct ListNode {
int val;
struct ListNode *next;
struct ListNode* next;
};

typedef struct {
int key;
struct ListNode *val;
struct ListNode* val;
UT_hash_handle hh;
} HashItem;

HashItem *hashFindItem(HashItem **obj, int key) {
HashItem *pEntry = NULL;
HashItem* hashFindItem(HashItem** obj, int key) {
HashItem* pEntry = NULL;
HASH_FIND_INT(*obj, &key, pEntry);
return pEntry;
}

bool hashAddItem(HashItem **obj, int key, const struct ListNode *val) {
bool hashAddItem(HashItem** obj, int key, const struct ListNode* val) {
if (hashFindItem(obj, key)) {
return false;
}
HashItem *pEntry = (HashItem *)malloc(sizeof(HashItem));
HashItem* pEntry = (HashItem*)malloc(sizeof(HashItem));
pEntry->key = key;
pEntry->val = val;
HASH_ADD_INT(*obj, key, pEntry);
return true;
}

bool hashSetItem(HashItem **obj, int key, const struct ListNode *val) {
HashItem *pEntry = hashFindItem(obj, key);
bool hashSetItem(HashItem** obj, int key, const struct ListNode* val) {
HashItem* pEntry = hashFindItem(obj, key);
if (!pEntry) {
hashAddItem(obj, key, val);
} else {
Expand All @@ -40,49 +40,49 @@ bool hashSetItem(HashItem **obj, int key, const struct ListNode *val) {
return true;
}

struct ListNode *hashGetItem(HashItem **obj, int key) {
HashItem *pEntry = hashFindItem(obj, key);
struct ListNode* hashGetItem(HashItem** obj, int key) {
HashItem* pEntry = hashFindItem(obj, key);
if (!pEntry) {
return NULL;
}
return pEntry->val;
}

void hashFree(HashItem **obj) {
void hashFree(HashItem** obj) {
HashItem *curr = NULL, *tmp = NULL;
HASH_ITER(hh, *obj, curr, tmp) {
HASH_DEL(*obj, curr);
free(curr);
}
}

struct ListNode *createListNode(int val) {
struct ListNode *obj = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode* createListNode(int val) {
struct ListNode* obj = (struct ListNode*)malloc(sizeof(struct ListNode));
obj->val = val;
obj->next = NULL;
return obj;
}

struct ListNode *removeZeroSumSublists(struct ListNode *head) {
struct ListNode *dummy = createListNode(0);
struct ListNode* removeZeroSumSublists(struct ListNode* head) {
struct ListNode* dummy = createListNode(0);
dummy->next = head;
int prefix = 0;
HashItem *seen = NULL;
for (struct ListNode *node = dummy; node; node = node->next) {
HashItem* seen = NULL;
for (struct ListNode* node = dummy; node; node = node->next) {
prefix += node->val;
hashSetItem(&seen, prefix, node);
}
prefix = 0;
for (struct ListNode *node = dummy; node; node = node->next) {
for (struct ListNode* node = dummy; node; node = node->next) {
prefix += node->val;
struct ListNode *curr = hashGetItem(&seen, prefix);
struct ListNode* curr = hashGetItem(&seen, prefix);
if (curr) {
node->next = curr->next;
}
}

hashFree(&seen);
struct ListNode *ret = dummy->next;
struct ListNode* ret = dummy->next;
free(dummy);
return ret;
}
8 changes: 4 additions & 4 deletions daily/cpp/100_same_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
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)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
bool isSameTree(TreeNode* p, TreeNode* q) {
if (p == nullptr && q == nullptr) return true;
if (p == nullptr || q == nullptr) return false;
if (p->val == q->val) {
Expand Down
2 changes: 1 addition & 1 deletion daily/cpp/1155_num_dice_rolls_w_target_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Solution {
private:
int mod = 1e9 + 7;

int solve(int n, int &k, int target, vector<vector<int>> &dp) {
int solve(int n, int& k, int target, vector<vector<int>>& dp) {
if (n == 0 && target == 0) return 1;
if (n <= 0 || target <= 0) return 0;

Expand Down
10 changes: 5 additions & 5 deletions daily/cpp/1457_pseudo_palindromic_paths_in_BT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ using namespace std;

struct TreeNode {
int root;
TreeNode *left;
TreeNode *right;
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)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
}

class Solution {
public:
int pseudoPalindromicPaths(TreeNode *root) { return cntPseudoPaths(root, 0); }
int pseudoPalindromicPaths(TreeNode* root) { return cntPseudoPaths(root, 0); }

private:
int cntPseudoPaths(TreeNode *node, int path) {
int cntPseudoPaths(TreeNode* node, int path) {
if (!node) return 0;

path ^= (1 << node->val);
Expand Down
8 changes: 4 additions & 4 deletions daily/cpp/1609_even_odd_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ struct TreeNode {
TreeNode *left, *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

class Solution {
public:
bool isEvenOddTree(TreeNode *root) {
queue<TreeNode *> q;
bool isEvenOddTree(TreeNode* root) {
queue<TreeNode*> q;
int level = 0;
q.push(root);

Expand All @@ -23,7 +23,7 @@ class Solution {
: numeric_limits<int>::max();

for (int i = 0; i < size; ++i) {
TreeNode *node = q.front();
TreeNode* node = q.front();
q.pop();

if ((level % 2 == 0 && (node->val % 2 == 0 || node->val <= prev)) ||
Expand Down
2 changes: 1 addition & 1 deletion daily/cpp/1642_furthest_building_you_can_reach.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using namespace std;

class Solution {
public:
int furthestBuilding(vector<int> &heights, int bricks, int ladders) {
int furthestBuilding(vector<int>& heights, int bricks, int ladders) {
// Priority Queue for storing the bricks used in decreasing order
priority_queue<int> pq;

Expand Down
2 changes: 1 addition & 1 deletion daily/cpp/205_isomorphic_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct TreeNode {
TreeNode *left, *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

Expand Down
2 changes: 1 addition & 1 deletion daily/cpp/459_repeated_substring_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Solution {
}

private:
bool kmp(const string &query, const string &pattern) {
bool kmp(const string& query, const string& pattern) {
int n = query.size();
int m = pattern.size();
vector<int> lps(m, -1);
Expand Down
8 changes: 4 additions & 4 deletions daily/cpp/559_max_depth_of_nary_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ using namespace std;
class Node {
public:
int val;
vector<Node *> children;
vector<Node*> children;

Node() {}

Node(int _val) { val = _val; }

Node(int _val, vector<Node *> _children) {
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};

class Solution {
public:
int maxDepth(Node *root) {
int maxDepth(Node* root) {
// bfs
if (!root) return 0;
int depth = 0;
for (Node *cur : root->children) {
for (Node* cur : root->children) {
depth = max(depth, maxDepth(cur));
}
return depth + 1;
Expand Down
4 changes: 2 additions & 2 deletions daily/cpp/739_daily_temperatures.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ using namespace std;

class Solution {
public:
vector<int> temperature(vector<int> &T) {
vector<int> temperature(vector<int>& T) {
vector<int> res(T.size(), 0);
stack<int> st;
for (int i = 0; i < T.size(); ++i) {
Expand All @@ -28,7 +28,7 @@ class Solution {
};

int main() {
Solution *s = new Solution();
Solution* s = new Solution();
s->test1();
return 0;
}
10 changes: 5 additions & 5 deletions daily/cpp/Dec-8-Construct-String-from-binary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
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)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

class Solution {
public:
string tree2str(TreeNode *root) {
string tree2str(TreeNode* root) {
string res = "";
check(root, res);
return res;
}

void check(TreeNode *root, string &str) {
void check(TreeNode* root, string& str) {
if (!root) return;

str += to_string(root->val);
Expand Down
10 changes: 5 additions & 5 deletions daily/cpp/Dec-9-Binary-Tree-Inorder-Traversal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
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)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

class Solution {
public:
vector<int> inorderTraversal(TreeNode *root) {
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
dfs(root, res);
return res;
}

void dfs(TreeNode *node, vector<int> &res) {
void dfs(TreeNode* node, vector<int>& res) {
if (!node) return;

if (node->left || node->right) {
Expand Down
6 changes: 3 additions & 3 deletions daily/cpp/Nov-15-max-element-dec-rearrange.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
using namespace std;
class Solution {
public:
int maximumElementAfterDecrementingAndRearranging(vector<int> &arr) {
int maximumElementAfterDecrementingAndRearranging(vector<int>& arr) {
// the result is no larger than the max value in the array
sort(arr.begin(), arr.end());
int pre = 0;
for (int &a : arr) pre = min(pre + 1, a);
for (int& a : arr) pre = min(pre + 1, a);
return pre;
}
// sort() time complexity is O(nlogn)

int maxEleAfterDecARe(vector<int> &arr) {
int maxEleAfterDecARe(vector<int>& arr) {
// optimized version with O(n)
int l = arr.size();
std::vector<int> counter(l, 0);
Expand Down
10 changes: 5 additions & 5 deletions daily/cpp/Nov-18-Min-depth-of-tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
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)
TreeNode(int x, TreeNode* left, TreeNode* right)
: val(x), left(left), right(right) {}
};

class Solution {
public:
int minDepth(TreeNode *root) {
int minDepth(TreeNode* root) {
int res = recur(root);
return res == INT_MAX ? 0 : res;
}

int recur(TreeNode *curr) {
int recur(TreeNode* curr) {
if (!curr) return INT_MAX;
if (!curr->left && !curr->right) return 1;
return 1 + min(recur(curr->left), recur(curr->right));
Expand Down
7 changes: 7 additions & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh

PROJECT_DIR="./daily"

find $PROJECT_DIR -name "*.cpp" -o -name "*.c" -o -name "*.h" | xargs clang-format -i

git add --all

0 comments on commit 31dbfd3

Please sign in to comment.