From 31dbfd3cb11258972316735d2a20180a1b093c22 Mon Sep 17 00:00:00 2001 From: aucker Date: Tue, 11 Jun 2024 18:43:10 +0800 Subject: [PATCH] chore: use shell script to format before commit --- ...o_sum_consecutive_nodes_from_linked_list.c | 40 +++++++++---------- daily/cpp/100_same_tree.cpp | 8 ++-- .../cpp/1155_num_dice_rolls_w_target_sum.cpp | 2 +- .../1457_pseudo_palindromic_paths_in_BT.cpp | 10 ++--- daily/cpp/1609_even_odd_tree.cpp | 8 ++-- .../1642_furthest_building_you_can_reach.cpp | 2 +- daily/cpp/205_isomorphic_strings.cpp | 2 +- daily/cpp/459_repeated_substring_pattern.cpp | 2 +- daily/cpp/559_max_depth_of_nary_tree.cpp | 8 ++-- daily/cpp/739_daily_temperatures.cpp | 4 +- .../Dec-8-Construct-String-from-binary.cpp | 10 ++--- .../Dec-9-Binary-Tree-Inorder-Traversal.cpp | 10 ++--- .../cpp/Nov-15-max-element-dec-rearrange.cpp | 6 +-- daily/cpp/Nov-18-Min-depth-of-tree.cpp | 10 ++--- format.sh | 7 ++++ 15 files changed, 68 insertions(+), 61 deletions(-) create mode 100755 format.sh diff --git a/daily/c/1171_remove_zero_sum_consecutive_nodes_from_linked_list.c b/daily/c/1171_remove_zero_sum_consecutive_nodes_from_linked_list.c index 709a7a5..6361bdc 100644 --- a/daily/c/1171_remove_zero_sum_consecutive_nodes_from_linked_list.c +++ b/daily/c/1171_remove_zero_sum_consecutive_nodes_from_linked_list.c @@ -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 { @@ -40,15 +40,15 @@ 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); @@ -56,33 +56,33 @@ void hashFree(HashItem **obj) { } } -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; } \ No newline at end of file diff --git a/daily/cpp/100_same_tree.cpp b/daily/cpp/100_same_tree.cpp index 31d691a..8cd10f1 100644 --- a/daily/cpp/100_same_tree.cpp +++ b/daily/cpp/100_same_tree.cpp @@ -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) { diff --git a/daily/cpp/1155_num_dice_rolls_w_target_sum.cpp b/daily/cpp/1155_num_dice_rolls_w_target_sum.cpp index f886d9e..d7ef895 100644 --- a/daily/cpp/1155_num_dice_rolls_w_target_sum.cpp +++ b/daily/cpp/1155_num_dice_rolls_w_target_sum.cpp @@ -11,7 +11,7 @@ class Solution { private: int mod = 1e9 + 7; - int solve(int n, int &k, int target, vector> &dp) { + int solve(int n, int& k, int target, vector>& dp) { if (n == 0 && target == 0) return 1; if (n <= 0 || target <= 0) return 0; diff --git a/daily/cpp/1457_pseudo_palindromic_paths_in_BT.cpp b/daily/cpp/1457_pseudo_palindromic_paths_in_BT.cpp index f164188..70e9610 100644 --- a/daily/cpp/1457_pseudo_palindromic_paths_in_BT.cpp +++ b/daily/cpp/1457_pseudo_palindromic_paths_in_BT.cpp @@ -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); diff --git a/daily/cpp/1609_even_odd_tree.cpp b/daily/cpp/1609_even_odd_tree.cpp index 77cb498..f2c88f9 100644 --- a/daily/cpp/1609_even_odd_tree.cpp +++ b/daily/cpp/1609_even_odd_tree.cpp @@ -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 q; + bool isEvenOddTree(TreeNode* root) { + queue q; int level = 0; q.push(root); @@ -23,7 +23,7 @@ class Solution { : numeric_limits::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)) || diff --git a/daily/cpp/1642_furthest_building_you_can_reach.cpp b/daily/cpp/1642_furthest_building_you_can_reach.cpp index fa482f1..a2fd277 100644 --- a/daily/cpp/1642_furthest_building_you_can_reach.cpp +++ b/daily/cpp/1642_furthest_building_you_can_reach.cpp @@ -3,7 +3,7 @@ using namespace std; class Solution { public: - int furthestBuilding(vector &heights, int bricks, int ladders) { + int furthestBuilding(vector& heights, int bricks, int ladders) { // Priority Queue for storing the bricks used in decreasing order priority_queue pq; diff --git a/daily/cpp/205_isomorphic_strings.cpp b/daily/cpp/205_isomorphic_strings.cpp index b8f2c21..84cb3b4 100644 --- a/daily/cpp/205_isomorphic_strings.cpp +++ b/daily/cpp/205_isomorphic_strings.cpp @@ -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) {} }; diff --git a/daily/cpp/459_repeated_substring_pattern.cpp b/daily/cpp/459_repeated_substring_pattern.cpp index 9c4a13b..4ce99f0 100644 --- a/daily/cpp/459_repeated_substring_pattern.cpp +++ b/daily/cpp/459_repeated_substring_pattern.cpp @@ -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 lps(m, -1); diff --git a/daily/cpp/559_max_depth_of_nary_tree.cpp b/daily/cpp/559_max_depth_of_nary_tree.cpp index 957080c..5b86a85 100644 --- a/daily/cpp/559_max_depth_of_nary_tree.cpp +++ b/daily/cpp/559_max_depth_of_nary_tree.cpp @@ -4,13 +4,13 @@ using namespace std; class Node { public: int val; - vector children; + vector children; Node() {} Node(int _val) { val = _val; } - Node(int _val, vector _children) { + Node(int _val, vector _children) { val = _val; children = _children; } @@ -18,11 +18,11 @@ class Node { 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; diff --git a/daily/cpp/739_daily_temperatures.cpp b/daily/cpp/739_daily_temperatures.cpp index 8fbeee0..13cbeec 100644 --- a/daily/cpp/739_daily_temperatures.cpp +++ b/daily/cpp/739_daily_temperatures.cpp @@ -3,7 +3,7 @@ using namespace std; class Solution { public: - vector temperature(vector &T) { + vector temperature(vector& T) { vector res(T.size(), 0); stack st; for (int i = 0; i < T.size(); ++i) { @@ -28,7 +28,7 @@ class Solution { }; int main() { - Solution *s = new Solution(); + Solution* s = new Solution(); s->test1(); return 0; } \ No newline at end of file diff --git a/daily/cpp/Dec-8-Construct-String-from-binary.cpp b/daily/cpp/Dec-8-Construct-String-from-binary.cpp index 707bc48..eb5b329 100644 --- a/daily/cpp/Dec-8-Construct-String-from-binary.cpp +++ b/daily/cpp/Dec-8-Construct-String-from-binary.cpp @@ -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); diff --git a/daily/cpp/Dec-9-Binary-Tree-Inorder-Traversal.cpp b/daily/cpp/Dec-9-Binary-Tree-Inorder-Traversal.cpp index 0312be1..dea4853 100644 --- a/daily/cpp/Dec-9-Binary-Tree-Inorder-Traversal.cpp +++ b/daily/cpp/Dec-9-Binary-Tree-Inorder-Traversal.cpp @@ -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 inorderTraversal(TreeNode *root) { + vector inorderTraversal(TreeNode* root) { vector res; dfs(root, res); return res; } - void dfs(TreeNode *node, vector &res) { + void dfs(TreeNode* node, vector& res) { if (!node) return; if (node->left || node->right) { diff --git a/daily/cpp/Nov-15-max-element-dec-rearrange.cpp b/daily/cpp/Nov-15-max-element-dec-rearrange.cpp index 73467e6..f39b77e 100644 --- a/daily/cpp/Nov-15-max-element-dec-rearrange.cpp +++ b/daily/cpp/Nov-15-max-element-dec-rearrange.cpp @@ -5,16 +5,16 @@ using namespace std; class Solution { public: - int maximumElementAfterDecrementingAndRearranging(vector &arr) { + int maximumElementAfterDecrementingAndRearranging(vector& 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 &arr) { + int maxEleAfterDecARe(vector& arr) { // optimized version with O(n) int l = arr.size(); std::vector counter(l, 0); diff --git a/daily/cpp/Nov-18-Min-depth-of-tree.cpp b/daily/cpp/Nov-18-Min-depth-of-tree.cpp index 46dfbac..1aff18c 100644 --- a/daily/cpp/Nov-18-Min-depth-of-tree.cpp +++ b/daily/cpp/Nov-18-Min-depth-of-tree.cpp @@ -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)); diff --git a/format.sh b/format.sh new file mode 100755 index 0000000..53b2fd8 --- /dev/null +++ b/format.sh @@ -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