diff --git a/.clang-format b/.clang-format index 293451ddcfb19..01e9dfcb045c4 100644 --- a/.clang-format +++ b/.clang-format @@ -12,12 +12,12 @@ AlignTrailingComments: false AllowAllArgumentsOnNextLine: true AllowAllConstructorInitializersOnNextLine: true AllowAllParametersOfDeclarationOnNextLine: true -AllowShortBlocksOnASingleLine: Empty -AllowShortCaseLabelsOnASingleLine: false +AllowShortBlocksOnASingleLine: Always +AllowShortCaseLabelsOnASingleLine: true AllowShortFunctionsOnASingleLine: All AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: true -AllowShortLoopsOnASingleLine: false +AllowShortLoopsOnASingleLine: true AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakAfterReturnType: None AlwaysBreakBeforeMultilineStrings: false @@ -56,7 +56,7 @@ CompactNamespaces: false ConstructorInitializerAllOnOneLineOrOnePerLine: false ConstructorInitializerIndentWidth: 4 ContinuationIndentWidth: 4 -Cpp11BracedListStyle: false +Cpp11BracedListStyle: true DeriveLineEnding: true DerivePointerAlignment: false DisableFormat: false diff --git a/basic/sorting/BubbleSort/BubbleSort.cpp b/basic/sorting/BubbleSort/BubbleSort.cpp index d0b2d66f00dd8..276e1dbb6b0d2 100644 --- a/basic/sorting/BubbleSort/BubbleSort.cpp +++ b/basic/sorting/BubbleSort/BubbleSort.cpp @@ -3,17 +3,12 @@ using namespace std; - -void bubbleSort(vector& arr) -{ +void bubbleSort(vector& arr) { int n = arr.size(); - for (int i = 0; i < n - 1; ++i) - { + for (int i = 0; i < n - 1; ++i) { bool change = false; - for (int j = 0; j < n - i - 1; ++j) - { - if (arr[j] > arr[j + 1]) - { + for (int j = 0; j < n - i - 1; ++j) { + if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); change = true; } @@ -22,8 +17,7 @@ void bubbleSort(vector& arr) } } -int main() -{ +int main() { vector arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; bubbleSort(arr); for (int v : arr) cout << v << " "; diff --git a/basic/sorting/BubbleSort/README.md b/basic/sorting/BubbleSort/README.md index e14b5dc1755c8..e1f517022f31b 100644 --- a/basic/sorting/BubbleSort/README.md +++ b/basic/sorting/BubbleSort/README.md @@ -104,17 +104,12 @@ func main() { using namespace std; - -void bubbleSort(vector& arr) -{ +void bubbleSort(vector& arr) { int n = arr.size(); - for (int i = 0; i < n - 1; ++i) - { + for (int i = 0; i < n - 1; ++i) { bool change = false; - for (int j = 0; j < n - i - 1; ++j) - { - if (arr[j] > arr[j + 1]) - { + for (int j = 0; j < n - i - 1; ++j) { + if (arr[j] > arr[j + 1]) { swap(arr[j], arr[j + 1]); change = true; } @@ -123,8 +118,7 @@ void bubbleSort(vector& arr) } } -int main() -{ +int main() { vector arr = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; bubbleSort(arr); for (int v : arr) cout << v << " "; diff --git a/basic/sorting/InsertionSort/InsertionSort.cpp b/basic/sorting/InsertionSort/InsertionSort.cpp index 40d913cd24d77..3515dc76c6642 100644 --- a/basic/sorting/InsertionSort/InsertionSort.cpp +++ b/basic/sorting/InsertionSort/InsertionSort.cpp @@ -3,11 +3,9 @@ using namespace std; -void printvec(const vector &vec, const string &strbegin = "", const string &strend = "") -{ +void printvec(const vector& vec, const string& strbegin = "", const string& strend = "") { cout << strbegin << endl; - for (auto val : vec) - { + for (auto val : vec) { cout << val << "\t"; } @@ -15,14 +13,11 @@ void printvec(const vector &vec, const string &strbegin = "", const string cout << strend << endl; } -void insertsort(vector &vec) -{ - for (int i = 1; i < vec.size(); i++) - { +void insertsort(vector& vec) { + for (int i = 1; i < vec.size(); i++) { int j = i - 1; int num = vec[i]; - for (; j >= 0 && vec[j] > num; j--) - { + for (; j >= 0 && vec[j] > num; j--) { vec[j + 1] = vec[j]; } @@ -32,8 +27,7 @@ void insertsort(vector &vec) return; } -int main() -{ +int main() { vector vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; printvec(vec); insertsort(vec); diff --git a/basic/sorting/InsertionSort/README.md b/basic/sorting/InsertionSort/README.md index 458c412751207..ccdfdfce8ea9a 100644 --- a/basic/sorting/InsertionSort/README.md +++ b/basic/sorting/InsertionSort/README.md @@ -95,11 +95,9 @@ func main() { using namespace std; -void printvec(const vector &vec, const string &strbegin = "", const string &strend = "") -{ +void printvec(const vector& vec, const string& strbegin = "", const string& strend = "") { cout << strbegin << endl; - for (auto val : vec) - { + for (auto val : vec) { cout << val << "\t"; } @@ -107,14 +105,11 @@ void printvec(const vector &vec, const string &strbegin = "", const string cout << strend << endl; } -void insertsort(vector &vec) -{ - for (int i = 1; i < vec.size(); i++) - { +void insertsort(vector& vec) { + for (int i = 1; i < vec.size(); i++) { int j = i - 1; int num = vec[i]; - for (; j >= 0 && vec[j] > num; j--) - { + for (; j >= 0 && vec[j] > num; j--) { vec[j + 1] = vec[j]; } @@ -124,8 +119,7 @@ void insertsort(vector &vec) return; } -int main() -{ +int main() { vector vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; printvec(vec); insertsort(vec); diff --git a/basic/sorting/MergeSort/Main.cpp b/basic/sorting/MergeSort/Main.cpp index 51e18b28e5c7c..b7a64c234765e 100644 --- a/basic/sorting/MergeSort/Main.cpp +++ b/basic/sorting/MergeSort/Main.cpp @@ -8,25 +8,24 @@ int n; int nums[N]; int tmp[N]; -void merge_sort(int nums[], int left, int right) -{ +void merge_sort(int nums[], int left, int right) { if (left >= right) return; int mid = (left + right) >> 1; merge_sort(nums, left, mid); merge_sort(nums, mid + 1, right); int i = left, j = mid + 1, k = 0; - while (i <= mid && j <= right) - { - if (nums[i] <= nums[j]) tmp[k++] = nums[i++]; - else tmp[k++] = nums[j++]; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) + tmp[k++] = nums[i++]; + else + tmp[k++] = nums[j++]; } while (i <= mid) tmp[k++] = nums[i++]; while (j <= right) tmp[k++] = nums[j++]; for (i = left, j = 0; i <= right; ++i, ++j) nums[i] = tmp[j]; } -int main() -{ +int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &nums[i]); diff --git a/basic/sorting/MergeSort/README.md b/basic/sorting/MergeSort/README.md index 7e116f1b70e16..6e603c1baf6a9 100644 --- a/basic/sorting/MergeSort/README.md +++ b/basic/sorting/MergeSort/README.md @@ -285,25 +285,24 @@ int n; int nums[N]; int tmp[N]; -void merge_sort(int nums[], int left, int right) -{ +void merge_sort(int nums[], int left, int right) { if (left >= right) return; int mid = (left + right) >> 1; merge_sort(nums, left, mid); merge_sort(nums, mid + 1, right); int i = left, j = mid + 1, k = 0; - while (i <= mid && j <= right) - { - if (nums[i] <= nums[j]) tmp[k++] = nums[i++]; - else tmp[k++] = nums[j++]; + while (i <= mid && j <= right) { + if (nums[i] <= nums[j]) + tmp[k++] = nums[i++]; + else + tmp[k++] = nums[j++]; } while (i <= mid) tmp[k++] = nums[i++]; while (j <= right) tmp[k++] = nums[j++]; for (i = left, j = 0; i <= right; ++i, ++j) nums[i] = tmp[j]; } -int main() -{ +int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &nums[i]); diff --git a/basic/sorting/QuickSort/Main.cpp b/basic/sorting/QuickSort/Main.cpp index 20b70d62f73a3..3d939bde06a97 100644 --- a/basic/sorting/QuickSort/Main.cpp +++ b/basic/sorting/QuickSort/Main.cpp @@ -7,23 +7,22 @@ const int N = 1e6 + 10; int n; int nums[N]; -void quick_sort(int nums[], int left, int right) -{ +void quick_sort(int nums[], int left, int right) { if (left >= right) return; int i = left - 1, j = right + 1; int x = nums[left + right >> 1]; - while (i < j) - { - while (nums[++i] < x); - while (nums[--j] > x); + while (i < j) { + while (nums[++i] < x) + ; + while (nums[--j] > x) + ; if (i < j) swap(nums[i], nums[j]); } quick_sort(nums, left, j); quick_sort(nums, j + 1, right); } -int main() -{ +int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &nums[i]); diff --git a/basic/sorting/QuickSort/README.md b/basic/sorting/QuickSort/README.md index aea1cda8eef2c..93a019e9bf5e6 100644 --- a/basic/sorting/QuickSort/README.md +++ b/basic/sorting/QuickSort/README.md @@ -299,23 +299,22 @@ const int N = 1e6 + 10; int n; int nums[N]; -void quick_sort(int nums[], int left, int right) -{ +void quick_sort(int nums[], int left, int right) { if (left >= right) return; int i = left - 1, j = right + 1; int x = nums[left + right >> 1]; - while (i < j) - { - while (nums[++i] < x); - while (nums[--j] > x); + while (i < j) { + while (nums[++i] < x) + ; + while (nums[--j] > x) + ; if (i < j) swap(nums[i], nums[j]); } quick_sort(nums, left, j); quick_sort(nums, j + 1, right); } -int main() -{ +int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", &nums[i]); diff --git a/basic/sorting/SelectionSort/README.md b/basic/sorting/SelectionSort/README.md index 5b37a25c281cc..39257ba6837fb 100644 --- a/basic/sorting/SelectionSort/README.md +++ b/basic/sorting/SelectionSort/README.md @@ -97,11 +97,9 @@ func main() { using namespace std; -void printvec( const vector &vec, const string &strbegin = "", const string &strend = "" ) -{ +void printvec(const vector& vec, const string& strbegin = "", const string& strend = "") { cout << strbegin << endl; - for ( auto val : vec ) - { + for (auto val : vec) { cout << val << "\t"; } @@ -109,32 +107,25 @@ void printvec( const vector &vec, const string &strbegin = "", const string cout << strend << endl; } - -void selectsort( vector & vec ) -{ - for ( int i = 0; i < vec.size() - 1; i++ ) - { +void selectsort(vector& vec) { + for (int i = 0; i < vec.size() - 1; i++) { int minidx = i; - for ( int j = i + 1; j < vec.size(); j++ ) - { - if ( vec[minidx] > vec[j] ) - { + for (int j = i + 1; j < vec.size(); j++) { + if (vec[minidx] > vec[j]) { minidx = j; } } - swap( vec[i], vec[minidx] ); + swap(vec[i], vec[minidx]); } } - -int main( void ) -{ - vector vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - printvec( vec ); - selectsort( vec ); - printvec( vec, "after insert sort" ); - return(0); +int main(void) { + vector vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; + printvec(vec); + selectsort(vec); + printvec(vec, "after insert sort"); + return (0); } ``` diff --git a/basic/sorting/SelectionSort/SelectionSort.cpp b/basic/sorting/SelectionSort/SelectionSort.cpp index eb6a25466933f..3318ea66ee7b2 100644 --- a/basic/sorting/SelectionSort/SelectionSort.cpp +++ b/basic/sorting/SelectionSort/SelectionSort.cpp @@ -3,42 +3,33 @@ using namespace std; -void printvec( const vector &vec, const string &strbegin = "", const string &strend = "" ) -{ - cout << strbegin << endl; - for ( auto val : vec ) - { - cout << val << "\t"; - } - - cout << endl; - cout << strend << endl; +void printvec(const vector& vec, const string& strbegin = "", const string& strend = "") { + cout << strbegin << endl; + for (auto val : vec) { + cout << val << "\t"; + } + + cout << endl; + cout << strend << endl; } - -void selectsort( vector & vec ) -{ - for ( int i = 0; i < vec.size() - 1; i++ ) - { - int minidx = i; - for ( int j = i + 1; j < vec.size(); j++ ) - { - if ( vec[minidx] > vec[j] ) - { - minidx = j; - } - } - - swap( vec[i], vec[minidx] ); - } +void selectsort(vector& vec) { + for (int i = 0; i < vec.size() - 1; i++) { + int minidx = i; + for (int j = i + 1; j < vec.size(); j++) { + if (vec[minidx] > vec[j]) { + minidx = j; + } + } + + swap(vec[i], vec[minidx]); + } } - -int main( void ) -{ - vector vec = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; - printvec( vec ); - selectsort( vec ); - printvec( vec, "after insert sort" ); - return(0); +int main(void) { + vector vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0}; + printvec(vec); + selectsort(vec); + printvec(vec, "after insert sort"); + return (0); } diff --git a/lcci/01.02.Check Permutation/README.md b/lcci/01.02.Check Permutation/README.md index e1b9d787a54e8..1aa4695f3da82 100644 --- a/lcci/01.02.Check Permutation/README.md +++ b/lcci/01.02.Check Permutation/README.md @@ -135,8 +135,7 @@ public: int n2 = s2.size(); if (n1 != n2) return 0; vector counter(128); - for (int i = 0; i < n1; ++i) - { + for (int i = 0; i < n1; ++i) { ++counter[s1[i]]; --counter[s2[i]]; } diff --git a/lcci/01.02.Check Permutation/README_EN.md b/lcci/01.02.Check Permutation/README_EN.md index 5b1b43fcd88d9..44407e8ebb3fc 100644 --- a/lcci/01.02.Check Permutation/README_EN.md +++ b/lcci/01.02.Check Permutation/README_EN.md @@ -127,8 +127,7 @@ public: int n2 = s2.size(); if (n1 != n2) return 0; vector counter(128); - for (int i = 0; i < n1; ++i) - { + for (int i = 0; i < n1; ++i) { ++counter[s1[i]]; --counter[s2[i]]; } diff --git a/lcci/01.02.Check Permutation/Solution.cpp b/lcci/01.02.Check Permutation/Solution.cpp index 17f86bb1e26a9..c8618d04dba50 100644 --- a/lcci/01.02.Check Permutation/Solution.cpp +++ b/lcci/01.02.Check Permutation/Solution.cpp @@ -5,8 +5,7 @@ class Solution { int n2 = s2.size(); if (n1 != n2) return 0; vector counter(128); - for (int i = 0; i < n1; ++i) - { + for (int i = 0; i < n1; ++i) { ++counter[s1[i]]; --counter[s2[i]]; } diff --git a/lcci/02.01.Remove Duplicate Node/README.md b/lcci/02.01.Remove Duplicate Node/README.md index 4beff5b3d4d02..8bf611c9ad204 100644 --- a/lcci/02.01.Remove Duplicate Node/README.md +++ b/lcci/02.01.Remove Duplicate Node/README.md @@ -151,8 +151,8 @@ public: return head; } unordered_set cache = {head->val}; - ListNode *cur = head; - for (ListNode *p = head->next; p != nullptr; p = p->next) { + ListNode* cur = head; + for (ListNode* p = head->next; p != nullptr; p = p->next) { if (!cache.count(p->val)) { cur->next = p; cur = cur->next; diff --git a/lcci/02.01.Remove Duplicate Node/README_EN.md b/lcci/02.01.Remove Duplicate Node/README_EN.md index 778dd2135cd7e..334c48ed5af20 100644 --- a/lcci/02.01.Remove Duplicate Node/README_EN.md +++ b/lcci/02.01.Remove Duplicate Node/README_EN.md @@ -151,8 +151,8 @@ public: return head; } unordered_set cache = {head->val}; - ListNode *cur = head; - for (ListNode *p = head->next; p != nullptr; p = p->next) { + ListNode* cur = head; + for (ListNode* p = head->next; p != nullptr; p = p->next) { if (!cache.count(p->val)) { cur->next = p; cur = cur->next; diff --git a/lcci/02.01.Remove Duplicate Node/Solution.cpp b/lcci/02.01.Remove Duplicate Node/Solution.cpp index 93e8150e9e653..98929534a877a 100644 --- a/lcci/02.01.Remove Duplicate Node/Solution.cpp +++ b/lcci/02.01.Remove Duplicate Node/Solution.cpp @@ -13,8 +13,8 @@ class Solution { return head; } unordered_set cache = {head->val}; - ListNode *cur = head; - for (ListNode *p = head->next; p != nullptr; p = p->next) { + ListNode* cur = head; + for (ListNode* p = head->next; p != nullptr; p = p->next) { if (!cache.count(p->val)) { cur->next = p; cur = cur->next; diff --git a/lcci/02.05.Sum Lists/README.md b/lcci/02.05.Sum Lists/README.md index cbff0a437b2ed..6071ee10786d4 100644 --- a/lcci/02.05.Sum Lists/README.md +++ b/lcci/02.05.Sum Lists/README.md @@ -109,9 +109,8 @@ public: ListNode* dummy = new ListNode(0); ListNode* cur = dummy; int carry = 0; - while (l1 || l2 || carry) - { - carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val); + while (l1 || l2 || carry) { + carry += (!l1 ? 0 : l1->val) + (!l2 ? 0 : l2->val); cur->next = new ListNode(carry % 10); cur = cur->next; carry /= 10; diff --git a/lcci/02.05.Sum Lists/README_EN.md b/lcci/02.05.Sum Lists/README_EN.md index eaa09f2d0826c..ad48ee3081ed2 100644 --- a/lcci/02.05.Sum Lists/README_EN.md +++ b/lcci/02.05.Sum Lists/README_EN.md @@ -104,9 +104,8 @@ public: ListNode* dummy = new ListNode(0); ListNode* cur = dummy; int carry = 0; - while (l1 || l2 || carry) - { - carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val); + while (l1 || l2 || carry) { + carry += (!l1 ? 0 : l1->val) + (!l2 ? 0 : l2->val); cur->next = new ListNode(carry % 10); cur = cur->next; carry /= 10; diff --git a/lcci/02.05.Sum Lists/Solution.cpp b/lcci/02.05.Sum Lists/Solution.cpp index 17726d21bd793..a0224cc4ce853 100644 --- a/lcci/02.05.Sum Lists/Solution.cpp +++ b/lcci/02.05.Sum Lists/Solution.cpp @@ -12,9 +12,8 @@ class Solution { ListNode* dummy = new ListNode(0); ListNode* cur = dummy; int carry = 0; - while (l1 || l2 || carry) - { - carry += (!l1 ? 0 : l1-> val) + (!l2 ? 0 : l2->val); + while (l1 || l2 || carry) { + carry += (!l1 ? 0 : l1->val) + (!l2 ? 0 : l2->val); cur->next = new ListNode(carry % 10); cur = cur->next; carry /= 10; diff --git a/lcci/02.07.Intersection of Two Linked Lists/README.md b/lcci/02.07.Intersection of Two Linked Lists/README.md index bc7a60a5407a1..d39a2bf0799ad 100644 --- a/lcci/02.07.Intersection of Two Linked Lists/README.md +++ b/lcci/02.07.Intersection of Two Linked Lists/README.md @@ -80,7 +80,7 @@ public class Solution { */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git a/lcci/02.07.Intersection of Two Linked Lists/README_EN.md b/lcci/02.07.Intersection of Two Linked Lists/README_EN.md index aca83da8fd6bc..6640ade4fae53 100644 --- a/lcci/02.07.Intersection of Two Linked Lists/README_EN.md +++ b/lcci/02.07.Intersection of Two Linked Lists/README_EN.md @@ -106,7 +106,7 @@ public class Solution { */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git a/lcci/02.07.Intersection of Two Linked Lists/Solution.cpp b/lcci/02.07.Intersection of Two Linked Lists/Solution.cpp index 32460fd2360cb..753db3cdd199c 100644 --- a/lcci/02.07.Intersection of Two Linked Lists/Solution.cpp +++ b/lcci/02.07.Intersection of Two Linked Lists/Solution.cpp @@ -8,7 +8,7 @@ */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git a/lcci/02.08.Linked List Cycle/README.md b/lcci/02.08.Linked List Cycle/README.md index db697327b0361..7850e53b52985 100644 --- a/lcci/02.08.Linked List Cycle/README.md +++ b/lcci/02.08.Linked List Cycle/README.md @@ -104,7 +104,7 @@ public class Solution { */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; @@ -173,20 +173,20 @@ var detectCycle = function (head) { * } */ func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast - } - if !hasCycle { - return nil - } - p := head - for p != slow { - p, slow = p.Next, slow.Next - } - return p + slow, fast := head, head + hasCycle := false + for !hasCycle && fast != nil && fast.Next != nil { + slow, fast = slow.Next, fast.Next.Next + hasCycle = slow == fast + } + if !hasCycle { + return nil + } + p := head + for p != slow { + p, slow = p.Next, slow.Next + } + return p } ``` diff --git a/lcci/02.08.Linked List Cycle/README_EN.md b/lcci/02.08.Linked List Cycle/README_EN.md index 63619b7d63306..31601d86a37ff 100644 --- a/lcci/02.08.Linked List Cycle/README_EN.md +++ b/lcci/02.08.Linked List Cycle/README_EN.md @@ -49,14 +49,20 @@ Can you solve it without using additional space?

# self.val = x # self.next = None + class Solution: - def hasCycle(self, head: ListNode) -> bool: + def detectCycle(self, head: ListNode) -> ListNode: slow = fast = head - while fast and fast.next: + has_cycle = False + while not has_cycle and fast and fast.next: slow, fast = slow.next, fast.next.next - if slow == fast: - return True - return False + has_cycle = slow == fast + if not has_cycle: + return None + p = head + while p != slow: + p, slow = p.next, slow.next + return p ``` ### **Java** @@ -74,17 +80,23 @@ class Solution: * } */ public class Solution { - public boolean hasCycle(ListNode head) { - ListNode slow = head; - ListNode fast = head; - while (fast != null && fast.next != null) { + public ListNode detectCycle(ListNode head) { + ListNode slow = head, fast = head; + boolean hasCycle = false; + while (!hasCycle && fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; - if (slow == fast) { - return true; - } + hasCycle = slow == fast; + } + if (!hasCycle) { + return null; + } + ListNode p = head; + while (p != slow) { + p = p.next; + slow = slow.next; } - return false; + return p; } } ``` @@ -102,17 +114,24 @@ public class Solution { */ class Solution { public: - bool hasCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; - while (fast && fast->next) { + bool hasCycle = false; + while (!hasCycle && fast && fast->next) { slow = slow->next; fast = fast->next->next; - if (slow == fast) { - return true; - } + hasCycle = slow == fast; + } + if (!hasCycle) { + return nullptr; + } + ListNode* p = head; + while (p != slow) { + p = p->next; + slow = slow->next; } - return false; + return p; } }; ``` @@ -130,19 +149,26 @@ public: /** * @param {ListNode} head - * @return {boolean} + * @return {ListNode} */ -var hasCycle = function (head) { +var detectCycle = function (head) { let slow = head; let fast = head; - while (fast && fast.next) { + let hasCycle = false; + while (!hasCycle && fast && fast.next) { slow = slow.next; fast = fast.next.next; - if (slow == fast) { - return true; - } + hasCycle = slow == fast; + } + if (!hasCycle) { + return null; + } + let p = head; + while (p != slow) { + p = p.next; + slow = slow.next; } - return false; + return p; }; ``` @@ -156,15 +182,21 @@ var hasCycle = function (head) { * Next *ListNode * } */ -func hasCycle(head *ListNode) bool { - slow, fast := head, head - for fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - if slow == fast { - return true - } - } - return false +func detectCycle(head *ListNode) *ListNode { + slow, fast := head, head + hasCycle := false + for !hasCycle && fast != nil && fast.Next != nil { + slow, fast = slow.Next, fast.Next.Next + hasCycle = slow == fast + } + if !hasCycle { + return nil + } + p := head + for p != slow { + p, slow = p.Next, slow.Next + } + return p } ``` diff --git a/lcci/02.08.Linked List Cycle/Solution.cpp b/lcci/02.08.Linked List Cycle/Solution.cpp index c5ce3fefb03b8..890916dbc2485 100644 --- a/lcci/02.08.Linked List Cycle/Solution.cpp +++ b/lcci/02.08.Linked List Cycle/Solution.cpp @@ -8,7 +8,7 @@ */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; diff --git a/lcci/02.08.Linked List Cycle/Solution.go b/lcci/02.08.Linked List Cycle/Solution.go index c4e646aa2c6c7..29cb7c3eb5848 100644 --- a/lcci/02.08.Linked List Cycle/Solution.go +++ b/lcci/02.08.Linked List Cycle/Solution.go @@ -5,19 +5,19 @@ * Next *ListNode * } */ - func detectCycle(head *ListNode) *ListNode { - slow, fast := head, head - hasCycle := false - for !hasCycle && fast != nil && fast.Next != nil { - slow, fast = slow.Next, fast.Next.Next - hasCycle = slow == fast - } - if !hasCycle { - return nil - } - p := head - for p != slow { - p, slow = p.Next, slow.Next - } - return p +func detectCycle(head *ListNode) *ListNode { + slow, fast := head, head + hasCycle := false + for !hasCycle && fast != nil && fast.Next != nil { + slow, fast = slow.Next, fast.Next.Next + hasCycle = slow == fast + } + if !hasCycle { + return nil + } + p := head + for p != slow { + p, slow = p.Next, slow.Next + } + return p } \ No newline at end of file diff --git a/lcci/03.02.Min Stack/README.md b/lcci/03.02.Min Stack/README.md index b981d6146f1e4..babb42b320c86 100644 --- a/lcci/03.02.Min Stack/README.md +++ b/lcci/03.02.Min Stack/README.md @@ -104,6 +104,7 @@ class MinStack { private: stack stk; stack minStk; + public: /** initialize your data structure here. */ MinStack() = default; diff --git a/lcci/03.02.Min Stack/README_EN.md b/lcci/03.02.Min Stack/README_EN.md index dd40abb1c0d79..db1e1cea5f455 100644 --- a/lcci/03.02.Min Stack/README_EN.md +++ b/lcci/03.02.Min Stack/README_EN.md @@ -115,6 +115,7 @@ class MinStack { private: stack stk; stack minStk; + public: /** initialize your data structure here. */ MinStack() = default; diff --git a/lcci/03.02.Min Stack/Solution.cpp b/lcci/03.02.Min Stack/Solution.cpp index 5e8c5f929070f..f9eef6bff5bc1 100644 --- a/lcci/03.02.Min Stack/Solution.cpp +++ b/lcci/03.02.Min Stack/Solution.cpp @@ -2,6 +2,7 @@ class MinStack { private: stack stk; stack minStk; + public: /** initialize your data structure here. */ MinStack() = default; diff --git a/lcci/04.01.Route Between Nodes/README.md b/lcci/04.01.Route Between Nodes/README.md index 745cfb03c3857..32f288e4d5708 100644 --- a/lcci/04.01.Route Between Nodes/README.md +++ b/lcci/04.01.Route Between Nodes/README.md @@ -155,10 +155,8 @@ public: bool dfs(int u, int& target, unordered_map>& g, unordered_set& vis) { if (u == target) return true; - for (int& v : g[u]) - { - if (!vis.count(v)) - { + for (int& v : g[u]) { + if (!vis.count(v)) { vis.insert(v); if (dfs(v, target, g, vis)) return true; } @@ -174,17 +172,14 @@ public: bool findWhetherExistsPath(int n, vector>& graph, int start, int target) { unordered_map> g; for (auto& e : graph) g[e[0]].push_back(e[1]); - queue q{{start}}; - unordered_set vis{{start}}; - while (!q.empty()) - { + queue q {{start}}; + unordered_set vis {{start}}; + while (!q.empty()) { int u = q.front(); if (u == target) return true; q.pop(); - for (int v : g[u]) - { - if (!vis.count(v)) - { + for (int v : g[u]) { + if (!vis.count(v)) { vis.insert(v); q.push(v); } diff --git a/lcci/04.01.Route Between Nodes/README_EN.md b/lcci/04.01.Route Between Nodes/README_EN.md index 25ff485727fe3..d399186cfa1d5 100644 --- a/lcci/04.01.Route Between Nodes/README_EN.md +++ b/lcci/04.01.Route Between Nodes/README_EN.md @@ -154,10 +154,8 @@ public: bool dfs(int u, int& target, unordered_map>& g, unordered_set& vis) { if (u == target) return true; - for (int& v : g[u]) - { - if (!vis.count(v)) - { + for (int& v : g[u]) { + if (!vis.count(v)) { vis.insert(v); if (dfs(v, target, g, vis)) return true; } @@ -173,17 +171,14 @@ public: bool findWhetherExistsPath(int n, vector>& graph, int start, int target) { unordered_map> g; for (auto& e : graph) g[e[0]].push_back(e[1]); - queue q{{start}}; - unordered_set vis{{start}}; - while (!q.empty()) - { + queue q {{start}}; + unordered_set vis {{start}}; + while (!q.empty()) { int u = q.front(); if (u == target) return true; q.pop(); - for (int v : g[u]) - { - if (!vis.count(v)) - { + for (int v : g[u]) { + if (!vis.count(v)) { vis.insert(v); q.push(v); } diff --git a/lcci/04.01.Route Between Nodes/Solution.cpp b/lcci/04.01.Route Between Nodes/Solution.cpp index d790bf85ad1a2..e6e45fdc94c27 100644 --- a/lcci/04.01.Route Between Nodes/Solution.cpp +++ b/lcci/04.01.Route Between Nodes/Solution.cpp @@ -3,17 +3,14 @@ class Solution { bool findWhetherExistsPath(int n, vector>& graph, int start, int target) { unordered_map> g; for (auto& e : graph) g[e[0]].push_back(e[1]); - queue q{{start}}; - unordered_set vis{{start}}; - while (!q.empty()) - { + queue q {{start}}; + unordered_set vis {{start}}; + while (!q.empty()) { int u = q.front(); if (u == target) return true; q.pop(); - for (int v : g[u]) - { - if (!vis.count(v)) - { + for (int v : g[u]) { + if (!vis.count(v)) { vis.insert(v); q.push(v); } diff --git a/lcci/04.05.Legal Binary Search Tree/README.md b/lcci/04.05.Legal Binary Search Tree/README.md index 6ef63d567034c..04340a5f301c6 100644 --- a/lcci/04.05.Legal Binary Search Tree/README.md +++ b/lcci/04.05.Legal Binary Search Tree/README.md @@ -142,10 +142,10 @@ func check(node *TreeNode, lower, upper int) bool { */ class Solution { public: - bool isValidBST(TreeNode *root) { - TreeNode *pre = nullptr; - TreeNode *cur = root; - stack stk; + bool isValidBST(TreeNode* root) { + TreeNode* pre = nullptr; + TreeNode* cur = root; + stack stk; while (cur || !stk.empty()) { if (cur) { stk.push(cur); diff --git a/lcci/04.05.Legal Binary Search Tree/README_EN.md b/lcci/04.05.Legal Binary Search Tree/README_EN.md index 3bf4d94737eb5..d5c3dd52b7f05 100644 --- a/lcci/04.05.Legal Binary Search Tree/README_EN.md +++ b/lcci/04.05.Legal Binary Search Tree/README_EN.md @@ -171,10 +171,10 @@ func check(node *TreeNode, lower, upper int) bool { */ class Solution { public: - bool isValidBST(TreeNode *root) { - TreeNode *pre = nullptr; - TreeNode *cur = root; - stack stk; + bool isValidBST(TreeNode* root) { + TreeNode* pre = nullptr; + TreeNode* cur = root; + stack stk; while (cur || !stk.empty()) { if (cur) { stk.push(cur); diff --git a/lcci/04.05.Legal Binary Search Tree/Solution.cpp b/lcci/04.05.Legal Binary Search Tree/Solution.cpp index bf3c0934de767..8c63408bf1fa1 100644 --- a/lcci/04.05.Legal Binary Search Tree/Solution.cpp +++ b/lcci/04.05.Legal Binary Search Tree/Solution.cpp @@ -9,10 +9,10 @@ */ class Solution { public: - bool isValidBST(TreeNode *root) { - TreeNode *pre = nullptr; - TreeNode *cur = root; - stack stk; + bool isValidBST(TreeNode* root) { + TreeNode* pre = nullptr; + TreeNode* cur = root; + stack stk; while (cur || !stk.empty()) { if (cur) { stk.push(cur); diff --git a/lcci/08.01.Three Steps Problem/README.md b/lcci/08.01.Three Steps Problem/README.md index 44a9940e9aecf..033c96f1581d1 100644 --- a/lcci/08.01.Three Steps Problem/README.md +++ b/lcci/08.01.Three Steps Problem/README.md @@ -95,15 +95,12 @@ var waysToStep = function (n) { ### **C** ```c -int waysToStep(int n) -{ - if (n < 3) - { +int waysToStep(int n) { + if (n < 3) { return n; } int a = 1, b = 2, c = 4, i = 4; - while (i++ <= n) - { + while (i++ <= n) { int t = ((a + b) % 1000000007 + c) % 1000000007; a = b; b = c; diff --git a/lcci/08.01.Three Steps Problem/README_EN.md b/lcci/08.01.Three Steps Problem/README_EN.md index 3ddf1aaa8f881..cafcf475aee03 100644 --- a/lcci/08.01.Three Steps Problem/README_EN.md +++ b/lcci/08.01.Three Steps Problem/README_EN.md @@ -89,15 +89,12 @@ var waysToStep = function (n) { ### **C** ```c -int waysToStep(int n) -{ - if (n < 3) - { +int waysToStep(int n) { + if (n < 3) { return n; } int a = 1, b = 2, c = 4, i = 4; - while (i++ <= n) - { + while (i++ <= n) { int t = ((a + b) % 1000000007 + c) % 1000000007; a = b; b = c; diff --git a/lcci/08.01.Three Steps Problem/Solution.c b/lcci/08.01.Three Steps Problem/Solution.c index f0d26e0e2e4cd..2c8b0d90591f8 100644 --- a/lcci/08.01.Three Steps Problem/Solution.c +++ b/lcci/08.01.Three Steps Problem/Solution.c @@ -1,12 +1,9 @@ -int waysToStep(int n) -{ - if (n < 3) - { +int waysToStep(int n) { + if (n < 3) { return n; } int a = 1, b = 2, c = 4, i = 4; - while (i++ <= n) - { + while (i++ <= n) { int t = ((a + b) % 1000000007 + c) % 1000000007; a = b; b = c; diff --git a/lcci/08.04.Power Set/README.md b/lcci/08.04.Power Set/README.md index 03b0209f35e9d..391c30cc6f297 100644 --- a/lcci/08.04.Power Set/README.md +++ b/lcci/08.04.Power Set/README.md @@ -193,8 +193,7 @@ public: } void dfs(int u, vector& t, vector& nums, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { ans.push_back(t); return; } diff --git a/lcci/08.04.Power Set/README_EN.md b/lcci/08.04.Power Set/README_EN.md index 2fa1b8c913fc3..f259db651cdc3 100644 --- a/lcci/08.04.Power Set/README_EN.md +++ b/lcci/08.04.Power Set/README_EN.md @@ -200,8 +200,7 @@ public: } void dfs(int u, vector& t, vector& nums, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { ans.push_back(t); return; } diff --git a/lcci/08.04.Power Set/Solution.cpp b/lcci/08.04.Power Set/Solution.cpp index 976e23c3b999b..9ac0a836de679 100644 --- a/lcci/08.04.Power Set/Solution.cpp +++ b/lcci/08.04.Power Set/Solution.cpp @@ -8,8 +8,7 @@ class Solution { } void dfs(int u, vector& t, vector& nums, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { ans.push_back(t); return; } diff --git a/lcci/08.07.Permutation I/README.md b/lcci/08.07.Permutation I/README.md index 9a2dfbf1b92ed..90443c5f7c55d 100644 --- a/lcci/08.07.Permutation I/README.md +++ b/lcci/08.07.Permutation I/README.md @@ -144,13 +144,11 @@ public: } void dfs(int u, string& S, string& t, vector& ans, unordered_set& vis) { - if (u == S.size()) - { + if (u == S.size()) { ans.push_back(t); return; } - for (char& c : S) - { + for (char& c : S) { if (vis.count(c)) continue; vis.insert(c); t.push_back(c); diff --git a/lcci/08.07.Permutation I/README_EN.md b/lcci/08.07.Permutation I/README_EN.md index bd7f948e0af18..1d7a715dfb71f 100644 --- a/lcci/08.07.Permutation I/README_EN.md +++ b/lcci/08.07.Permutation I/README_EN.md @@ -143,13 +143,11 @@ public: } void dfs(int u, string& S, string& t, vector& ans, unordered_set& vis) { - if (u == S.size()) - { + if (u == S.size()) { ans.push_back(t); return; } - for (char& c : S) - { + for (char& c : S) { if (vis.count(c)) continue; vis.insert(c); t.push_back(c); diff --git a/lcci/08.07.Permutation I/Solution.cpp b/lcci/08.07.Permutation I/Solution.cpp index e582008aadb45..380f09189a528 100644 --- a/lcci/08.07.Permutation I/Solution.cpp +++ b/lcci/08.07.Permutation I/Solution.cpp @@ -9,13 +9,11 @@ class Solution { } void dfs(int u, string& S, string& t, vector& ans, unordered_set& vis) { - if (u == S.size()) - { + if (u == S.size()) { ans.push_back(t); return; } - for (char& c : S) - { + for (char& c : S) { if (vis.count(c)) continue; vis.insert(c); t.push_back(c); diff --git a/lcci/08.09.Bracket/README.md b/lcci/08.09.Bracket/README.md index 4463bdc4329ad..99ff66358ab59 100644 --- a/lcci/08.09.Bracket/README.md +++ b/lcci/08.09.Bracket/README.md @@ -111,8 +111,7 @@ public: } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git a/lcci/08.09.Bracket/README_EN.md b/lcci/08.09.Bracket/README_EN.md index 4428e8e16a8ca..0f23731d6d6ba 100644 --- a/lcci/08.09.Bracket/README_EN.md +++ b/lcci/08.09.Bracket/README_EN.md @@ -112,8 +112,7 @@ public: } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git a/lcci/08.09.Bracket/Solution.cpp b/lcci/08.09.Bracket/Solution.cpp index 2af50ad429a32..37d8d0850aaf4 100644 --- a/lcci/08.09.Bracket/Solution.cpp +++ b/lcci/08.09.Bracket/Solution.cpp @@ -7,8 +7,7 @@ class Solution { } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git a/lcci/08.12.Eight Queens/README.md b/lcci/08.12.Eight Queens/README.md index 59aa747f78722..3a76050a35719 100644 --- a/lcci/08.12.Eight Queens/README.md +++ b/lcci/08.12.Eight Queens/README.md @@ -127,15 +127,12 @@ public: } void dfs(int u, int n, vector& col, vector& dg, vector& udg, vector& g, vector>& res) { - if (u == n) - { + if (u == n) { res.push_back(g); return; } - for (int i = 0; i < n; ++i) - { - if (!col[i] && !dg[u + i] && !udg[n - u + i]) - { + for (int i = 0; i < n; ++i) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { g[u][i] = 'Q'; col[i] = dg[u + i] = udg[n - u + i] = true; dfs(u + 1, n, col, dg, udg, g, res); diff --git a/lcci/08.12.Eight Queens/README_EN.md b/lcci/08.12.Eight Queens/README_EN.md index 86a58591bc021..e5998f5bea7bf 100644 --- a/lcci/08.12.Eight Queens/README_EN.md +++ b/lcci/08.12.Eight Queens/README_EN.md @@ -132,15 +132,12 @@ public: } void dfs(int u, int n, vector& col, vector& dg, vector& udg, vector& g, vector>& res) { - if (u == n) - { + if (u == n) { res.push_back(g); return; } - for (int i = 0; i < n; ++i) - { - if (!col[i] && !dg[u + i] && !udg[n - u + i]) - { + for (int i = 0; i < n; ++i) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { g[u][i] = 'Q'; col[i] = dg[u + i] = udg[n - u + i] = true; dfs(u + 1, n, col, dg, udg, g, res); diff --git a/lcci/08.12.Eight Queens/Solution.cpp b/lcci/08.12.Eight Queens/Solution.cpp index c4893270b9fdf..21409d1fb3c38 100644 --- a/lcci/08.12.Eight Queens/Solution.cpp +++ b/lcci/08.12.Eight Queens/Solution.cpp @@ -11,15 +11,12 @@ class Solution { } void dfs(int u, int n, vector& col, vector& dg, vector& udg, vector& g, vector>& res) { - if (u == n) - { + if (u == n) { res.push_back(g); return; } - for (int i = 0; i < n; ++i) - { - if (!col[i] && !dg[u + i] && !udg[n - u + i]) - { + for (int i = 0; i < n; ++i) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { g[u][i] = 'Q'; col[i] = dg[u + i] = udg[n - u + i] = true; dfs(u + 1, n, col, dg, udg, g, res); diff --git a/lcci/08.14.Boolean Evaluation/README.md b/lcci/08.14.Boolean Evaluation/README.md index 573681163ffcb..349e661c2fdf5 100644 --- a/lcci/08.14.Boolean Evaluation/README.md +++ b/lcci/08.14.Boolean Evaluation/README.md @@ -132,24 +132,23 @@ public: vector dfs(string s) { if (memo.count(s)) return memo[s]; vector res(2); - if (s.size() == 1) - { + if (s.size() == 1) { res[s[0] - '0'] = 1; return res; } - for (int k = 0; k < s.size(); ++k) - { + for (int k = 0; k < s.size(); ++k) { if (s[k] == '0' || s[k] == '1') continue; vector left = dfs(s.substr(0, k)); vector right = dfs(s.substr(k + 1, s.size() - k)); - for (int i = 0; i < 2; ++i) - { - for (int j = 0; j < 2; ++j) - { + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { int v = 0; - if (s[k] == '&') v = i & j; - else if (s[k] == '|') v = i | j; - else if (s[k] == '^') v = i ^ j; + if (s[k] == '&') + v = i & j; + else if (s[k] == '|') + v = i | j; + else if (s[k] == '^') + v = i ^ j; res[v] += left[i] * right[j]; } } diff --git a/lcci/08.14.Boolean Evaluation/README_EN.md b/lcci/08.14.Boolean Evaluation/README_EN.md index dea5ea733d21e..f7d6359ead94d 100644 --- a/lcci/08.14.Boolean Evaluation/README_EN.md +++ b/lcci/08.14.Boolean Evaluation/README_EN.md @@ -135,24 +135,23 @@ public: vector dfs(string s) { if (memo.count(s)) return memo[s]; vector res(2); - if (s.size() == 1) - { + if (s.size() == 1) { res[s[0] - '0'] = 1; return res; } - for (int k = 0; k < s.size(); ++k) - { + for (int k = 0; k < s.size(); ++k) { if (s[k] == '0' || s[k] == '1') continue; vector left = dfs(s.substr(0, k)); vector right = dfs(s.substr(k + 1, s.size() - k)); - for (int i = 0; i < 2; ++i) - { - for (int j = 0; j < 2; ++j) - { + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { int v = 0; - if (s[k] == '&') v = i & j; - else if (s[k] == '|') v = i | j; - else if (s[k] == '^') v = i ^ j; + if (s[k] == '&') + v = i & j; + else if (s[k] == '|') + v = i | j; + else if (s[k] == '^') + v = i ^ j; res[v] += left[i] * right[j]; } } diff --git a/lcci/08.14.Boolean Evaluation/Solution.cpp b/lcci/08.14.Boolean Evaluation/Solution.cpp index b222f94098481..666b98c153dd5 100644 --- a/lcci/08.14.Boolean Evaluation/Solution.cpp +++ b/lcci/08.14.Boolean Evaluation/Solution.cpp @@ -10,24 +10,23 @@ class Solution { vector dfs(string s) { if (memo.count(s)) return memo[s]; vector res(2); - if (s.size() == 1) - { + if (s.size() == 1) { res[s[0] - '0'] = 1; return res; } - for (int k = 0; k < s.size(); ++k) - { + for (int k = 0; k < s.size(); ++k) { if (s[k] == '0' || s[k] == '1') continue; vector left = dfs(s.substr(0, k)); vector right = dfs(s.substr(k + 1, s.size() - k)); - for (int i = 0; i < 2; ++i) - { - for (int j = 0; j < 2; ++j) - { + for (int i = 0; i < 2; ++i) { + for (int j = 0; j < 2; ++j) { int v = 0; - if (s[k] == '&') v = i & j; - else if (s[k] == '|') v = i | j; - else if (s[k] == '^') v = i ^ j; + if (s[k] == '&') + v = i & j; + else if (s[k] == '|') + v = i | j; + else if (s[k] == '^') + v = i ^ j; res[v] += left[i] * right[j]; } } diff --git a/lcci/10.01.Sorted Merge/README.md b/lcci/10.01.Sorted Merge/README.md index 627308b12be38..b967d31f6feb5 100644 --- a/lcci/10.01.Sorted Merge/README.md +++ b/lcci/10.01.Sorted Merge/README.md @@ -144,10 +144,11 @@ class Solution { public: void merge(vector& A, int m, vector& B, int n) { int i = m - 1, j = n - 1; - for (int k = A.size() - 1; k >= 0; --k) - { - if (j < 0 || (i >= 0 && A[i] >= B[j])) A[k] = A[i--]; - else A[k] = B[j--]; + for (int k = A.size() - 1; k >= 0; --k) { + if (j < 0 || (i >= 0 && A[i] >= B[j])) + A[k] = A[i--]; + else + A[k] = B[j--]; } } }; diff --git a/lcci/10.01.Sorted Merge/README_EN.md b/lcci/10.01.Sorted Merge/README_EN.md index ce1a3779a9479..d3aa59e6abb4f 100644 --- a/lcci/10.01.Sorted Merge/README_EN.md +++ b/lcci/10.01.Sorted Merge/README_EN.md @@ -137,10 +137,11 @@ class Solution { public: void merge(vector& A, int m, vector& B, int n) { int i = m - 1, j = n - 1; - for (int k = A.size() - 1; k >= 0; --k) - { - if (j < 0 || (i >= 0 && A[i] >= B[j])) A[k] = A[i--]; - else A[k] = B[j--]; + for (int k = A.size() - 1; k >= 0; --k) { + if (j < 0 || (i >= 0 && A[i] >= B[j])) + A[k] = A[i--]; + else + A[k] = B[j--]; } } }; diff --git a/lcci/10.01.Sorted Merge/Solution.cpp b/lcci/10.01.Sorted Merge/Solution.cpp index 407b69b0a8eb5..ec7609f8772ed 100644 --- a/lcci/10.01.Sorted Merge/Solution.cpp +++ b/lcci/10.01.Sorted Merge/Solution.cpp @@ -2,10 +2,11 @@ class Solution { public: void merge(vector& A, int m, vector& B, int n) { int i = m - 1, j = n - 1; - for (int k = A.size() - 1; k >= 0; --k) - { - if (j < 0 || (i >= 0 && A[i] >= B[j])) A[k] = A[i--]; - else A[k] = B[j--]; + for (int k = A.size() - 1; k >= 0; --k) { + if (j < 0 || (i >= 0 && A[i] >= B[j])) + A[k] = A[i--]; + else + A[k] = B[j--]; } } }; \ No newline at end of file diff --git a/lcci/10.02.Group Anagrams/README.md b/lcci/10.02.Group Anagrams/README.md index 1ba6e6f9fff30..68c4494e38081 100644 --- a/lcci/10.02.Group Anagrams/README.md +++ b/lcci/10.02.Group Anagrams/README.md @@ -80,17 +80,15 @@ class Solution { ```cpp class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git a/lcci/10.02.Group Anagrams/README_EN.md b/lcci/10.02.Group Anagrams/README_EN.md index 08dd681bbd84d..bd549e5464e5f 100644 --- a/lcci/10.02.Group Anagrams/README_EN.md +++ b/lcci/10.02.Group Anagrams/README_EN.md @@ -77,17 +77,15 @@ class Solution { ```cpp class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git a/lcci/10.02.Group Anagrams/Solution.cpp b/lcci/10.02.Group Anagrams/Solution.cpp index 5b530c70aa9ec..2e25d4fe06c4f 100644 --- a/lcci/10.02.Group Anagrams/Solution.cpp +++ b/lcci/10.02.Group Anagrams/Solution.cpp @@ -1,16 +1,14 @@ class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git a/lcci/10.05.Sparse Array Search/README.md b/lcci/10.05.Sparse Array Search/README.md index 4b55b7c1e56e6..e2fe64bce8236 100644 --- a/lcci/10.05.Sparse Array Search/README.md +++ b/lcci/10.05.Sparse Array Search/README.md @@ -82,12 +82,13 @@ class Solution { public: int findString(vector& words, string s) { int left = 0, right = words.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; while (left < mid && words[mid] == "") --mid; - if (s <= words[mid]) right = mid; - else left = mid + 1; + if (s <= words[mid]) + right = mid; + else + left = mid + 1; } return words[left] == s ? left : -1; } diff --git a/lcci/10.05.Sparse Array Search/README_EN.md b/lcci/10.05.Sparse Array Search/README_EN.md index 8addec12d4c1b..f68b4eea7da6d 100644 --- a/lcci/10.05.Sparse Array Search/README_EN.md +++ b/lcci/10.05.Sparse Array Search/README_EN.md @@ -82,12 +82,13 @@ class Solution { public: int findString(vector& words, string s) { int left = 0, right = words.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; while (left < mid && words[mid] == "") --mid; - if (s <= words[mid]) right = mid; - else left = mid + 1; + if (s <= words[mid]) + right = mid; + else + left = mid + 1; } return words[left] == s ? left : -1; } diff --git a/lcci/10.05.Sparse Array Search/Solution.cpp b/lcci/10.05.Sparse Array Search/Solution.cpp index f96dad49a23cf..50c1bdceca217 100644 --- a/lcci/10.05.Sparse Array Search/Solution.cpp +++ b/lcci/10.05.Sparse Array Search/Solution.cpp @@ -2,12 +2,13 @@ class Solution { public: int findString(vector& words, string s) { int left = 0, right = words.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; while (left < mid && words[mid] == "") --mid; - if (s <= words[mid]) right = mid; - else left = mid + 1; + if (s <= words[mid]) + right = mid; + else + left = mid + 1; } return words[left] == s ? left : -1; } diff --git a/lcci/10.09.Sorted Matrix Search/README.md b/lcci/10.09.Sorted Matrix Search/README.md index 2f41abb7dc436..cbac5e9dced93 100644 --- a/lcci/10.09.Sorted Matrix Search/README.md +++ b/lcci/10.09.Sorted Matrix Search/README.md @@ -90,11 +90,12 @@ public: if (matrix.size() == 0 || matrix[0].size() == 0) return false; int m = matrix.size(), n = matrix[0].size(); int i = m - 1, j = 0; - while (i >= 0 && j < n) - { + while (i >= 0 && j < n) { if (matrix[i][j] == target) return true; - if (matrix[i][j] > target) --i; - else ++j; + if (matrix[i][j] > target) + --i; + else + ++j; } return false; } diff --git a/lcci/10.09.Sorted Matrix Search/README_EN.md b/lcci/10.09.Sorted Matrix Search/README_EN.md index d382ddcee9b68..be59a60b499d3 100644 --- a/lcci/10.09.Sorted Matrix Search/README_EN.md +++ b/lcci/10.09.Sorted Matrix Search/README_EN.md @@ -89,11 +89,12 @@ public: if (matrix.size() == 0 || matrix[0].size() == 0) return false; int m = matrix.size(), n = matrix[0].size(); int i = m - 1, j = 0; - while (i >= 0 && j < n) - { + while (i >= 0 && j < n) { if (matrix[i][j] == target) return true; - if (matrix[i][j] > target) --i; - else ++j; + if (matrix[i][j] > target) + --i; + else + ++j; } return false; } diff --git a/lcci/10.09.Sorted Matrix Search/Solution.cpp b/lcci/10.09.Sorted Matrix Search/Solution.cpp index 2ba14d7b3bbb2..cc71467ae916f 100644 --- a/lcci/10.09.Sorted Matrix Search/Solution.cpp +++ b/lcci/10.09.Sorted Matrix Search/Solution.cpp @@ -4,11 +4,12 @@ class Solution { if (matrix.size() == 0 || matrix[0].size() == 0) return false; int m = matrix.size(), n = matrix[0].size(); int i = m - 1, j = 0; - while (i >= 0 && j < n) - { + while (i >= 0 && j < n) { if (matrix[i][j] == target) return true; - if (matrix[i][j] > target) --i; - else ++j; + if (matrix[i][j] > target) + --i; + else + ++j; } return false; } diff --git a/lcci/10.10.Rank from Stream/README.md b/lcci/10.10.Rank from Stream/README.md index a20e1e80220b0..a3726bb6f69de 100644 --- a/lcci/10.10.Rank from Stream/README.md +++ b/lcci/10.10.Rank from Stream/README.md @@ -162,11 +162,12 @@ public: int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -174,8 +175,7 @@ public: int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } diff --git a/lcci/10.10.Rank from Stream/README_EN.md b/lcci/10.10.Rank from Stream/README_EN.md index 800683270f802..768c389912ba7 100644 --- a/lcci/10.10.Rank from Stream/README_EN.md +++ b/lcci/10.10.Rank from Stream/README_EN.md @@ -145,11 +145,12 @@ public: int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -157,8 +158,7 @@ public: int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } diff --git a/lcci/10.10.Rank from Stream/Solution.cpp b/lcci/10.10.Rank from Stream/Solution.cpp index b92a54bbbf0d3..55af0f4b09793 100644 --- a/lcci/10.10.Rank from Stream/Solution.cpp +++ b/lcci/10.10.Rank from Stream/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -35,11 +35,11 @@ class StreamRank { StreamRank() { tree = new BinaryIndexedTree(50010); } - + void track(int x) { tree->update(x + 1, 1); } - + int getRankOfNumber(int x) { return tree->query(x + 1); } diff --git a/lcci/16.06.Smallest Difference/README.md b/lcci/16.06.Smallest Difference/README.md index 3ab261951bf4c..2c8fbd3a978d9 100644 --- a/lcci/16.06.Smallest Difference/README.md +++ b/lcci/16.06.Smallest Difference/README.md @@ -79,8 +79,10 @@ public: long res = LONG_MAX; while (i < a.size() && j < b.size()) { res = min(res, abs((long)a[i] - (long)b[j])); - if (a[i] > b[j]) ++j; - else ++i; + if (a[i] > b[j]) + ++j; + else + ++i; } return res; } diff --git a/lcci/16.06.Smallest Difference/README_EN.md b/lcci/16.06.Smallest Difference/README_EN.md index 63524dafcb073..91f5ee15f6aaa 100644 --- a/lcci/16.06.Smallest Difference/README_EN.md +++ b/lcci/16.06.Smallest Difference/README_EN.md @@ -80,8 +80,10 @@ public: long res = LONG_MAX; while (i < a.size() && j < b.size()) { res = min(res, abs((long)a[i] - (long)b[j])); - if (a[i] > b[j]) ++j; - else ++i; + if (a[i] > b[j]) + ++j; + else + ++i; } return res; } diff --git a/lcci/16.06.Smallest Difference/Solution.cpp b/lcci/16.06.Smallest Difference/Solution.cpp index bf3569f8f3677..32eebd5b86c34 100644 --- a/lcci/16.06.Smallest Difference/Solution.cpp +++ b/lcci/16.06.Smallest Difference/Solution.cpp @@ -7,8 +7,10 @@ class Solution { long res = LONG_MAX; while (i < a.size() && j < b.size()) { res = min(res, abs((long)a[i] - (long)b[j])); - if (a[i] > b[j]) ++j; - else ++i; + if (a[i] > b[j]) + ++j; + else + ++i; } return res; } diff --git a/lcci/16.19.Pond Sizes/README.md b/lcci/16.19.Pond Sizes/README.md index b35add6644f7e..6d2faf30a9322 100644 --- a/lcci/16.19.Pond Sizes/README.md +++ b/lcci/16.19.Pond Sizes/README.md @@ -224,15 +224,12 @@ public: vector pondSizes(vector>& land) { int m = land.size(), n = land[0].size(); - for (int i = 0; i < m * n; ++i) - { + for (int i = 0; i < m * n; ++i) { p.push_back(i); size.push_back(1); } - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (land[i][j] != 0) continue; int idx = i * n + j; if (i < m - 1 && land[i + 1][j] == 0) unite(idx, (i + 1) * n + j); diff --git a/lcci/16.19.Pond Sizes/README_EN.md b/lcci/16.19.Pond Sizes/README_EN.md index 290ff6a19a12e..af7a915f371ff 100644 --- a/lcci/16.19.Pond Sizes/README_EN.md +++ b/lcci/16.19.Pond Sizes/README_EN.md @@ -172,15 +172,12 @@ public: vector pondSizes(vector>& land) { int m = land.size(), n = land[0].size(); - for (int i = 0; i < m * n; ++i) - { + for (int i = 0; i < m * n; ++i) { p.push_back(i); size.push_back(1); } - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (land[i][j] != 0) continue; int idx = i * n + j; if (i < m - 1 && land[i + 1][j] == 0) unite(idx, (i + 1) * n + j); diff --git a/lcci/16.19.Pond Sizes/Solution.cpp b/lcci/16.19.Pond Sizes/Solution.cpp index 353992de757fb..a0c53a6571f3a 100644 --- a/lcci/16.19.Pond Sizes/Solution.cpp +++ b/lcci/16.19.Pond Sizes/Solution.cpp @@ -5,15 +5,12 @@ class Solution { vector pondSizes(vector>& land) { int m = land.size(), n = land[0].size(); - for (int i = 0; i < m * n; ++i) - { + for (int i = 0; i < m * n; ++i) { p.push_back(i); size.push_back(1); } - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (land[i][j] != 0) continue; int idx = i * n + j; if (i < m - 1 && land[i + 1][j] == 0) unite(idx, (i + 1) * n + j); diff --git a/lcci/17.05.Find Longest Subarray/README.md b/lcci/17.05.Find Longest Subarray/README.md index dc74a7eb342d4..3fa5d71a25013 100644 --- a/lcci/17.05.Find Longest Subarray/README.md +++ b/lcci/17.05.Find Longest Subarray/README.md @@ -101,19 +101,14 @@ public: unordered_map seen; seen[0] = -1; int t = 0, mx = 0, j = 0; - for (int i = 0; i < array.size(); ++i) - { + for (int i = 0; i < array.size(); ++i) { t += isdigit(array[i][0]) ? 1 : -1; - if (seen.count(t)) - { - if (mx < i - seen[t]) - { + if (seen.count(t)) { + if (mx < i - seen[t]) { mx = i - seen[t]; j = seen[t] + 1; } - } - else - { + } else { seen[t] = i; } } diff --git a/lcci/17.05.Find Longest Subarray/README_EN.md b/lcci/17.05.Find Longest Subarray/README_EN.md index 83d9428d736af..37cfed06cf36e 100644 --- a/lcci/17.05.Find Longest Subarray/README_EN.md +++ b/lcci/17.05.Find Longest Subarray/README_EN.md @@ -99,19 +99,14 @@ public: unordered_map seen; seen[0] = -1; int t = 0, mx = 0, j = 0; - for (int i = 0; i < array.size(); ++i) - { + for (int i = 0; i < array.size(); ++i) { t += isdigit(array[i][0]) ? 1 : -1; - if (seen.count(t)) - { - if (mx < i - seen[t]) - { + if (seen.count(t)) { + if (mx < i - seen[t]) { mx = i - seen[t]; j = seen[t] + 1; } - } - else - { + } else { seen[t] = i; } } diff --git a/lcci/17.05.Find Longest Subarray/Solution.cpp b/lcci/17.05.Find Longest Subarray/Solution.cpp index 123785a3841c8..93c30bfe314c9 100644 --- a/lcci/17.05.Find Longest Subarray/Solution.cpp +++ b/lcci/17.05.Find Longest Subarray/Solution.cpp @@ -4,19 +4,14 @@ class Solution { unordered_map seen; seen[0] = -1; int t = 0, mx = 0, j = 0; - for (int i = 0; i < array.size(); ++i) - { + for (int i = 0; i < array.size(); ++i) { t += isdigit(array[i][0]) ? 1 : -1; - if (seen.count(t)) - { - if (mx < i - seen[t]) - { + if (seen.count(t)) { + if (mx < i - seen[t]) { mx = i - seen[t]; j = seen[t] + 1; } - } - else - { + } else { seen[t] = i; } } diff --git a/lcci/17.10.Find Majority Element/README.md b/lcci/17.10.Find Majority Element/README.md index 26a880e1537e6..50e56a8a3bcb5 100644 --- a/lcci/17.10.Find Majority Element/README.md +++ b/lcci/17.10.Find Majority Element/README.md @@ -129,14 +129,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } cnt = count(nums.begin(), nums.end(), m); return cnt > nums.size() / 2 ? m : -1; diff --git a/lcci/17.10.Find Majority Element/README_EN.md b/lcci/17.10.Find Majority Element/README_EN.md index 1c7f5d8147e1b..4e325ec7b0fb5 100644 --- a/lcci/17.10.Find Majority Element/README_EN.md +++ b/lcci/17.10.Find Majority Element/README_EN.md @@ -116,14 +116,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } cnt = count(nums.begin(), nums.end(), m); return cnt > nums.size() / 2 ? m : -1; diff --git a/lcci/17.10.Find Majority Element/Solution.cpp b/lcci/17.10.Find Majority Element/Solution.cpp index 24289f7cfe34f..ca5f3ac7afd6e 100644 --- a/lcci/17.10.Find Majority Element/Solution.cpp +++ b/lcci/17.10.Find Majority Element/Solution.cpp @@ -2,14 +2,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } cnt = count(nums.begin(), nums.end(), m); return cnt > nums.size() / 2 ? m : -1; diff --git a/lcci/17.11.Find Closest/README.md b/lcci/17.11.Find Closest/README.md index 22b84bc825184..e079031ca9021 100644 --- a/lcci/17.11.Find Closest/README.md +++ b/lcci/17.11.Find Closest/README.md @@ -133,11 +133,12 @@ class Solution { public: int findClosest(vector& words, string word1, string word2) { int i = 1e5, j = -1e5, ans = 1e5; - for (int k = 0; k < words.size(); ++k) - { + for (int k = 0; k < words.size(); ++k) { string word = words[k]; - if (word == word1) i = k; - else if (word == word2) j = k; + if (word == word1) + i = k; + else if (word == word2) + j = k; ans = min(ans, abs(i - j)); } return ans; diff --git a/lcci/17.11.Find Closest/README_EN.md b/lcci/17.11.Find Closest/README_EN.md index ee34ffb7c7799..3ea658804ea4c 100644 --- a/lcci/17.11.Find Closest/README_EN.md +++ b/lcci/17.11.Find Closest/README_EN.md @@ -129,11 +129,12 @@ class Solution { public: int findClosest(vector& words, string word1, string word2) { int i = 1e5, j = -1e5, ans = 1e5; - for (int k = 0; k < words.size(); ++k) - { + for (int k = 0; k < words.size(); ++k) { string word = words[k]; - if (word == word1) i = k; - else if (word == word2) j = k; + if (word == word1) + i = k; + else if (word == word2) + j = k; ans = min(ans, abs(i - j)); } return ans; diff --git a/lcci/17.11.Find Closest/Solution.cpp b/lcci/17.11.Find Closest/Solution.cpp index d3b00f657fb92..9dfedf9467f3a 100644 --- a/lcci/17.11.Find Closest/Solution.cpp +++ b/lcci/17.11.Find Closest/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: int findClosest(vector& words, string word1, string word2) { int i = 1e5, j = -1e5, ans = 1e5; - for (int k = 0; k < words.size(); ++k) - { + for (int k = 0; k < words.size(); ++k) { string word = words[k]; - if (word == word1) i = k; - else if (word == word2) j = k; + if (word == word1) + i = k; + else if (word == word2) + j = k; ans = min(ans, abs(i - j)); } return ans; diff --git a/lcci/17.12.BiNode/Solution.cpp b/lcci/17.12.BiNode/Solution.cpp index 3006fa05bbc07..36b487b40347a 100644 --- a/lcci/17.12.BiNode/Solution.cpp +++ b/lcci/17.12.BiNode/Solution.cpp @@ -10,7 +10,7 @@ class Solution { public: TreeNode* prev; - + TreeNode* convertBiNode(TreeNode* root) { TreeNode* dummy = new TreeNode(0, nullptr, root); prev = dummy; diff --git a/lcci/17.13.Re-Space/README.md b/lcci/17.13.Re-Space/README.md index 3f9ecd6d25288..8d889909fa44d 100644 --- a/lcci/17.13.Re-Space/README.md +++ b/lcci/17.13.Re-Space/README.md @@ -86,13 +86,10 @@ public: unordered_set s(dictionary.begin(), dictionary.end()); int n = sentence.size(); vector dp(n + 1); - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] + 1; - for (int j = 0; j < i; ++j) - { - if (s.count(sentence.substr(j, i - j))) - { + for (int j = 0; j < i; ++j) { + if (s.count(sentence.substr(j, i - j))) { dp[i] = min(dp[i], dp[j]); } } diff --git a/lcci/17.13.Re-Space/README_EN.md b/lcci/17.13.Re-Space/README_EN.md index c0404fb9c5064..fb953301f7bc1 100644 --- a/lcci/17.13.Re-Space/README_EN.md +++ b/lcci/17.13.Re-Space/README_EN.md @@ -84,13 +84,10 @@ public: unordered_set s(dictionary.begin(), dictionary.end()); int n = sentence.size(); vector dp(n + 1); - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] + 1; - for (int j = 0; j < i; ++j) - { - if (s.count(sentence.substr(j, i - j))) - { + for (int j = 0; j < i; ++j) { + if (s.count(sentence.substr(j, i - j))) { dp[i] = min(dp[i], dp[j]); } } diff --git a/lcci/17.13.Re-Space/Solution.cpp b/lcci/17.13.Re-Space/Solution.cpp index 784f7542b1d1b..c9c091543b8fa 100644 --- a/lcci/17.13.Re-Space/Solution.cpp +++ b/lcci/17.13.Re-Space/Solution.cpp @@ -4,13 +4,10 @@ class Solution { unordered_set s(dictionary.begin(), dictionary.end()); int n = sentence.size(); vector dp(n + 1); - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { dp[i] = dp[i - 1] + 1; - for (int j = 0; j < i; ++j) - { - if (s.count(sentence.substr(j, i - j))) - { + for (int j = 0; j < i; ++j) { + if (s.count(sentence.substr(j, i - j))) { dp[i] = min(dp[i], dp[j]); } } diff --git a/lcci/17.17.Multi Search/README.md b/lcci/17.17.Multi Search/README.md index f731e676cbc5f..e5383ba05398d 100644 --- a/lcci/17.17.Multi Search/README.md +++ b/lcci/17.17.Multi Search/README.md @@ -158,12 +158,13 @@ private: int idx; public: - Trie() : children(26), idx(-1) {} + Trie() + : children(26) + , idx(-1) { } void insert(string word, int i) { Trie* node = this; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new Trie(); node = node->children[idx]; @@ -174,8 +175,7 @@ public: vector search(string word) { Trie* node = this; vector res; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) return res; node = node->children[idx]; @@ -192,8 +192,7 @@ public: int n = smalls.size(); for (int i = 0; i < n; ++i) tree->insert(smalls[i], i); vector> ans(n); - for (int i = 0, m = big.size(); i < m; ++i) - { + for (int i = 0, m = big.size(); i < m; ++i) { string s = big.substr(i, m - i); vector t = tree->search(s); for (int& idx : t) ans[idx].push_back(i); diff --git a/lcci/17.17.Multi Search/README_EN.md b/lcci/17.17.Multi Search/README_EN.md index 7746914e40190..7af9fba228323 100644 --- a/lcci/17.17.Multi Search/README_EN.md +++ b/lcci/17.17.Multi Search/README_EN.md @@ -155,12 +155,13 @@ private: int idx; public: - Trie() : children(26), idx(-1) {} + Trie() + : children(26) + , idx(-1) { } void insert(string word, int i) { Trie* node = this; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new Trie(); node = node->children[idx]; @@ -171,8 +172,7 @@ public: vector search(string word) { Trie* node = this; vector res; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) return res; node = node->children[idx]; @@ -189,8 +189,7 @@ public: int n = smalls.size(); for (int i = 0; i < n; ++i) tree->insert(smalls[i], i); vector> ans(n); - for (int i = 0, m = big.size(); i < m; ++i) - { + for (int i = 0, m = big.size(); i < m; ++i) { string s = big.substr(i, m - i); vector t = tree->search(s); for (int& idx : t) ans[idx].push_back(i); diff --git a/lcci/17.17.Multi Search/Solution.cpp b/lcci/17.17.Multi Search/Solution.cpp index 231c036739ddf..be3f6d923223a 100644 --- a/lcci/17.17.Multi Search/Solution.cpp +++ b/lcci/17.17.Multi Search/Solution.cpp @@ -4,24 +4,24 @@ class Trie { int idx; public: - Trie() : children(26), idx(-1) {} - + Trie() + : children(26) + , idx(-1) { } + void insert(string word, int i) { Trie* node = this; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new Trie(); node = node->children[idx]; } node->idx = i; } - + vector search(string word) { Trie* node = this; vector res; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) return res; node = node->children[idx]; @@ -38,8 +38,7 @@ class Solution { int n = smalls.size(); for (int i = 0; i < n; ++i) tree->insert(smalls[i], i); vector> ans(n); - for (int i = 0, m = big.size(); i < m; ++i) - { + for (int i = 0, m = big.size(); i < m; ++i) { string s = big.substr(i, m - i); vector t = tree->search(s); for (int& idx : t) ans[idx].push_back(i); diff --git a/lcci/17.22.Word Transformer/README.md b/lcci/17.22.Word Transformer/README.md index 72fa3a796d4bd..a758912565080 100644 --- a/lcci/17.22.Word Transformer/README.md +++ b/lcci/17.22.Word Transformer/README.md @@ -144,13 +144,11 @@ public: void dfs(string begin, string end, vector& t) { if (!ans.empty()) return; - if (begin == end) - { + if (begin == end) { ans = t; return; } - for (auto word : words) - { + for (auto word : words) { if (visited.count(word) || !check(begin, word)) continue; visited.insert(word); t.push_back(word); diff --git a/lcci/17.22.Word Transformer/README_EN.md b/lcci/17.22.Word Transformer/README_EN.md index 4985e1f21d7db..bf6af35f0c9cc 100644 --- a/lcci/17.22.Word Transformer/README_EN.md +++ b/lcci/17.22.Word Transformer/README_EN.md @@ -155,13 +155,11 @@ public: void dfs(string begin, string end, vector& t) { if (!ans.empty()) return; - if (begin == end) - { + if (begin == end) { ans = t; return; } - for (auto word : words) - { + for (auto word : words) { if (visited.count(word) || !check(begin, word)) continue; visited.insert(word); t.push_back(word); diff --git a/lcci/17.22.Word Transformer/Solution.cpp b/lcci/17.22.Word Transformer/Solution.cpp index a5926ae2a2ade..53a7fc19c1ea0 100644 --- a/lcci/17.22.Word Transformer/Solution.cpp +++ b/lcci/17.22.Word Transformer/Solution.cpp @@ -15,13 +15,11 @@ class Solution { void dfs(string begin, string end, vector& t) { if (!ans.empty()) return; - if (begin == end) - { + if (begin == end) { ans = t; return; } - for (auto word : words) - { + for (auto word : words) { if (visited.count(word) || !check(begin, word)) continue; visited.insert(word); t.push_back(word); diff --git a/lcci/17.24.Max Submatrix/README.md b/lcci/17.24.Max Submatrix/README.md index 619ed041b856c..5bf00a62cd5bb 100644 --- a/lcci/17.24.Max Submatrix/README.md +++ b/lcci/17.24.Max Submatrix/README.md @@ -127,25 +127,21 @@ public: s[i + 1][j] = s[i][j] + matrix[i][j]; int mx = matrix[0][0]; vector ans(4); - for (int i1 = 0; i1 < m; ++i1) - { - for (int i2 = i1; i2 < m; ++i2) - { + for (int i1 = 0; i1 < m; ++i1) { + for (int i2 = i1; i2 < m; ++i2) { vector nums; for (int j = 0; j < n; ++j) nums.push_back(s[i2 + 1][j] - s[i1][j]); int start = 0; int f = nums[0]; - for (int j = 1; j < n; ++j) - { - if (f > 0) f += nums[j]; - else - { + for (int j = 1; j < n; ++j) { + if (f > 0) + f += nums[j]; + else { f = nums[j]; start = j; } - if (f > mx) - { + if (f > mx) { mx = f; ans[0] = i1; ans[1] = start; diff --git a/lcci/17.24.Max Submatrix/README_EN.md b/lcci/17.24.Max Submatrix/README_EN.md index 89577a32128c4..94dc92eade2a1 100644 --- a/lcci/17.24.Max Submatrix/README_EN.md +++ b/lcci/17.24.Max Submatrix/README_EN.md @@ -123,25 +123,21 @@ public: s[i + 1][j] = s[i][j] + matrix[i][j]; int mx = matrix[0][0]; vector ans(4); - for (int i1 = 0; i1 < m; ++i1) - { - for (int i2 = i1; i2 < m; ++i2) - { + for (int i1 = 0; i1 < m; ++i1) { + for (int i2 = i1; i2 < m; ++i2) { vector nums; for (int j = 0; j < n; ++j) nums.push_back(s[i2 + 1][j] - s[i1][j]); int start = 0; int f = nums[0]; - for (int j = 1; j < n; ++j) - { - if (f > 0) f += nums[j]; - else - { + for (int j = 1; j < n; ++j) { + if (f > 0) + f += nums[j]; + else { f = nums[j]; start = j; } - if (f > mx) - { + if (f > mx) { mx = f; ans[0] = i1; ans[1] = start; diff --git a/lcci/17.24.Max Submatrix/Solution.cpp b/lcci/17.24.Max Submatrix/Solution.cpp index a1a54a9786672..4f7843e6f39ab 100644 --- a/lcci/17.24.Max Submatrix/Solution.cpp +++ b/lcci/17.24.Max Submatrix/Solution.cpp @@ -8,25 +8,21 @@ class Solution { s[i + 1][j] = s[i][j] + matrix[i][j]; int mx = matrix[0][0]; vector ans(4); - for (int i1 = 0; i1 < m; ++i1) - { - for (int i2 = i1; i2 < m; ++i2) - { + for (int i1 = 0; i1 < m; ++i1) { + for (int i2 = i1; i2 < m; ++i2) { vector nums; for (int j = 0; j < n; ++j) nums.push_back(s[i2 + 1][j] - s[i1][j]); int start = 0; int f = nums[0]; - for (int j = 1; j < n; ++j) - { - if (f > 0) f += nums[j]; - else - { + for (int j = 1; j < n; ++j) { + if (f > 0) + f += nums[j]; + else { f = nums[j]; start = j; } - if (f > mx) - { + if (f > mx) { mx = f; ans[0] = i1; ans[1] = start; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.cpp" index d9c61179311b0..e00a25323e79e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.cpp" @@ -5,7 +5,7 @@ class Solution { for (int j = 0; j < board[0].size(); ++j) if (dfs(i, j, 0, board, word)) return 1; - return 0; + return 0; } bool dfs(int i, int j, int k, vector>& board, string word) { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/README.md" index b0e60f3676c31..c82c5e4f24988 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/README.md" @@ -121,8 +121,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n &= n - 1; ++ans; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.cpp" index 0c33aaeecd64c..a14f6399639df 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.cpp" @@ -2,8 +2,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n &= n - 1; ++ans; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" index 5a2f543768574..0f19075b2d14d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" @@ -139,10 +139,11 @@ func deleteNode(head *ListNode, val int) *ListNode { */ class Solution { public: - ListNode *deleteNode(ListNode *head, int val) { - ListNode *dummy = new ListNode(0, head); - ListNode *pre = dummy; - for (; pre->next && pre->next->val != val; pre = pre->next); + ListNode* deleteNode(ListNode* head, int val) { + ListNode* dummy = new ListNode(0, head); + ListNode* pre = dummy; + for (; pre->next && pre->next->val != val; pre = pre->next) + ; pre->next = pre->next ? pre->next->next : nullptr; return dummy->next; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.cpp" index b151ed5891d80..20e50023515ee 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.cpp" @@ -8,10 +8,11 @@ */ class Solution { public: - ListNode *deleteNode(ListNode *head, int val) { - ListNode *dummy = new ListNode(0, head); - ListNode *pre = dummy; - for (; pre->next && pre->next->val != val; pre = pre->next); + ListNode* deleteNode(ListNode* head, int val) { + ListNode* dummy = new ListNode(0, head); + ListNode* pre = dummy; + for (; pre->next && pre->next->val != val; pre = pre->next) + ; pre->next = pre->next ? pre->next->next : nullptr; return dummy->next; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" index 1e215d107c78b..47d650d7faa33 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" @@ -165,15 +165,24 @@ func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode { ### **C++** ```cpp +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ + class Solution { public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if (nullptr == l1 && nullptr == l2) { - return nullptr; // 两个都为空,则直接返回 + return nullptr; } if (nullptr == l1 || nullptr == l2) { - return l1 == nullptr ? l2 : l1; // 有且仅有一个为空,则返回非空节点 + return l1 == nullptr ? l2 : l1; } ListNode* node = nullptr; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.cpp" index 3b853e138a948..e949eafb21530 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.cpp" @@ -17,7 +17,7 @@ class Solution { if (nullptr == l1 || nullptr == l2) { return l1 == nullptr ? l2 : l1; } - + ListNode* node = nullptr; if (l1->val > l2->val) { node = l2; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" index effa8b84235d0..0278f1bc49f09 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" @@ -145,7 +145,7 @@ func mirrorTree(root *TreeNode) *TreeNode { class Solution { public: TreeNode* mirrorTree(TreeNode* root) { - // 后续遍历 + // 后序遍历 if (nullptr == root) { return nullptr; } @@ -157,7 +157,6 @@ public: return root; } }; - ``` ### **TypeScript** diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" index 8a98dc1458e14..e650f3935833b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.cpp" @@ -11,7 +11,6 @@ class Solution { public: TreeNode* mirrorTree(TreeNode* root) { - // 后续遍历 if (nullptr == root) { return nullptr; } @@ -21,6 +20,5 @@ class Solution { std::swap(root->left, root->right); return root; - } }; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" index 71aa6f12a4882..24b889c542006 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" @@ -107,6 +107,43 @@ class Solution { } ``` +### **C++** + +```cpp +class Solution { +public: + vector spiralOrder(vector>& matrix) { + vector ans; + if (matrix.size() == 0) + return ans; + int left = 0, top = 0, bottom = matrix.size() - 1, right = matrix[0].size() - 1; + while (true) { + for (int i = left; i <= right; i++) + ans.push_back(matrix[top][i]); + top++; + if (top > bottom) + break; + for (int i = top; i <= bottom; i++) + ans.push_back(matrix[i][right]); + right--; + if (right < left) + break; + for (int i = right; i >= left; i--) + ans.push_back(matrix[bottom][i]); + bottom--; + if (bottom < top) + break; + for (int i = bottom; i >= top; i--) + ans.push_back(matrix[i][left]); + left++; + if (left > right) + break; + } + return ans; + } +}; +``` + ### **JavaScript** ```js diff --git "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.cpp" index fe5d726550c41..7751270bf017b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.cpp" @@ -1,14 +1,11 @@ -class Solution -{ +class Solution { public: - vector spiralOrder(vector> &matrix) - { + vector spiralOrder(vector>& matrix) { vector ans; if (matrix.size() == 0) return ans; int left = 0, top = 0, bottom = matrix.size() - 1, right = matrix[0].size() - 1; - while (true) - { + while (true) { for (int i = left; i <= right; i++) ans.push_back(matrix[top][i]); top++; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" index 248a8f8416e29..6cbd82b056a2c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" @@ -167,14 +167,14 @@ func levelOrder(root *TreeNode) []int { */ class Solution { public: - vector levelOrder(TreeNode *root) { + vector levelOrder(TreeNode* root) { vector res; - queue q; + queue q; if (root != nullptr) { q.push(root); } while (!q.empty()) { - TreeNode *node = q.front(); + TreeNode* node = q.front(); q.pop(); if (node->left != nullptr) { q.push(node->left); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.cpp" index 2469cd42d071a..b0b034d1371b2 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.cpp" @@ -9,14 +9,14 @@ */ class Solution { public: - vector levelOrder(TreeNode *root) { + vector levelOrder(TreeNode* root) { vector res; - queue q; + queue q; if (root != nullptr) { q.push(root); } while (!q.empty()) { - TreeNode *node = q.front(); + TreeNode* node = q.front(); q.pop(); if (node->left != nullptr) { q.push(node->left); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" index 298095a9e5b95..5e9393a6d1baf 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" @@ -129,11 +129,9 @@ class Solution { public: void func(string str, int index, set& mySet) { if (index == str.size()) { - // 当轮训到最后一个字符的时候,直接放入set中。加入set结构,是为了避免插入的值重复 mySet.insert(str); } else { for (int i = index; i < str.size(); i++) { - // 从传入位置(index)开始算,固定第一个字符,然后后面的字符依次跟index位置交换 swap(str[i], str[index]); int temp = index + 1; func(str, temp, mySet); @@ -146,9 +144,7 @@ public: set mySet; func(s, 0, mySet); vector ret; - for (auto& x : mySet) { - /* 这一题加入mySet是为了进行结果的去重。 - 但由于在最后加入了将set转vector的过程,所以时间复杂度稍高 */ + for (string x : mySet) { ret.push_back(x); } return ret; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" index 3d3775e4dd0f8..e0664c88803ad 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/Solution.cpp" @@ -1,5 +1,5 @@ class Solution { -public: +public: void func(string str, int index, set& mySet) { if (index == str.size()) { mySet.insert(str); @@ -9,7 +9,7 @@ class Solution { int temp = index + 1; func(str, temp, mySet); swap(str[i], str[index]); - } + } } } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" index daebe9c1a46cd..03e9943eded72 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/README.md" @@ -113,14 +113,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } return m; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cpp" index 279972790d91b..f487480e340a0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.cpp" @@ -2,14 +2,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } return m; } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" index af65ad13eb4ec..3647e4a3e95b6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" @@ -201,7 +201,6 @@ public: private: priority_queue maxHeap; priority_queue, greater> minHeap; - }; ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.cpp" index a6cd93c377d73..c7db975b5e1f9 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.cpp" @@ -28,5 +28,4 @@ class MedianFinder { private: priority_queue maxHeap; priority_queue, greater> minHeap; - }; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index 26e1b33021850..e9bd131cdbdec 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -96,10 +96,8 @@ public: int lengthOfLongestSubstring(string s) { int res = 0; unordered_set chars; - for (int i = 0, j = 0; i < s.size(); ++i) - { - while (chars.count(s[i])) - { + for (int i = 0, j = 0; i < s.size(); ++i) { + while (chars.count(s[i])) { chars.erase(s[j++]); } chars.insert(s[i]); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" index 0e0445a7b3293..ac81364872165 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" @@ -3,10 +3,8 @@ class Solution { int lengthOfLongestSubstring(string s) { int res = 0; unordered_set chars; - for (int i = 0, j = 0; i < s.size(); ++i) - { - while (chars.count(s[i])) - { + for (int i = 0, j = 0; i < s.size(); ++i) { + while (chars.count(s[i])) { chars.erase(s[j++]); } chars.insert(s[i]); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" index 7f107f5c6adcc..a1868e0b039d2 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/README.md" @@ -150,7 +150,7 @@ var getIntersectionNode = function (headA, headB) { */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" index 32460fd2360cb..753db3cdd199c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23052. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\345\205\254\345\205\261\350\212\202\347\202\271/Solution.cpp" @@ -8,7 +8,7 @@ */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" index e21ec7c56363a..0c5a706c65f4b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" @@ -148,7 +148,7 @@ public: ret; } - while(small < mid) { + while (small < mid) { if (curSum == target) { ret.push_back(build(small, big)); } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.cpp" index 16c899bb62749..c15bd31c0f861 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.cpp" @@ -20,7 +20,7 @@ class Solution { ret; } - while(small < mid) { + while (small < mid) { if (curSum == target) { ret.push_back(build(small, big)); } diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" index c58e24ed605c9..186ffe282351c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" @@ -149,7 +149,7 @@ public: for (int p = 0, q = nums.size() - 1; p < q;) { int s = nums[p] + nums[q]; if (s == target) { - return vector{nums[p], nums[q]}; + return vector {nums[p], nums[q]}; } if (s < target) { ++p; @@ -157,7 +157,7 @@ public: --q; } } - return vector{}; + return vector {}; } }; ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.cpp" index 6fa1a869317b2..9e8da7dfd8ca0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.cpp" @@ -4,7 +4,7 @@ class Solution { for (int p = 0, q = nums.size() - 1; p < q;) { int s = nums[p] + nums[q]; if (s == target) { - return vector{nums[p], nums[q]}; + return vector {nums[p], nums[q]}; } if (s < target) { ++p; @@ -12,6 +12,6 @@ class Solution { --q; } } - return vector{}; + return vector {}; } }; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" index da6f27c8369d0..75dc014fb4a53 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -180,7 +180,7 @@ private: deque d; public: - MaxQueue() {} + MaxQueue() { } int max_value() { if (d.empty()) return -1; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" index 6baefee8a473f..26eedcf8affc6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" @@ -4,7 +4,7 @@ class MaxQueue { deque d; public: - MaxQueue() {} + MaxQueue() { } int max_value() { if (d.empty()) return -1; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" index da22a423af4cf..d5840fdfafdb5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" @@ -151,8 +151,8 @@ public: zeroNum++; } - for (int i = zeroNum; i < nums.size()-1; i++) { - if (nums[i] == nums[i+1]) { + for (int i = zeroNum; i < nums.size() - 1; i++) { + if (nums[i] == nums[i + 1]) { return false; } } @@ -160,7 +160,6 @@ public: return nums[4] - nums[zeroNum] <= 4; } }; - ``` ### **Go** diff --git "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.cpp" index 66bbfb8bc5e92..4fbd87e87acee 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.cpp" @@ -15,12 +15,12 @@ class Solution { zeroNum++; } - for (int i = zeroNum; i < nums.size()-1; i++) { - if (nums[i] == nums[i+1]) { + for (int i = zeroNum; i < nums.size() - 1; i++) { + if (nums[i] == nums[i + 1]) { return false; } } - + return nums[4] - nums[zeroNum] <= 4; } }; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" index 35935089923e3..4e446accc5985 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" @@ -114,16 +114,15 @@ class Solution { public: int maxProfit(vector& prices) { if (prices.size() < 2) { - return 0; // 如果小于两个,直接返回0值 + return 0; } int curMin = prices[0]; int maxDiff = prices[1] - prices[0]; - // 贪心循环,记录当前最小值,和最大diff值 for (int i = 2; i < prices.size(); i++) { - if (curMin > prices[i-1]) { - curMin = prices[i-1]; + if (curMin > prices[i - 1]) { + curMin = prices[i - 1]; } int diff = prices[i] - curMin; @@ -132,7 +131,6 @@ public: } } - // 根据题意,如果是负数的话,则返回0值 return maxDiff > 0 ? maxDiff : 0; } }; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.cpp" index 40d906451f7a5..da9efe72817ac 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.cpp" @@ -9,8 +9,8 @@ class Solution { int maxDiff = prices[1] - prices[0]; for (int i = 2; i < prices.size(); i++) { - if (curMin > prices[i-1]) { - curMin = prices[i-1]; + if (curMin > prices[i - 1]) { + curMin = prices[i - 1]; } int diff = prices[i] - curMin; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" index 94b9e6ef5d160..83e932d00b05e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" @@ -56,12 +56,12 @@ class Solution { ```cpp class Solution { - public int sumNums(int n) { - int s = n; - boolean t = n > 0 && (s += sumNums(n - 1)) > 0; - return s; +public: + int sumNums(int n) { + n && (n += sumNums(n - 1)); + return n; } -} +}; ``` ### **JavaScript** diff --git "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.cpp" index dcf3592ab0f0d..54c9c11c93ea4 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.cpp" @@ -1,4 +1,4 @@ -class Solution{ +class Solution { public: int sumNums(int n) { n && (n += sumNums(n - 1)); diff --git "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" index 89e5dec4d1f2d..7aea2854a66b7 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" @@ -96,8 +96,7 @@ class Solution { class Solution { public: int add(int a, int b) { - while (b) - { + while (b) { unsigned int carry = (unsigned int)(a & b) << 1; a = a ^ b; b = carry; diff --git "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.cpp" "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.cpp" index 9d2b8cf9d9db1..ee146b68f5509 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.cpp" +++ "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.cpp" @@ -1,8 +1,7 @@ class Solution { public: int add(int a, int b) { - while (b) - { + while (b) { unsigned int carry = (unsigned int)(a & b) << 1; a = a ^ b; b = carry; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" index 95ba47852bfb3..12335061b6b20 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" @@ -113,11 +113,9 @@ class Solution { public: int singleNumber(vector& nums) { int ans = 0; - for (int i = 0; i < 32; ++i) - { + for (int i = 0; i < 32; ++i) { int cnt = 0; - for (int num : nums) - { + for (int num : nums) { cnt += ((num >> i) & 1); } cnt %= 3; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" index 9b8a43b1904e2..689660fc4046a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 004. \345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" @@ -2,11 +2,9 @@ class Solution { public: int singleNumber(vector& nums) { int ans = 0; - for (int i = 0; i < 32; ++i) - { + for (int i = 0; i < 32; ++i) { int cnt = 0; - for (int num : nums) - { + for (int num : nums) { cnt += ((num >> i) & 1); } cnt %= 3; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/README.md" index 0dece1684c24e..adda00fee2863 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/README.md" @@ -144,7 +144,7 @@ public: for (int i = 0; i < n - 1; ++i) for (int j = i + 1; j < n; ++j) if (!(mask[i] & mask[j])) - ans = max(ans, (int) words[i].size() * (int) words[j].size()); + ans = max(ans, (int)words[i].size() * (int)words[j].size()); return ans; } }; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/Solution.cpp" index 087e5cb0932af..22220c211ac3c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 005. \345\215\225\350\257\215\351\225\277\345\272\246\347\232\204\346\234\200\345\244\247\344\271\230\347\247\257/Solution.cpp" @@ -10,7 +10,7 @@ class Solution { for (int i = 0; i < n - 1; ++i) for (int j = i + 1; j < n; ++j) if (!(mask[i] & mask[j])) - ans = max(ans, (int) words[i].size() * (int) words[j].size()); + ans = max(ans, (int)words[i].size() * (int)words[j].size()); return ans; } }; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" index 494c0a53565f8..60b3f7811e064 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/README.md" @@ -115,36 +115,28 @@ func twoSum(numbers []int, target int) []int { ### **C++** ```cpp -class Solution -{ - public: - vector twoSum( vector & numbers, int target ) - { - int i = 0; - int j = numbers.size() - 1; - vector res; - - while (i < j) - { - int sum = numbers[i] + numbers[j]; - if(sum < target) - { - i++; - } - else if (sum > target) - { - j--; - } - else - { - res.push_back(i); - res.push_back(j); - break; - } +class Solution { +public: + vector twoSum(vector& numbers, int target) { + int i = 0; + int j = numbers.size() - 1; + vector res; + + while (i < j) { + int sum = numbers[i] + numbers[j]; + if (sum < target) { + i++; + } else if (sum > target) { + j--; + } else { + res.push_back(i); + res.push_back(j); + break; } - - return res; } + + return res; + } }; ``` diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" index 4df337098f3f2..8b770c3ab2e42 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 006. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\344\270\244\344\270\252\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" @@ -1,31 +1,23 @@ -class Solution -{ - public: - vector twoSum( vector & numbers, int target ) - { - int i = 0; - int j = numbers.size() - 1; - vector res; +class Solution { +public: + vector twoSum(vector& numbers, int target) { + int i = 0; + int j = numbers.size() - 1; + vector res; - while (i < j) - { - int sum = numbers[i] + numbers[j]; - if(sum < target) - { - i++; - } - else if (sum > target) - { - j--; - } - else - { - res.push_back(i); - res.push_back(j); - break; - } + while (i < j) { + int sum = numbers[i] + numbers[j]; + if (sum < target) { + i++; + } else if (sum > target) { + j--; + } else { + res.push_back(i); + res.push_back(j); + break; } - - return res; } + + return res; + } }; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" index f785e51432a2a..4c66b486e7557 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/README.md" @@ -167,14 +167,14 @@ public: int j = nums.size() - 1; if (k > 0 && nums[k] == nums[k - 1]) continue; - while(i < j) { + while (i < j) { if (nums[i] + nums[j] + nums[k] == 0) { - res.push_back(vector{nums[k], nums[i], nums[j]}); + res.push_back(vector {nums[k], nums[i], nums[j]}); i++; j--; - while(i < j && nums[i] == nums[i - 1]) i++; - while(i < j && nums[j] == nums[j + 1]) j--; + while (i < j && nums[i] == nums[i - 1]) i++; + while (i < j && nums[j] == nums[j + 1]) j--; } else if (nums[i] + nums[j] + nums[k] < 0) { i++; } else { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.cpp" index 8f9d66ba96935..cf353be56a4a1 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 007. \346\225\260\347\273\204\344\270\255\345\222\214\344\270\272 0 \347\232\204\344\270\211\344\270\252\346\225\260/Solution.cpp" @@ -9,14 +9,14 @@ class Solution { int j = nums.size() - 1; if (k > 0 && nums[k] == nums[k - 1]) continue; - while(i < j) { + while (i < j) { if (nums[i] + nums[j] + nums[k] == 0) { - res.push_back(vector{nums[k], nums[i], nums[j]}); + res.push_back(vector {nums[k], nums[i], nums[j]}); i++; j--; - while(i < j && nums[i] == nums[i - 1]) i++; - while(i < j && nums[j] == nums[j + 1]) j--; + while (i < j && nums[i] == nums[i - 1]) i++; + while (i < j && nums[j] == nums[j + 1]) j--; } else if (nums[i] + nums[j] + nums[k] < 0) { i++; } else { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/README.md" index 91a78e9c29b31..76dcfb156002e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/README.md" @@ -123,11 +123,11 @@ public: for (right = 0; right < nums.size(); right++) { mul *= nums[right]; - while(left <= right && mul >= k) { + while (left <= right && mul >= k) { mul /= nums[left++]; } - count += right >= left? right - left + 1: 0; + count += right >= left ? right - left + 1 : 0; } return count; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" index c98f2e1efdafa..0c127f9de8ae2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 009. \344\271\230\347\247\257\345\260\217\344\272\216 K \347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" @@ -8,11 +8,11 @@ class Solution { for (right = 0; right < nums.size(); right++) { mul *= nums[right]; - while(left <= right && mul >= k) { + while (left <= right && mul >= k) { mul /= nums[left++]; } - count += right >= left? right - left + 1: 0; + count += right >= left ? right - left + 1 : 0; } return count; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/README.md" index ecd8836571c34..5b1c26358e8a8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/README.md" @@ -101,11 +101,12 @@ public: unordered_map mp; int s = 0, ans = 0; mp[0] = -1; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { s += nums[i] == 1 ? 1 : -1; - if (mp.count(s)) ans = max(ans, i - mp[s]); - else mp[s] = i; + if (mp.count(s)) + ans = max(ans, i - mp[s]); + else + mp[s] = i; } return ans; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" index bf4a907bf60b8..8344d29f987e5 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 011. 0 \345\222\214 1 \344\270\252\346\225\260\347\233\270\345\220\214\347\232\204\345\255\220\346\225\260\347\273\204/Solution.cpp" @@ -4,11 +4,12 @@ class Solution { unordered_map mp; int s = 0, ans = 0; mp[0] = -1; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { s += nums[i] == 1 ? 1 : -1; - if (mp.count(s)) ans = max(ans, i - mp[s]); - else mp[s] = i; + if (mp.count(s)) + ans = max(ans, i - mp[s]); + else + mp[s] = i; } return ans; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" index 2a5bfa17b467e..01f3ddfee97b3 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/README.md" @@ -129,7 +129,7 @@ public: int pivotIndex(vector& nums) { int sum = 0; int total = 0; - for (int num: nums) + for (int num : nums) sum += num; for (int i = 0; i < nums.size(); i++) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.cpp" index 935fc996fcb65..f6268bf4512e2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 012. \345\267\246\345\217\263\344\270\244\350\276\271\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214\347\233\270\347\255\211/Solution.cpp" @@ -3,7 +3,7 @@ class Solution { int pivotIndex(vector& nums) { int sum = 0; int total = 0; - for (int num: nums) + for (int num : nums) sum += num; for (int i = 0; i < nums.size(); i++) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 013. \344\272\214\347\273\264\345\255\220\347\237\251\351\230\265\347\232\204\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 013. \344\272\214\347\273\264\345\255\220\347\237\251\351\230\265\347\232\204\345\222\214/Solution.cpp" index fa557aabd9080..0d263f6b969cd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 013. \344\272\214\347\273\264\345\255\220\347\237\251\351\230\265\347\232\204\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 013. \344\272\214\347\273\264\345\255\220\347\237\251\351\230\265\347\232\204\345\222\214/Solution.cpp" @@ -11,7 +11,7 @@ class NumMatrix { } } } - + int sumRegion(int row1, int col1, int row2, int col2) { return pre[row2 + 1][col2 + 1] - pre[row2 + 1][col1] - pre[row1][col2 + 1] + pre[row1][col1]; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 014. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 014. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" index 6156861af4cf1..50a4da5944a15 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 014. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 014. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" @@ -36,7 +36,7 @@ class Solution { if (r + 1 >= len2) { break; } - + ++count[s2[l++] - 'a']; --count[s2[++r] - 'a']; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/README.md" index e7ad731011823..5ce3918547921 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/README.md" @@ -178,7 +178,6 @@ public: res.push_back(i - p.size() + 1); } - return res; } }; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/Solution.cpp" index 2e7e90ec0a022..65740af8b980c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 015. \345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\346\211\200\346\234\211\345\217\230\344\275\215\350\257\215/Solution.cpp" @@ -23,7 +23,6 @@ class Solution { res.push_back(i - p.size() + 1); } - return res; } }; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" index b98c4f4861c7a..db32005c4ab80 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/README.md" @@ -160,7 +160,7 @@ public class Solution { */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" index c5ce3fefb03b8..890916dbc2485 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 022. \351\223\276\350\241\250\344\270\255\347\216\257\347\232\204\345\205\245\345\217\243\350\212\202\347\202\271/Solution.cpp" @@ -8,7 +8,7 @@ */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" index 137df18b158cd..bdfa3292d0f22 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/README.md" @@ -148,7 +148,7 @@ public class Solution { */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" index 32460fd2360cb..753db3cdd199c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 023. \344\270\244\344\270\252\351\223\276\350\241\250\347\232\204\347\254\254\344\270\200\344\270\252\351\207\215\345\220\210\350\212\202\347\202\271/Solution.cpp" @@ -8,7 +8,7 @@ */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode* cur1 = headA; ListNode* cur2 = headB; while (cur1 != cur2) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" index c315726863194..4c984a642c110 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" @@ -197,8 +197,7 @@ public: ListNode* reverseList(ListNode* head) { ListNode* pre = nullptr; ListNode* p = head; - while (p) - { + while (p) { ListNode* q = p->next; p->next = pre; pre = p; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.cpp" index fe2511dba73ec..100019aead84c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.cpp" @@ -13,8 +13,7 @@ class Solution { ListNode* reverseList(ListNode* head) { ListNode* pre = nullptr; ListNode* p = head; - while (p) - { + while (p) { ListNode* q = p->next; p->next = pre; pre = p; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/README.md" index 8bf8dfa0fdf3d..ff52239e952fe 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/README.md" @@ -148,15 +148,12 @@ public: for (; l2; l2 = l2->next) s2.push(l2->val); int carry = 0; ListNode* dummy = new ListNode(); - while (!s1.empty() || !s2.empty() || carry) - { - if (!s1.empty()) - { + while (!s1.empty() || !s2.empty() || carry) { + if (!s1.empty()) { carry += s1.top(); s1.pop(); } - if (!s2.empty()) - { + if (!s2.empty()) { carry += s2.top(); s2.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/Solution.cpp" index d14b2e87bcafe..5a2cb2da5bb2f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 025. \351\223\276\350\241\250\344\270\255\347\232\204\344\270\244\346\225\260\347\233\270\345\212\240/Solution.cpp" @@ -17,15 +17,12 @@ class Solution { for (; l2; l2 = l2->next) s2.push(l2->val); int carry = 0; ListNode* dummy = new ListNode(); - while (!s1.empty() || !s2.empty() || carry) - { - if (!s1.empty()) - { + while (!s1.empty() || !s2.empty() || carry) { + if (!s1.empty()) { carry += s1.top(); s1.pop(); } - if (!s2.empty()) - { + if (!s2.empty()) { carry += s2.top(); s2.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/README.md" index 9c6f49f36139d..7a54425d9b4bd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/README.md" @@ -191,8 +191,7 @@ public: ListNode* middleNode(ListNode* head) { ListNode* slow = head; ListNode* fast = head; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -202,8 +201,7 @@ public: ListNode* reverseList(ListNode* head) { ListNode* pre = nullptr; ListNode* cur = head; - while (cur) - { + while (cur) { ListNode* tmp = cur->next; cur->next = pre; pre = cur; @@ -215,8 +213,7 @@ public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { + while (l1 && l2) { cur->next = l1; l1 = l1->next; cur = cur->next; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/Solution.cpp" index c817e23440828..45a4d71e4369e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 026. \351\207\215\346\216\222\351\223\276\350\241\250/Solution.cpp" @@ -21,8 +21,7 @@ class Solution { ListNode* middleNode(ListNode* head) { ListNode* slow = head; ListNode* fast = head; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -32,8 +31,7 @@ class Solution { ListNode* reverseList(ListNode* head) { ListNode* pre = nullptr; ListNode* cur = head; - while (cur) - { + while (cur) { ListNode* tmp = cur->next; cur->next = pre; pre = cur; @@ -45,8 +43,7 @@ class Solution { ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { + while (l1 && l2) { cur->next = l1; l1 = l1->next; cur = cur->next; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/README.md" index 660bac6ac76bd..a8a6eb7057a8d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/README.md" @@ -147,22 +147,19 @@ public: if (!head || !head->next) return true; ListNode* slow = head; ListNode* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } ListNode* pre = nullptr; ListNode* cur = slow->next; - while (cur) - { + while (cur) { ListNode* t = cur->next; cur->next = pre; pre = cur; cur = t; } - while (pre) - { + while (pre) { if (pre->val != head->val) return false; pre = pre->next; head = head->next; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/Solution.cpp" index 2e59862d01ab4..04c0173e6011a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 027. \345\233\236\346\226\207\351\223\276\350\241\250/Solution.cpp" @@ -14,22 +14,19 @@ class Solution { if (!head || !head->next) return true; ListNode* slow = head; ListNode* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } ListNode* pre = nullptr; ListNode* cur = slow->next; - while (cur) - { + while (cur) { ListNode* t = cur->next; cur->next = pre; pre = cur; cur = t; } - while (pre) - { + while (pre) { if (pre->val != head->val) return false; pre = pre->next; head = head->next; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/README.md" index ca703e213c75f..bd59bbcb172a4 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/README.md" @@ -175,14 +175,12 @@ class Solution { public: Node* insert(Node* head, int insertVal) { Node* node = new Node(insertVal); - if (!head) - { + if (!head) { node->next = node; return node; } Node *prev = head, *curr = head->next; - while (curr != head) - { + while (curr != head) { if ((prev->val <= insertVal && insertVal <= curr->val) || (prev->val > curr->val && (insertVal >= prev->val || insertVal <= curr->val))) break; prev = curr; curr = curr->next; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/Solution.cpp" index 6a51ad86e2075..84580318378eb 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 029. \346\216\222\345\272\217\347\232\204\345\276\252\347\216\257\351\223\276\350\241\250/Solution.cpp" @@ -23,14 +23,12 @@ class Solution { public: Node* insert(Node* head, int insertVal) { Node* node = new Node(insertVal); - if (!head) - { + if (!head) { node->next = node; return node; } Node *prev = head, *curr = head->next; - while (curr != head) - { + while (curr != head) { if ((prev->val <= insertVal && insertVal <= curr->val) || (prev->val > curr->val && (insertVal >= prev->val || insertVal <= curr->val))) break; prev = curr; curr = curr->next; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/README.md" index 0fec0b8095b6f..eda13ae32d239 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/README.md" @@ -184,9 +184,9 @@ class RandomizedSet { class RandomizedSet { unordered_map mp; vector nums; + public: RandomizedSet() { - } bool insert(int val) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/Solution.cpp" index 6c8672294bdd6..e73dd7245a28b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 030. \346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\351\232\217\346\234\272\350\256\277\351\227\256\351\203\275\346\230\257 O(1) \347\232\204\345\256\271\345\231\250/Solution.cpp" @@ -1,9 +1,9 @@ class RandomizedSet { unordered_map mp; vector nums; + public: RandomizedSet() { - } bool insert(int val) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/README.md" index 06605d729489e..e06e74e77ebdc 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/README.md" @@ -109,13 +109,11 @@ public: if (s.size() != t.size() || s == t) return false; vector chars(26, 0); - for (int i = 0, n = s.size(); i < n; ++i) - { + for (int i = 0, n = s.size(); i < n; ++i) { ++chars[s[i] - 'a']; --chars[t[i] - 'a']; } - for (int c : chars) - { + for (int c : chars) { if (c != 0) return false; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" index d49f889ea726d..1a5a01648df70 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 032. \346\234\211\346\225\210\347\232\204\345\217\230\344\275\215\350\257\215/Solution.cpp" @@ -4,13 +4,11 @@ class Solution { if (s.size() != t.size() || s == t) return false; vector chars(26, 0); - for (int i = 0, n = s.size(); i < n; ++i) - { + for (int i = 0, n = s.size(); i < n; ++i) { ++chars[s[i] - 'a']; --chars[t[i] - 'a']; } - for (int c : chars) - { + for (int c : chars) { if (c != 0) return false; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" index ce0bec02774fa..c88ee25ef810b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/README.md" @@ -97,17 +97,15 @@ class Solution { ```cpp class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.cpp" index 5b530c70aa9ec..2e25d4fe06c4f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 033. \345\217\230\344\275\215\350\257\215\347\273\204/Solution.cpp" @@ -1,16 +1,14 @@ class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/README.md" index 7fe6eb4e39c29..c6e6184b2e777 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/README.md" @@ -140,17 +140,15 @@ function isAlienSorted(words: string[], order: string): boolean { ```cpp class Solution { public: - bool isAlienSorted(vector &words, string order) { + bool isAlienSorted(vector& words, string order) { vector index(26); for (int i = 0; i < index.size(); ++i) index[order[i] - 'a'] = i; - for (int i = 0; i < words.size() - 1; ++i) - { + for (int i = 0; i < words.size() - 1; ++i) { string w1 = words[i]; string w2 = words[i + 1]; int l1 = w1.size(), l2 = w2.size(); - for (int j = 0; j < max(l1, l2); ++j) - { + for (int j = 0; j < max(l1, l2); ++j) { int i1 = j >= l1 ? -1 : index[w1[j] - 'a']; int i2 = j >= l2 ? -1 : index[w2[j] - 'a']; if (i1 > i2) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/Solution.cpp" index 4e5b41a2ca977..33fadc246be08 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 034. \345\244\226\346\230\237\350\257\255\350\250\200\346\230\257\345\220\246\346\216\222\345\272\217/Solution.cpp" @@ -1,16 +1,14 @@ class Solution { public: - bool isAlienSorted(vector &words, string order) { + bool isAlienSorted(vector& words, string order) { vector index(26); for (int i = 0; i < index.size(); ++i) index[order[i] - 'a'] = i; - for (int i = 0; i < words.size() - 1; ++i) - { + for (int i = 0; i < words.size() - 1; ++i) { string w1 = words[i]; string w2 = words[i + 1]; int l1 = w1.size(), l2 = w2.size(); - for (int j = 0; j < max(l1, l2); ++j) - { + for (int j = 0; j < max(l1, l2); ++j) { int i1 = j >= l1 ? -1 : index[w1[j] - 'a']; int i2 = j >= l2 ? -1 : index[w2[j] - 'a']; if (i1 > i2) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/README.md" index c703618881016..05eb43f9ba5e8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/README.md" @@ -99,7 +99,7 @@ class Solution { ```cpp class Solution { public: - int findMinDifference(vector &timePoints) { + int findMinDifference(vector& timePoints) { if (timePoints.size() > 24 * 60) return 0; vector mins; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/Solution.cpp" index 18c4fd094f2f1..16bf8b7636d04 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 035. \346\234\200\345\260\217\346\227\266\351\227\264\345\267\256/Solution.cpp" @@ -1,6 +1,6 @@ class Solution { public: - int findMinDifference(vector &timePoints) { + int findMinDifference(vector& timePoints) { if (timePoints.size() > 24 * 60) return 0; vector mins; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/README.md" index 5f10c2ce53e25..e2e9128ee74e5 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/README.md" @@ -158,20 +158,21 @@ public: int evalRPN(vector& tokens) { stack stk; for (auto& t : tokens) { - if (t.size() > 1 || isdigit(t[0])) - { + if (t.size() > 1 || isdigit(t[0])) { stk.push(stoi(t)); - } - else - { + } else { int y = stk.top(); stk.pop(); int x = stk.top(); stk.pop(); - if (t[0] == '+') stk.push(x + y); - else if (t[0] == '-') stk.push(x - y); - else if (t[0] == '*') stk.push(x * y); - else stk.push(x / y); + if (t[0] == '+') + stk.push(x + y); + else if (t[0] == '-') + stk.push(x - y); + else if (t[0] == '*') + stk.push(x * y); + else + stk.push(x / y); } } return stk.top(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/Solution.cpp" index 0c346abfadc81..67ebfcb3ec300 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 036. \345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/Solution.cpp" @@ -3,20 +3,21 @@ class Solution { int evalRPN(vector& tokens) { stack stk; for (auto& t : tokens) { - if (t.size() > 1 || isdigit(t[0])) - { + if (t.size() > 1 || isdigit(t[0])) { stk.push(stoi(t)); - } - else - { + } else { int y = stk.top(); stk.pop(); int x = stk.top(); stk.pop(); - if (t[0] == '+') stk.push(x + y); - else if (t[0] == '-') stk.push(x - y); - else if (t[0] == '*') stk.push(x * y); - else stk.push(x / y); + if (t[0] == '+') + stk.push(x + y); + else if (t[0] == '-') + stk.push(x - y); + else if (t[0] == '*') + stk.push(x * y); + else + stk.push(x / y); } } return stk.top(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" index f31f75cafa586..18e496643a141 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/README.md" @@ -140,14 +140,12 @@ class Solution { ```cpp class Solution { public: - vector dailyTemperatures(vector &temperatures) { + vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); vector ans(n); stack stk; - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { ans[stk.top()] = i - stk.top(); stk.pop(); } @@ -165,8 +163,7 @@ public: int n = temperatures.size(); vector ans(n); stack stk; - for (int i = n - 1; ~i; --i) - { + for (int i = n - 1; ~i; --i) { while (!stk.empty() && temperatures[stk.top()] <= temperatures[i]) stk.pop(); if (!stk.empty()) ans[i] = stk.top() - i; stk.push(i); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/Solution.cpp" index 5b49f3c253825..7b71f8274ef28 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 038. \346\257\217\346\227\245\346\270\251\345\272\246/Solution.cpp" @@ -1,13 +1,11 @@ class Solution { public: - vector dailyTemperatures(vector &temperatures) { + vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); vector ans(n); stack stk; - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { ans[stk.top()] = i - stk.top(); stk.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/README.md" index a7adc36515f8a..0a30c0945bc47 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/README.md" @@ -182,22 +182,20 @@ public: int m = matrix[0].size(); int ans = 0; stack st; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { h[j] = (matrix[i][j] == '1' ? h[j] + 1 : 0); - while (st.size() && h[j] <= h[st.top()]) - { + while (st.size() && h[j] <= h[st.top()]) { ans = max(ans, (j - l[st.top()] - 1) * h[st.top()]); st.pop(); } - if (st.size()) l[j] = st.top(); - else l[j] = -1; + if (st.size()) + l[j] = st.top(); + else + l[j] = -1; st.push(j); } - while (st.size()) - { + while (st.size()) { ans = max(ans, (m - 1 - l[st.top()]) * h[st.top()]); st.pop(); } @@ -215,10 +213,8 @@ public: int n = matrix[0].size(); vector heights(n); int ans = 0; - for (auto& row : matrix) - { - for (int j = 0; j < n; ++j) - { + for (auto& row : matrix) { + for (int j = 0; j < n; ++j) { if (row[j] == '1') ++heights[j]; else heights[j] = 0; } @@ -232,10 +228,8 @@ public: stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/Solution.cpp" index 02a0eb3f7e928..f4c34b7ea737d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 040. \347\237\251\351\230\265\344\270\255\346\234\200\345\244\247\347\232\204\347\237\251\345\275\242/Solution.cpp" @@ -8,22 +8,20 @@ class Solution { int m = matrix[0].size(); int ans = 0; stack st; - for (int i = 0; i < n; i++) - { - for (int j = 0; j < m; j++) - { + for (int i = 0; i < n; i++) { + for (int j = 0; j < m; j++) { h[j] = (matrix[i][j] == '1' ? h[j] + 1 : 0); - while (st.size() && h[j] <= h[st.top()]) - { + while (st.size() && h[j] <= h[st.top()]) { ans = max(ans, (j - l[st.top()] - 1) * h[st.top()]); st.pop(); } - if (st.size()) l[j] = st.top(); - else l[j] = -1; + if (st.size()) + l[j] = st.top(); + else + l[j] = -1; st.push(j); } - while (st.size()) - { + while (st.size()) { ans = max(ans, (m - 1 - l[st.top()]) * h[st.top()]); st.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/README.md" index 93804fff23df1..55d6cb9333d09 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/README.md" @@ -173,7 +173,7 @@ public: s += val - arr[idx]; arr[idx] = val; ++cnt; - return (double) s / min(cnt, (int) arr.size()); + return (double)s / min(cnt, (int)arr.size()); } private: @@ -197,8 +197,7 @@ public: } double next(int val) { - if (q.size() == n) - { + if (q.size() == n) { s -= q.front(); q.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/Solution.cpp" index 83001cb699e51..a5886735c121b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 041. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\345\271\263\345\235\207\345\200\274/Solution.cpp" @@ -3,13 +3,13 @@ class MovingAverage { MovingAverage(int size) { arr.resize(size); } - + double next(int val) { int idx = cnt % arr.size(); s += val - arr[idx]; arr[idx] = val; ++cnt; - return (double) s / min(cnt, (int) arr.size()); + return (double)s / min(cnt, (int)arr.size()); } private: diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" index c9530594d6ce9..253cb68aca589 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/README.md" @@ -116,7 +116,6 @@ public: deque q; RecentCounter() { - } int ping(int t) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.cpp" index f9efde6459894..3b28b2c570b3c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 042. \346\234\200\350\277\221\350\257\267\346\261\202\346\254\241\346\225\260/Solution.cpp" @@ -3,9 +3,8 @@ class RecentCounter { deque q; RecentCounter() { - } - + int ping(int t) { q.push_back(t); while (q.front() < t - 3000) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" index 4e9789387bf62..63f1561b955eb 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/README.md" @@ -182,9 +182,8 @@ public: vector tree; CBTInserter(TreeNode* root) { - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { auto node = q.front(); q.pop(); tree.push_back(node); @@ -198,8 +197,10 @@ public: TreeNode* node = new TreeNode(v); tree.push_back(node); TreeNode* p = tree[pid]; - if (!p->left) p->left = node; - else p->right = node; + if (!p->left) + p->left = node; + else + p->right = node; return p->val; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.cpp" index 914b9e79fc201..80f786748d2f8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 043. \345\276\200\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221\346\267\273\345\212\240\350\212\202\347\202\271/Solution.cpp" @@ -14,9 +14,8 @@ class CBTInserter { vector tree; CBTInserter(TreeNode* root) { - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { auto node = q.front(); q.pop(); tree.push_back(node); @@ -24,17 +23,19 @@ class CBTInserter { if (node->right) q.push(node->right); } } - + int insert(int v) { int pid = tree.size() - 1 >> 1; TreeNode* node = new TreeNode(v); tree.push_back(node); TreeNode* p = tree[pid]; - if (!p->left) p->left = node; - else p->right = node; + if (!p->left) + p->left = node; + else + p->right = node; return p->val; } - + TreeNode* get_root() { return tree[0]; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/README.md" index 88537a21c549d..d9736e7ff2045 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -173,13 +173,11 @@ class Solution { public: vector largestValues(TreeNode* root) { if (!root) return {}; - queue q{{root}}; + queue q {{root}}; vector ans; - while (!q.empty()) - { + while (!q.empty()) { int t = INT_MIN; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { auto node = q.front(); q.pop(); t = max(t, node->val); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" index a2e297a96ed36..f7316713ca755 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 044. \344\272\214\345\217\211\346\240\221\346\257\217\345\261\202\347\232\204\346\234\200\345\244\247\345\200\274/Solution.cpp" @@ -13,13 +13,11 @@ class Solution { public: vector largestValues(TreeNode* root) { if (!root) return {}; - queue q{{root}}; + queue q {{root}}; vector ans; - while (!q.empty()) - { + while (!q.empty()) { int t = INT_MIN; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { auto node = q.front(); q.pop(); t = max(t, node->val); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/README.md" index c7578a14eea92..7d1483163c138 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/README.md" @@ -142,10 +142,8 @@ public: queue q; q.push(root); int ans = -1; - while (!q.empty()) - { - for (int i = 0, n = q.size(); i < n; ++i) - { + while (!q.empty()) { + for (int i = 0, n = q.size(); i < n; ++i) { TreeNode* node = q.front(); if (i == 0) ans = node->val; q.pop(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/Solution.cpp" index 01a38b15870fe..c3c3337babf9d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 045. \344\272\214\345\217\211\346\240\221\346\234\200\345\272\225\345\261\202\346\234\200\345\267\246\350\276\271\347\232\204\345\200\274/Solution.cpp" @@ -15,10 +15,8 @@ class Solution { queue q; q.push(root); int ans = -1; - while (!q.empty()) - { - for (int i = 0, n = q.size(); i < n; ++i) - { + while (!q.empty()) { + for (int i = 0, n = q.size(); i < n; ++i) { TreeNode* node = q.front(); if (i == 0) ans = node->val; q.pop(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" index 27a645cad2603..e3c7419c2ea7e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/README.md" @@ -147,8 +147,7 @@ public: if (!root) return ans; queue q; q.push(root); - while (!q.empty()) - { + while (!q.empty()) { ans.push_back(q.front()->val); for (int i = q.size(); i > 0; --i) { auto node = q.front(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" index 64b84b4cda537..379f125ffa85d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 046. \344\272\214\345\217\211\346\240\221\347\232\204\345\217\263\344\276\247\350\247\206\345\233\276/Solution.cpp" @@ -16,8 +16,7 @@ class Solution { if (!root) return ans; queue q; q.push(root); - while (!q.empty()) - { + while (!q.empty()) { ans.push_back(q.front()->val); for (int i = q.size(); i > 0; --i) { auto node = q.front(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" index 4a8ad699f498b..6be3e3cc77cca 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" @@ -203,7 +203,6 @@ public class Codec { */ class Codec { public: - // Encodes a tree to a single string. string serialize(TreeNode* root) { if (!root) return ""; @@ -213,9 +212,9 @@ public: } void preorder(TreeNode* root, string& s) { - if (!root) s += "# "; - else - { + if (!root) + s += "# "; + else { s += to_string(root->val) + " "; preorder(root->left, s); preorder(root->right, s); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.cpp" index 3eb98b9b42343..a55345bc92edf 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 048. \345\272\217\345\210\227\345\214\226\344\270\216\345\217\215\345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.cpp" @@ -9,7 +9,6 @@ */ class Codec { public: - // Encodes a tree to a single string. string serialize(TreeNode* root) { if (!root) return ""; @@ -19,9 +18,9 @@ class Codec { } void preorder(TreeNode* root, string& s) { - if (!root) s += "# "; - else - { + if (!root) + s += "# "; + else { s += to_string(root->val) + " "; preorder(root->left, s); preorder(root->right, s); @@ -49,4 +48,3 @@ class Codec { // Your Codec object will be instantiated and called as such: // Codec ser, deser; // TreeNode* ans = deser.deserialize(ser.serialize(root)); - diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" index f1cc8e378aaa1..585aa21f9e2f1 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" @@ -144,11 +144,11 @@ class Solution { */ class Solution { public: - int sumNumbers(TreeNode *root) { + int sumNumbers(TreeNode* root) { return dfs(root, 0); } - int dfs(TreeNode *root, int presum) { + int dfs(TreeNode* root, int presum) { if (!root) return 0; int s = presum * 10 + root->val; if (!root->left && !root->right) return s; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" index c4cbebdcf991f..b9d7c5423dc65 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" @@ -11,11 +11,11 @@ */ class Solution { public: - int sumNumbers(TreeNode *root) { + int sumNumbers(TreeNode* root) { return dfs(root, 0); } - int dfs(TreeNode *root, int presum) { + int dfs(TreeNode* root, int presum) { if (!root) return 0; int s = presum * 10 + root->val; if (!root->left && !root->right) return s; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/README.md" index 6c28d31eba7c0..537a647778b8b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/README.md" @@ -264,31 +264,23 @@ Morris 遍历: */ class Solution { public: - TreeNode *convertBST(TreeNode *root) { + TreeNode* convertBST(TreeNode* root) { int s = 0; - TreeNode *node = root; - while (root) - { - if (root->right == nullptr) - { + TreeNode* node = root; + while (root) { + if (root->right == nullptr) { s += root->val; root->val = s; root = root->left; - } - else - { - TreeNode *next = root->right; - while (next->left && next->left != root) - { + } else { + TreeNode* next = root->right; + while (next->left && next->left != root) { next = next->left; } - if (next->left == nullptr) - { + if (next->left == nullptr) { next->left = root; root = root->right; - } - else - { + } else { s += root->val; root->val = s; next->left = nullptr; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/Solution.cpp" index dfb0ec14551c0..4f15c632cf1ad 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 054. \346\211\200\346\234\211\345\244\247\344\272\216\347\255\211\344\272\216\350\212\202\347\202\271\347\232\204\345\200\274\344\271\213\345\222\214/Solution.cpp" @@ -11,31 +11,23 @@ */ class Solution { public: - TreeNode *convertBST(TreeNode *root) { + TreeNode* convertBST(TreeNode* root) { int s = 0; - TreeNode *node = root; - while (root) - { - if (root->right == nullptr) - { + TreeNode* node = root; + while (root) { + if (root->right == nullptr) { s += root->val; root->val = s; root = root->left; - } - else - { - TreeNode *next = root->right; - while (next->left && next->left != root) - { + } else { + TreeNode* next = root->right; + while (next->left && next->left != root) { next = next->left; } - if (next->left == nullptr) - { + if (next->left == nullptr) { next->left = root; root = root->right; - } - else - { + } else { s += root->val; root->val = s; next->left = nullptr; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.cpp" index 7cffe141cff36..1e9939f2773f1 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 055. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\350\277\255\344\273\243\345\231\250/Solution.cpp" @@ -17,7 +17,7 @@ class BSTIterator { stack.push(root); } } - + int next() { TreeNode* cur = stack.top(); stack.pop(); @@ -27,7 +27,7 @@ class BSTIterator { } return cur->val; } - + bool hasNext() { return !stack.empty(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 056. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 056. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/Solution.cpp" index 1eee463e49006..f5f3ea19eed5b 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 056. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 056. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\255\344\270\244\344\270\252\350\212\202\347\202\271\344\271\213\345\222\214/Solution.cpp" @@ -14,7 +14,7 @@ class Solution { unordered_set nodes; bool findTarget(TreeNode* root, int k) { - return find(root, k); + return find(root, k); } bool find(TreeNode* root, int k) { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" index 210117f8ead0f..8e0eac472629d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/README.md" @@ -102,12 +102,11 @@ class Solution { public: bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { set s; - for (int i = 0; i < nums.size(); ++i) - { - auto it = s.lower_bound((long) nums[i] - t); - if (it != s.end() && *it <= (long) nums[i] + t) return true; - s.insert((long) nums[i]); - if (i >= k) s.erase((long) nums[i - k]); + for (int i = 0; i < nums.size(); ++i) { + auto it = s.lower_bound((long)nums[i] - t); + if (it != s.end() && *it <= (long)nums[i] + t) return true; + s.insert((long)nums[i]); + if (i >= k) s.erase((long)nums[i - k]); } return false; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.cpp" index 7eb786c64d7b0..cf409f482a57d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 057. \345\200\274\345\222\214\344\270\213\346\240\207\344\271\213\345\267\256\351\203\275\345\234\250\347\273\231\345\256\232\347\232\204\350\214\203\345\233\264\345\206\205/Solution.cpp" @@ -2,12 +2,11 @@ class Solution { public: bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { set s; - for (int i = 0; i < nums.size(); ++i) - { - auto it = s.lower_bound((long) nums[i] - t); - if (it != s.end() && *it <= (long) nums[i] + t) return true; - s.insert((long) nums[i]); - if (i >= k) s.erase((long) nums[i - k]); + for (int i = 0; i < nums.size(); ++i) { + auto it = s.lower_bound((long)nums[i] - t); + if (it != s.end() && *it <= (long)nums[i] + t) return true; + s.insert((long)nums[i]); + if (i >= k) s.erase((long)nums[i - k]); } return false; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.cpp" index 72f168d144f19..b1e5e3e44425a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 059. \346\225\260\346\215\256\346\265\201\347\232\204\347\254\254 K \345\244\247\346\225\260\345\200\274/Solution.cpp" @@ -7,7 +7,7 @@ class KthLargest { size = k; for (int num : nums) add(num); } - + int add(int val) { q.push(val); if (q.size() > size) q.pop(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" index f5e541b0106e5..e70a5a748d7fa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/README.md" @@ -126,18 +126,15 @@ public: unordered_map counter; for (auto& e : nums) ++counter[e]; priority_queue, vector>, decltype(&cmp)> pq(cmp); - for (auto& [num, freq] : counter) - { - if (pq.size() == k) - { + for (auto& [num, freq] : counter) { + if (pq.size() == k) { pq.emplace(num, freq); pq.pop(); - } - else pq.emplace(num, freq); + } else + pq.emplace(num, freq); } vector ans; - while (!pq.empty()) - { + while (!pq.empty()) { ans.push_back(pq.top().first); pq.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.cpp" index 02e35a19c4be3..d5a73d9038cd6 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 060. \345\207\272\347\216\260\351\242\221\347\216\207\346\234\200\351\253\230\347\232\204 k \344\270\252\346\225\260\345\255\227/Solution.cpp" @@ -7,18 +7,15 @@ class Solution { unordered_map counter; for (auto& e : nums) ++counter[e]; priority_queue, vector>, decltype(&cmp)> pq(cmp); - for (auto& [num, freq] : counter) - { - if (pq.size() == k) - { + for (auto& [num, freq] : counter) { + if (pq.size() == k) { pq.emplace(num, freq); pq.pop(); - } - else pq.emplace(num, freq); + } else + pq.emplace(num, freq); } vector ans; - while (!pq.empty()) - { + while (!pq.empty()) { ans.push_back(pq.top().first); pq.pop(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/README.md" index 5cc8a0a52e105..ea2e8040455db 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/README.md" @@ -259,8 +259,7 @@ private: Trie* searchPrefix(string s) { Trie* node = this; - for (char c : s) - { + for (char c : s) { int idx = c - 'a'; if (!node->children[idx]) return nullptr; node = node->children[idx]; @@ -269,12 +268,13 @@ private: } public: - Trie() : children(26), isEnd(false) {} + Trie() + : children(26) + , isEnd(false) { } void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new Trie(); node = node->children[idx]; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/Solution.cpp" index e69db33d331b0..874449720f66e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 062. \345\256\236\347\216\260\345\211\215\347\274\200\346\240\221/Solution.cpp" @@ -5,8 +5,7 @@ class Trie { Trie* searchPrefix(string s) { Trie* node = this; - for (char c : s) - { + for (char c : s) { int idx = c - 'a'; if (!node->children[idx]) return nullptr; node = node->children[idx]; @@ -15,24 +14,25 @@ class Trie { } public: - Trie() : children(26), isEnd(false) {} - + Trie() + : children(26) + , isEnd(false) { } + void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new Trie(); node = node->children[idx]; } node->isEnd = true; } - + bool search(string word) { Trie* node = searchPrefix(word); return node != nullptr && node->isEnd; } - + bool startsWith(string prefix) { Trie* node = searchPrefix(prefix); return node != nullptr; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/README.md" index 9fe5a0cf51023..5e246cd6a8c3f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/README.md" @@ -238,12 +238,13 @@ class Trie { public: vector children; string v; - Trie() : children(26), v("") {} + Trie() + : children(26) + , v("") { } void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; @@ -253,8 +254,7 @@ public: string search(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) break; node = node->children[c]; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/Solution.cpp" index 189502194160b..b1447f015af42 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 063. \346\233\277\346\215\242\345\215\225\350\257\215/Solution.cpp" @@ -2,12 +2,13 @@ class Trie { public: vector children; string v; - Trie() : children(26), v("") {} + Trie() + : children(26) + , v("") { } void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; @@ -17,8 +18,7 @@ class Trie { string search(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) break; node = node->children[c]; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" index 582cd3fa34aff..d9c1f0a7b1dfc 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/README.md" @@ -162,20 +162,17 @@ class MagicDictionary { public: /** Initialize your data structure here. */ MagicDictionary() { - } void buildDict(vector dictionary) { - for (string word : dictionary) - { + for (string word : dictionary) { words.insert(word); for (string p : patterns(word)) ++counter[p]; } } bool search(string searchWord) { - for (string p : patterns(searchWord)) - { + for (string p : patterns(searchWord)) { if (counter[p] > 1 || (counter[p] == 1 && !words.count(searchWord))) return true; } return false; @@ -187,8 +184,7 @@ private: vector patterns(string word) { vector res; - for (int i = 0; i < word.size(); ++i) - { + for (int i = 0; i < word.size(); ++i) { char c = word[i]; word[i] = '*'; res.push_back(word); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.cpp" index 9ef7cb48e1ef3..32240ccbd4e4a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 064. \347\245\236\345\245\207\347\232\204\345\255\227\345\205\270/Solution.cpp" @@ -2,20 +2,17 @@ class MagicDictionary { public: /** Initialize your data structure here. */ MagicDictionary() { - } - + void buildDict(vector dictionary) { - for (string word : dictionary) - { + for (string word : dictionary) { words.insert(word); for (string p : patterns(word)) ++counter[p]; } } - + bool search(string searchWord) { - for (string p : patterns(searchWord)) - { + for (string p : patterns(searchWord)) { if (counter[p] > 1 || (counter[p] == 1 && !words.count(searchWord))) return true; } return false; @@ -27,8 +24,7 @@ class MagicDictionary { vector patterns(string word) { vector res; - for (int i = 0; i < word.size(); ++i) - { + for (int i = 0; i < word.size(); ++i) { char c = word[i]; word[i] = '*'; res.push_back(word); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/README.md" index a24facb8f8ae0..9a1a81140204a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/README.md" @@ -269,7 +269,7 @@ func minimumLengthEncoding(words []string) int { ```cpp struct Trie { - Trie* children[26] = { nullptr }; + Trie* children[26] = {nullptr}; }; class Solution { @@ -315,11 +315,9 @@ public: int insert(string w) { Trie* node = this; bool pref = true; - for (char c : w) - { + for (char c : w) { c -= 'a'; - if (!node->children[c]) - { + if (!node->children[c]) { pref = false; node->children[c] = new Trie(); } @@ -335,8 +333,7 @@ public: sort(words.begin(), words.end(), [](string &a, string &b) {return a.size() > b.size();}); Trie* trie = new Trie(); int ans = 0; - for (auto& w : words) - { + for (auto& w : words) { reverse(w.begin(), w.end()); ans += trie->insert(w); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/Solution.cpp" index 71c6721b29261..e1a647698d5a9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 065. \346\234\200\347\237\255\347\232\204\345\215\225\350\257\215\347\274\226\347\240\201/Solution.cpp" @@ -1,5 +1,5 @@ struct Trie { - Trie* children[26] = { nullptr }; + Trie* children[26] = {nullptr}; }; class Solution { diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" index 74f41179fb6d2..07ec9c265a534 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/README.md" @@ -131,14 +131,12 @@ public: /** Initialize your data structure here. */ MapSum() { - } void insert(string key, int val) { int old = t[key]; t[key] = val; - for (int i = 1; i < key.size() + 1; ++i) - { + for (int i = 1; i < key.size() + 1; ++i) { string k = key.substr(0, i); data[k] += (val - old); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.cpp" index 211a1f366580f..02a261264d990 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 066. \345\215\225\350\257\215\344\271\213\345\222\214/Solution.cpp" @@ -5,19 +5,17 @@ class MapSum { /** Initialize your data structure here. */ MapSum() { - } - + void insert(string key, int val) { int old = t[key]; t[key] = val; - for (int i = 1; i < key.size() + 1; ++i) - { + for (int i = 1; i < key.size() + 1; ++i) { string k = key.substr(0, i); data[k] += (val - old); } } - + int sum(string prefix) { return data[prefix]; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/README.md" index b45bcbd9308d8..6ae1e0fafeeba 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/README.md" @@ -227,12 +227,12 @@ class Trie { public: vector children; string v; - Trie() : children(2) {} + Trie() + : children(2) { } void insert(int x) { Trie* node = this; - for (int i = 30; ~i; --i) - { + for (int i = 30; ~i; --i) { int v = (x >> i) & 1; if (!node->children[v]) node->children[v] = new Trie(); node = node->children[v]; @@ -242,16 +242,12 @@ public: int search(int x) { Trie* node = this; int res = 0; - for (int i = 30; ~i; --i) - { + for (int i = 30; ~i; --i) { int v = (x >> i) & 1; - if (node->children[v ^ 1]) - { + if (node->children[v ^ 1]) { res = res << 1 | 1; node = node->children[v ^ 1]; - } - else - { + } else { res <<= 1; node = node->children[v]; } @@ -265,8 +261,7 @@ public: int findMaximumXOR(vector& nums) { Trie* trie = new Trie(); int ans = 0; - for (int v : nums) - { + for (int v : nums) { trie->insert(v); ans = max(ans, trie->search(v)); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/Solution.cpp" index a6f29f8656832..6795f17f7385d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 067. \346\234\200\345\244\247\347\232\204\345\274\202\346\210\226/Solution.cpp" @@ -2,12 +2,12 @@ class Trie { public: vector children; string v; - Trie() : children(2) {} + Trie() + : children(2) { } void insert(int x) { Trie* node = this; - for (int i = 30; ~i; --i) - { + for (int i = 30; ~i; --i) { int v = (x >> i) & 1; if (!node->children[v]) node->children[v] = new Trie(); node = node->children[v]; @@ -17,16 +17,12 @@ class Trie { int search(int x) { Trie* node = this; int res = 0; - for (int i = 30; ~i; --i) - { + for (int i = 30; ~i; --i) { int v = (x >> i) & 1; - if (node->children[v ^ 1]) - { + if (node->children[v ^ 1]) { res = res << 1 | 1; node = node->children[v ^ 1]; - } - else - { + } else { res <<= 1; node = node->children[v]; } @@ -40,8 +36,7 @@ class Solution { int findMaximumXOR(vector& nums) { Trie* trie = new Trie(); int ans = 0; - for (int v : nums) - { + for (int v : nums) { trie->insert(v); ans = max(ans, trie->search(v)); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/README.md" index 7177d657201ca..0bfd09ae0073f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/README.md" @@ -113,11 +113,12 @@ class Solution { public: int searchInsert(vector& nums, int target) { int left = 0, right = nums.size(); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] >= target) right = mid; - else left = mid + 1; + if (nums[mid] >= target) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/Solution.cpp" index 2d90abd508aa0..6849c3ca0a42f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 068. \346\237\245\346\211\276\346\217\222\345\205\245\344\275\215\347\275\256/Solution.cpp" @@ -2,11 +2,12 @@ class Solution { public: int searchInsert(vector& nums, int target) { int left = 0, right = nums.size(); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] >= target) right = mid; - else left = mid + 1; + if (nums[mid] >= target) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/README.md" index fc45d49e052f6..092cce57b22a3 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/README.md" @@ -126,11 +126,12 @@ class Solution { public: int peakIndexInMountainArray(vector& arr) { int left = 1, right = arr.size() - 2; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (arr[mid] > arr[mid + 1]) right = mid; - else left = mid + 1; + if (arr[mid] > arr[mid + 1]) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/Solution.cpp" index 262f84dfc9675..b48e04663f8df 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 069. \345\261\261\345\263\260\346\225\260\347\273\204\347\232\204\351\241\266\351\203\250/Solution.cpp" @@ -2,11 +2,12 @@ class Solution { public: int peakIndexInMountainArray(vector& arr) { int left = 1, right = arr.size() - 2; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (arr[mid] > arr[mid + 1]) right = mid; - else left = mid + 1; + if (arr[mid] > arr[mid + 1]) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" index 6d23b890fd2ee..baee341cd8549 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/README.md" @@ -116,11 +116,12 @@ class Solution { public: int singleNonDuplicate(vector& nums) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] != nums[mid ^ 1]) right = mid; - else left = mid + 1; + if (nums[mid] != nums[mid ^ 1]) + right = mid; + else + left = mid + 1; } return nums[left]; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" index f27306f6d7b4a..5668de91bd506 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 070. \346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\345\217\252\345\207\272\347\216\260\344\270\200\346\254\241\347\232\204\346\225\260\345\255\227/Solution.cpp" @@ -2,11 +2,12 @@ class Solution { public: int singleNonDuplicate(vector& nums) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] != nums[mid ^ 1]) right = mid; - else left = mid + 1; + if (nums[mid] != nums[mid ^ 1]) + right = mid; + else + left = mid + 1; } return nums[left]; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" index 00077fb253e72..1266fc8647cb3 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/README.md" @@ -161,11 +161,12 @@ public: int n = presum.size(); int x = rand() % presum[n - 1] + 1; int left = 0, right = n - 2; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (presum[mid + 1] >= x) right = mid; - else left = mid + 1; + if (presum[mid + 1] >= x) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.cpp" index b4518de5ccc77..40cb266c28a30 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 071. \346\214\211\346\235\203\351\207\215\347\224\237\346\210\220\351\232\217\346\234\272\346\225\260/Solution.cpp" @@ -7,16 +7,17 @@ class Solution { presum.resize(n + 1); for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + w[i]; } - + int pickIndex() { int n = presum.size(); int x = rand() % presum[n - 1] + 1; int left = 0, right = n - 2; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (presum[mid + 1] >= x) right = mid; - else left = mid + 1; + if (presum[mid + 1] >= x) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/README.md" index 0cd5b3287e169..660ee3195ad4f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/README.md" @@ -81,13 +81,14 @@ class Solution { public: int mySqrt(int x) { long long left = 0, right = x; - while (left < right) - { + while (left < right) { long long mid = left + ((right - left + 1) >> 1); - if (mid <= x / mid) left = mid; - else right = mid - 1; + if (mid <= x / mid) + left = mid; + else + right = mid - 1; } - return (int) left; + return (int)left; } }; ``` diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/Solution.cpp" index a95563389590a..106f2218862a4 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 072. \346\261\202\345\271\263\346\226\271\346\240\271/Solution.cpp" @@ -2,12 +2,13 @@ class Solution { public: int mySqrt(int x) { long long left = 0, right = x; - while (left < right) - { + while (left < right) { long long mid = left + ((right - left + 1) >> 1); - if (mid <= x / mid) left = mid; - else right = mid - 1; + if (mid <= x / mid) + left = mid; + else + right = mid - 1; } - return (int) left; + return (int)left; } }; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" index 435fb3ba41821..0abbd0f00f2ff 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/README.md" @@ -114,13 +114,14 @@ class Solution { public: int minEatingSpeed(vector& piles, int h) { int left = 1, right = *max_element(piles.begin(), piles.end()); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; int s = 0; for (int pile : piles) s += (pile + mid - 1) / mid; - if (s <= h) right = mid; - else left = mid + 1; + if (s <= h) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/Solution.cpp" index b5227b6037445..2399857378797 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 073. \347\213\222\347\213\222\345\220\203\351\246\231\350\225\211/Solution.cpp" @@ -2,13 +2,14 @@ class Solution { public: int minEatingSpeed(vector& piles, int h) { int left = 1, right = *max_element(piles.begin(), piles.end()); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; int s = 0; for (int pile : piles) s += (pile + mid - 1) / mid; - if (s <= h) right = mid; - else left = mid + 1; + if (s <= h) + right = mid; + else + left = mid + 1; } return left; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/README.md" index d9081bcd49106..b70d3bef817dd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/README.md" @@ -119,15 +119,13 @@ public: sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/Solution.cpp" index 2b669b600d3fd..6def29301f002 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 074. \345\220\210\345\271\266\345\214\272\351\227\264/Solution.cpp" @@ -4,15 +4,13 @@ class Solution { sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/README.md" index 15a73d81341a9..1236774e4c114 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/README.md" @@ -116,12 +116,10 @@ public: vector mp(1001); for (int x : arr1) ++mp[x]; int i = 0; - for (int x : arr2) - { + for (int x : arr2) { while (mp[x]-- > 0) arr1[i++] = x; } - for (int j = 0; j < mp.size(); ++j) - { + for (int j = 0; j < mp.size(); ++j) { while (mp[j]-- > 0) arr1[i++] = j; } return arr1; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/Solution.cpp" index a6e744fc7b430..6b3c5fdd5b2c8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 075. \346\225\260\347\273\204\347\233\270\345\257\271\346\216\222\345\272\217/Solution.cpp" @@ -4,12 +4,10 @@ class Solution { vector mp(1001); for (int x : arr1) ++mp[x]; int i = 0; - for (int x : arr2) - { + for (int x : arr2) { while (mp[x]-- > 0) arr1[i++] = x; } - for (int j = 0; j < mp.size(); ++j) - { + for (int j = 0; j < mp.size(); ++j) { while (mp[j]-- > 0) arr1[i++] = j; } return arr1; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/README.md" index a76bbdb7c4bbf..61ed03322e232 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/README.md" @@ -124,10 +124,11 @@ public: if (left == right) return nums[left]; int i = left - 1, j = right + 1; int x = nums[left + right >> 1]; - while (i < j) - { - while (nums[++i] < x); - while (nums[--j] > x); + while (i < j) { + while (nums[++i] < x) + ; + while (nums[--j] > x) + ; if (i < j) swap(nums[i], nums[j]); } return j < k ? quickSort(nums, j + 1, right, k) : quickSort(nums, left, j, k); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/Solution.cpp" index 5f514fead2fe4..9413e80e60e65 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 076. \346\225\260\347\273\204\344\270\255\347\232\204\347\254\254 k \345\244\247\347\232\204\346\225\260\345\255\227/Solution.cpp" @@ -9,10 +9,11 @@ class Solution { if (left == right) return nums[left]; int i = left - 1, j = right + 1; int x = nums[left + right >> 1]; - while (i < j) - { - while (nums[++i] < x); - while (nums[--j] > x); + while (i < j) { + while (nums[++i] < x) + ; + while (nums[--j] > x) + ; if (i < j) swap(nums[i], nums[j]); } return j < k ? quickSort(nums, j + 1, right, k) : quickSort(nums, left, j, k); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/README.md" index 8f8a507aab166..2a4ca936ac360 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/README.md" @@ -161,8 +161,7 @@ public: if (!head || !head->next) return head; auto* slow = head; auto* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -172,15 +171,11 @@ public: auto* l2 = sortList(t); auto* dummy = new ListNode(); auto* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/Solution.cpp" index b78b98175803c..f41cd39e5ddb7 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 077. \351\223\276\350\241\250\346\216\222\345\272\217/Solution.cpp" @@ -14,8 +14,7 @@ class Solution { if (!head || !head->next) return head; auto* slow = head; auto* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -25,15 +24,11 @@ class Solution { auto* l2 = sortList(t); auto* dummy = new ListNode(); auto* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/README.md" index 3fa17454f5afb..4951768d40e2e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/README.md" @@ -170,15 +170,11 @@ private: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/Solution.cpp" index 1c23143159e04..a6aad3f61836f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 078. \345\220\210\345\271\266\346\216\222\345\272\217\351\223\276\350\241\250/Solution.cpp" @@ -21,15 +21,11 @@ class Solution { ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" index dd01dcbcd7ec5..467ec9bfeb211 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/README.md" @@ -124,8 +124,7 @@ public: void dfs(int i, vector& nums, vector t, vector>& res) { res.push_back(t); if (i == nums.size()) return; - for (int j = i; j < nums.size(); ++j) - { + for (int j = i; j < nums.size(); ++j) { t.push_back(nums[j]); dfs(j + 1, nums, t, res); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.cpp" index 54346053afdb8..3afbd7231dd1a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 079. \346\211\200\346\234\211\345\255\220\351\233\206/Solution.cpp" @@ -10,8 +10,7 @@ class Solution { void dfs(int i, vector& nums, vector t, vector>& res) { res.push_back(t); if (i == nums.size()) return; - for (int j = i; j < nums.size(); ++j) - { + for (int j = i; j < nums.size(); ++j) { t.push_back(nums[j]); dfs(j + 1, nums, t, res); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" index 8e7287703fbaf..d42ad90f017fa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" @@ -110,13 +110,11 @@ public: } void dfs(int i, int n, int k, vector t, vector>& res) { - if (t.size() == k) - { + if (t.size() == k) { res.push_back(t); return; } - for (int j = i; j <= n; ++j) - { + for (int j = i; j <= n; ++j) { t.push_back(j); dfs(j + 1, n, k, t, res); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" index 234e38fa007c8..a01fbf6f7c01c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 080. \345\220\253\346\234\211 k \344\270\252\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" @@ -8,13 +8,11 @@ class Solution { } void dfs(int i, int n, int k, vector t, vector>& res) { - if (t.size() == k) - { + if (t.size() == k) { res.push_back(t); return; } - for (int j = i; j <= n; ++j) - { + for (int j = i; j <= n; ++j) { t.push_back(j); dfs(j + 1, n, k, t, res); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" index 650444226d738..b7d67654521f7 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/README.md" @@ -151,14 +151,12 @@ public: } void dfs(int s, int u, vector& t) { - if (s == target) - { + if (s == target) { ans.push_back(t); return; } if (s > target) return; - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { int c = candidates[i]; t.push_back(c); dfs(s + c, i, t); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" index 67384a822a2fc..3810f6ce654c2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 081. \345\205\201\350\256\270\351\207\215\345\244\215\351\200\211\346\213\251\345\205\203\347\264\240\347\232\204\347\273\204\345\220\210/Solution.cpp" @@ -13,14 +13,12 @@ class Solution { } void dfs(int s, int u, vector& t) { - if (s == target) - { + if (s == target) { ans.push_back(t); return; } if (s > target) return; - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { int c = candidates[i]; t.push_back(c); dfs(s + c, i, t); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" index 43cce93dd37f3..3dd5d167f51e8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/README.md" @@ -147,13 +147,11 @@ public: void dfs(int u, int s, vector& t) { if (s > target) return; - if (s == target) - { + if (s == target) { ans.push_back(t); return; } - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { if (i > u && candidates[i] == candidates[i - 1]) continue; t.push_back(candidates[i]); dfs(i + 1, s + candidates[i], t); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.cpp" index dfd70d3a3d34e..f41905337945e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 082. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\347\273\204\345\220\210/Solution.cpp" @@ -16,13 +16,11 @@ class Solution { void dfs(int u, int s, vector& t) { if (s > target) return; - if (s == target) - { + if (s == target) { ans.push_back(t); return; } - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { if (i > u && candidates[i] == candidates[i - 1]) continue; t.push_back(candidates[i]); dfs(i + 1, s + candidates[i], t); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" index 369301a4b96ee..9c7f3eaf815c8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" @@ -159,15 +159,12 @@ public: } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { - if (!used[i]) - { + for (int i = 0; i < n; ++i) { + if (!used[i]) { path[u] = nums[i]; used[i] = true; dfs(u + 1, n, nums, used, path, res); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" index db79849dfcc15..a269f66aec666 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 083. \346\262\241\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" @@ -10,15 +10,12 @@ class Solution { } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { - if (!used[i]) - { + for (int i = 0; i < n; ++i) { + if (!used[i]) { path[u] = nums[i]; used[i] = true; dfs(u + 1, n, nums, used, path, res); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" index 6f753d235321a..2a89a6b91ca94 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/README.md" @@ -126,13 +126,11 @@ public: } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) continue; path[u] = nums[i]; used[i] = true; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" index d39f639ee147c..8c0ea42c608fa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 084. \345\220\253\346\234\211\351\207\215\345\244\215\345\205\203\347\264\240\351\233\206\345\220\210\347\232\204\345\205\250\346\216\222\345\210\227/Solution.cpp" @@ -11,13 +11,11 @@ class Solution { } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) continue; path[u] = nums[i]; used[i] = true; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/README.md" index d136458288c35..52ecb82eabda9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/README.md" @@ -124,8 +124,7 @@ public: } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/Solution.cpp" index 2af50ad429a32..37d8d0850aaf4 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 085. \347\224\237\346\210\220\345\214\271\351\205\215\347\232\204\346\213\254\345\217\267/Solution.cpp" @@ -7,8 +7,7 @@ class Solution { } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index 406fbe3e56102..9a359bbce4f00 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -136,10 +136,8 @@ public: vector> partition(string s) { n = s.size(); dp.assign(n, vector(n, true)); - for (int i = n - 1; i >= 0; --i) - { - for (int j = i + 1; j < n; ++j) - { + for (int i = n - 1; i >= 0; --i) { + for (int j = i + 1; j < n; ++j) { dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1]; } } @@ -149,15 +147,12 @@ public: } void dfs(string& s, int i, vector t) { - if (i == n) - { + if (i == n) { ans.push_back(t); return; } - for (int j = i; j < n; ++j) - { - if (dp[i][j]) - { + for (int j = i; j < n; ++j) { + if (dp[i][j]) { t.push_back(s.substr(i, j - i + 1)); dfs(s, j + 1, t); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" index 9a0725108be44..de617ea30b03e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 086. \345\210\206\345\211\262\345\233\236\346\226\207\345\255\220\345\255\227\347\254\246\344\270\262/Solution.cpp" @@ -5,12 +5,10 @@ class Solution { int n; vector> partition(string s) { - n = s.size(); + n = s.size(); dp.assign(n, vector(n, true)); - for (int i = n - 1; i >= 0; --i) - { - for (int j = i + 1; j < n; ++j) - { + for (int i = n - 1; i >= 0; --i) { + for (int j = i + 1; j < n; ++j) { dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1]; } } @@ -20,15 +18,12 @@ class Solution { } void dfs(string& s, int i, vector t) { - if (i == n) - { + if (i == n) { ans.push_back(t); return; } - for (int j = i; j < n; ++j) - { - if (dp[i][j]) - { + for (int j = i; j < n; ++j) { + if (dp[i][j]) { t.push_back(s.substr(i, j - i + 1)); dfs(s, j + 1, t); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/README.md" index 83fc59b1df565..f478cf6fb678f 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/README.md" @@ -158,10 +158,8 @@ public: } void dfs(string s, vector& t, vector& ans) { - if (t.size() == 4) - { - if (s == "") - { + if (t.size() == 4) { + if (s == "") { string p = ""; for (auto e : t) p += e + "."; p.pop_back(); @@ -169,11 +167,9 @@ public: } return; } - for (int i = 1; i < min(4, (int) s.size() + 1); ++i) - { + for (int i = 1; i < min(4, (int)s.size() + 1); ++i) { string c = s.substr(0, i); - if (check(c)) - { + if (check(c)) { t.push_back(c); dfs(s.substr(i, s.size() - i), t, ans); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/Solution.cpp" index 3746aa1f91de7..3675f39f08d54 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 087. \345\244\215\345\216\237 IP/Solution.cpp" @@ -8,10 +8,8 @@ class Solution { } void dfs(string s, vector& t, vector& ans) { - if (t.size() == 4) - { - if (s == "") - { + if (t.size() == 4) { + if (s == "") { string p = ""; for (auto e : t) p += e + "."; p.pop_back(); @@ -19,11 +17,9 @@ class Solution { } return; } - for (int i = 1; i < min(4, (int) s.size() + 1); ++i) - { + for (int i = 1; i < min(4, (int)s.size() + 1); ++i) { string c = s.substr(0, i); - if (check(c)) - { + if (check(c)) { t.push_back(c); dfs(s.substr(i, s.size() - i), t, ans); t.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" index cd8a87d530545..07eff3ce015d5 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" @@ -133,22 +133,17 @@ public: int n = arr.size(); for (int i = 0; i < n; ++i) mp[arr[i]] = i; vector> dp(n, vector(n)); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) dp[j][i] = 2; } int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < i; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { int delta = arr[i] - arr[j]; - if (mp.count(delta)) - { + if (mp.count(delta)) { int k = mp[delta]; - if (k < j) - { + if (k < j) { dp[j][i] = dp[k][j] + 1; ans = max(ans, dp[j][i]); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.cpp" index f04c5d344fcd6..a46cbaafce1bf 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 093. \346\234\200\351\225\277\346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.cpp" @@ -5,22 +5,17 @@ class Solution { int n = arr.size(); for (int i = 0; i < n; ++i) mp[arr[i]] = i; vector> dp(n, vector(n)); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { for (int j = 0; j < i; ++j) dp[j][i] = 2; } int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < i; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { int delta = arr[i] - arr[j]; - if (mp.count(delta)) - { + if (mp.count(delta)) { int k = mp[delta]; - if (k < j) - { + if (k < j) { dp[j][i] = dp[k][j] + 1; ans = max(ans, dp[j][i]); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/README.md" index 625faf080cbfb..1397a8e6ce976 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/README.md" @@ -118,16 +118,11 @@ public: int longestCommonSubsequence(string text1, string text2) { int m = text1.size(), n = text2.size(); vector> dp(m + 1, vector(n + 1)); - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { - if (text1[i - 1] == text2[j - 1]) - { + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (text1[i - 1] == text2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; - } - else - { + } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.cpp" index 845804ae763a9..c8e00609373e8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 095. \346\234\200\351\225\277\345\205\254\345\205\261\345\255\220\345\272\217\345\210\227/Solution.cpp" @@ -3,16 +3,11 @@ class Solution { int longestCommonSubsequence(string text1, string text2) { int m = text1.size(), n = text2.size(); vector> dp(m + 1, vector(n + 1)); - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { - if (text1[i - 1] == text2[j - 1]) - { + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (text1[i - 1] == text2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; - } - else - { + } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" index 5e366d8a72a05..d9aa2671ebdd7 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/README.md" @@ -146,8 +146,7 @@ public: auto it = memo.find(i * 100 + j); if (it != memo.end()) return it->second; - bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || - (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); + bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); memo[i * 100 + j] = ret; return ret; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/Solution.cpp" index 1a0aed5cd4ddc..31f09f9279ac9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 096. \345\255\227\347\254\246\344\270\262\344\272\244\347\273\207/Solution.cpp" @@ -12,8 +12,7 @@ class Solution { auto it = memo.find(i * 100 + j); if (it != memo.end()) return it->second; - bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || - (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); + bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); memo[i * 100 + j] = ret; return ret; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" index 4c2883a4da29c..8d7d78844abdd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/README.md" @@ -124,10 +124,8 @@ class Solution { public: int uniquePaths(int m, int n) { vector> dp(m, vector(n, 1)); - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.cpp" index 04f6829f5d650..8aeef0fdfdcce 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 098. \350\267\257\345\276\204\347\232\204\346\225\260\347\233\256/Solution.cpp" @@ -2,10 +2,8 @@ class Solution { public: int uniquePaths(int m, int n) { vector> dp(m, vector(n, 1)); - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/README.md" index c8e2054797882..c2af63e61527c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/README.md" @@ -127,21 +127,17 @@ function minPathSum(grid: number[][]): number { ```cpp class Solution { public: - int minPathSum(vector> &grid) { + int minPathSum(vector>& grid) { int m = grid.size(), n = grid[0].size(); vector> dp(m, vector(n, grid[0][0])); - for (int i = 1; i < m; ++i) - { + for (int i = 1; i < m; ++i) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } - for (int j = 1; j < n; ++j) - { + for (int j = 1; j < n; ++j) { dp[0][j] = dp[0][j - 1] + grid[0][j]; } - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/Solution.cpp" index 87f905dcee1ec..a772d1d3ce168 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 099. \346\234\200\345\260\217\350\267\257\345\276\204\344\271\213\345\222\214/Solution.cpp" @@ -1,20 +1,16 @@ class Solution { public: - int minPathSum(vector> &grid) { + int minPathSum(vector>& grid) { int m = grid.size(), n = grid[0].size(); vector> dp(m, vector(n, grid[0][0])); - for (int i = 1; i < m; ++i) - { + for (int i = 1; i < m; ++i) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } - for (int j = 1; j < n; ++j) - { + for (int j = 1; j < n; ++j) { dp[0][j] = dp[0][j - 1] + grid[0][j]; } - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/README.md" index 165c94162773f..3219017caf5e5 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/README.md" @@ -160,10 +160,8 @@ public: vector dp(n); dp[0] = true; if (nums[0] < n) dp[nums[0]] = true; - for (int i = 1; i < m; ++i) - { - for (int j = n - 1; j >= nums[i]; --j) - { + for (int i = 1; i < m; ++i) { + for (int j = n - 1; j >= nums[i]; --j) { dp[j] = dp[j] || dp[j - nums[i]]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/Solution.cpp" index 8930526605345..b12daa08cd190 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 101. \345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\344\270\262/Solution.cpp" @@ -8,10 +8,8 @@ class Solution { vector dp(n); dp[0] = true; if (nums[0] < n) dp[nums[0]] = true; - for (int i = 1; i < m; ++i) - { - for (int j = n - 1; j >= nums[i]; --j) - { + for (int i = 1; i < m; ++i) { + for (int j = n - 1; j >= nums[i]; --j) { dp[j] = dp[j] || dp[j - nums[i]]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/README.md" index 584a4eb08b9f6..d179c8b73a4cf 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/README.md" @@ -206,10 +206,8 @@ public: target = (s - target) / 2 + 1; vector dp(target); dp[0] = 1; - for (int i = 1; i < nums.size() + 1; ++i) - { - for (int j = target - 1; j >= nums[i - 1]; --j) - { + for (int i = 1; i < nums.size() + 1; ++i) { + for (int j = target - 1; j >= nums[i - 1]; --j) { dp[j] += dp[j - nums[i - 1]]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/Solution.cpp" index 100d022b5ea87..bf2d0591a1b3e 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 102. \345\212\240\345\207\217\347\232\204\347\233\256\346\240\207\345\200\274/Solution.cpp" @@ -7,10 +7,8 @@ class Solution { target = (s - target) / 2 + 1; vector dp(target); dp[0] = 1; - for (int i = 1; i < nums.size() + 1; ++i) - { - for (int j = target - 1; j >= nums[i - 1]; --j) - { + for (int i = 1; i < nums.size() + 1; ++i) { + for (int j = target - 1; j >= nums[i - 1]; --j) { dp[j] += dp[j - nums[i - 1]]; } } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" index aae90f11072c1..ae25503ff05fd 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" @@ -360,8 +360,7 @@ public: grid[i][j] = 0; int ans = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k]; int y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.cpp" index 95eb739ff22e9..548aa09ce7849 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/Solution.cpp" @@ -18,8 +18,7 @@ class Solution { grid[i][j] = 0; int ans = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k]; int y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" index e2f149242208c..f76a4ada8d8c0 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/README.md" @@ -194,11 +194,9 @@ public: int n = graph.size(); p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; - for (int u = 0; u < n; ++u) - { + for (int u = 0; u < n; ++u) { auto& g = graph[u]; - for (int v : g) - { + for (int v : g) { if (find(u) == find(v)) return 0; p[find(v)] = find(g[0]); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.cpp" index 0f14b101b2769..73ab8a67cc3a2 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 106. \344\272\214\345\210\206\345\233\276/Solution.cpp" @@ -6,11 +6,9 @@ class Solution { int n = graph.size(); p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; - for (int u = 0; u < n; ++u) - { + for (int u = 0; u < n; ++u) { auto& g = graph[u]; - for (int v : g) - { + for (int v : g) { if (find(u) == find(v)) return 0; p[find(v)] = find(g[0]); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" index a5de0b79804d9..d07447c2ddd58 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/README.md" @@ -132,28 +132,22 @@ public: int m = mat.size(), n = mat[0].size(); vector> ans(m, vector(n, -1)); queue> q; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (mat[i][j] == 0) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (mat[i][j] == 0) { ans[i][j] = 0; q.emplace(i, j); } } } vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { int x = p.first + dirs[i]; int y = p.second + dirs[i + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) - { + if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) { ans[x][y] = ans[p.first][p.second] + 1; q.emplace(x, y); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.cpp" index 9ce4fb2904568..f36023e9bad31 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 107. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\235\347\246\273/Solution.cpp" @@ -4,28 +4,22 @@ class Solution { int m = mat.size(), n = mat[0].size(); vector> ans(m, vector(n, -1)); queue> q; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (mat[i][j] == 0) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (mat[i][j] == 0) { ans[i][j] = 0; q.emplace(i, j); } } } vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { int x = p.first + dirs[i]; int y = p.second + dirs[i + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) - { + if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) { ans[x][y] = ans[p.first][p.second] + 1; q.emplace(x, y); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/README.md" index d824374d5b575..42c7879df9e1c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/README.md" @@ -141,19 +141,15 @@ class Solution { public: int ladderLength(string beginWord, string endWord, vector& wordList) { unordered_set words(wordList.begin(), wordList.end()); - queue q{{beginWord}}; + queue q {{beginWord}}; int ans = 1; - while (!q.empty()) - { - for (int i = q.size(); i > 0; --i) - { + while (!q.empty()) { + for (int i = q.size(); i > 0; --i) { string s = q.front(); q.pop(); - for (int j = 0; j < s.size(); ++j) - { + for (int j = 0; j < s.size(); ++j) { char ch = s[j]; - for (char k = 'a'; k <= 'z'; ++k) - { + for (char k = 'a'; k <= 'z'; ++k) { s[j] = k; if (!words.count(s)) continue; if (s == endWord) return ans + 1; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/Solution.cpp" index 6442c10653892..55768d2ea9884 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 108. \345\215\225\350\257\215\346\274\224\345\217\230/Solution.cpp" @@ -2,19 +2,15 @@ class Solution { public: int ladderLength(string beginWord, string endWord, vector& wordList) { unordered_set words(wordList.begin(), wordList.end()); - queue q{{beginWord}}; + queue q {{beginWord}}; int ans = 1; - while (!q.empty()) - { - for (int i = q.size(); i > 0; --i) - { + while (!q.empty()) { + for (int i = q.size(); i > 0; --i) { string s = q.front(); q.pop(); - for (int j = 0; j < s.size(); ++j) - { + for (int j = 0; j < s.size(); ++j) { char ch = s[j]; - for (char k = 'a'; k <= 'z'; ++k) - { + for (char k = 'a'; k <= 'z'; ++k) { s[j] = k; if (!words.count(s)) continue; if (s == endWord) return ans + 1; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" index 0646022a88f8e..cf216d11bd394 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/README.md" @@ -190,15 +190,12 @@ public: queue q; q.push("0000"); int step = 0; - while (!q.empty()) - { + while (!q.empty()) { ++step; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { string status = q.front(); q.pop(); - for (auto t : get(status)) - { + for (auto t : get(status)) { if (visited.count(t) || s.count(t)) continue; if (t == target) return step; q.push(t); @@ -210,17 +207,16 @@ public: } char prev(char c) { - return c == '0' ? '9' : (char) (c - 1); + return c == '0' ? '9' : (char)(c - 1); } char next(char c) { - return c == '9' ? '0' : (char) (c + 1); + return c == '9' ? '0' : (char)(c + 1); } vector get(string& t) { vector res; - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { char c = t[i]; t[i] = prev(c); res.push_back(t); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.cpp" index a834464b001a5..b9ca045819688 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 109. \345\274\200\345\257\206\347\240\201\351\224\201/Solution.cpp" @@ -8,15 +8,12 @@ class Solution { queue q; q.push("0000"); int step = 0; - while (!q.empty()) - { + while (!q.empty()) { ++step; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { string status = q.front(); q.pop(); - for (auto t : get(status)) - { + for (auto t : get(status)) { if (visited.count(t) || s.count(t)) continue; if (t == target) return step; q.push(t); @@ -28,17 +25,16 @@ class Solution { } char prev(char c) { - return c == '0' ? '9' : (char) (c - 1); + return c == '0' ? '9' : (char)(c - 1); } char next(char c) { - return c == '9' ? '0' : (char) (c + 1); + return c == '9' ? '0' : (char)(c + 1); } vector get(string& t) { vector res; - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { char c = t[i]; t[i] = prev(c); res.push_back(t); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/README.md" index 77785f2ff9c88..c2eb3086eb312 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/README.md" @@ -145,13 +145,11 @@ public: } void dfs(int i, vector path) { - if (i == graph.size() - 1) - { + if (i == graph.size() - 1) { ans.push_back(path); return; } - for (int j : graph[i]) - { + for (int j : graph[i]) { path.push_back(j); dfs(j, path); path.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/Solution.cpp" index 59cca55f63498..f8c547dc931b9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 110. \346\211\200\346\234\211\350\267\257\345\276\204/Solution.cpp" @@ -12,13 +12,11 @@ class Solution { } void dfs(int i, vector path) { - if (i == graph.size() - 1) - { + if (i == graph.size() - 1) { ans.push_back(path); return; } - for (int j : graph[i]) - { + for (int j : graph[i]) { path.push_back(j); dfs(j, path); path.pop_back(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/README.md" index c1a4f3e4deb51..a8e96d5f07779 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/README.md" @@ -242,15 +242,13 @@ public: vector calcEquation(vector>& equations, vector& values, vector>& queries) { int n = equations.size(); - for (int i = 0; i < (n << 1); ++i) - { + for (int i = 0; i < (n << 1); ++i) { p.push_back(i); w.push_back(1.0); } unordered_map mp; int idx = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { auto e = equations[i]; string a = e[0], b = e[1]; if (mp.find(a) == mp.end()) mp[a] = idx++; @@ -262,12 +260,11 @@ public: } int m = queries.size(); vector res; - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { string c = queries[i][0], d = queries[i][1]; - if (mp.find(c) == mp.end() || mp.find(d) == mp.end()) res.push_back(-1.0); - else - { + if (mp.find(c) == mp.end() || mp.find(d) == mp.end()) + res.push_back(-1.0); + else { int pa = find(mp[c]), pb = find(mp[d]); res.push_back(pa == pb ? w[mp[c]] / w[mp[d]] : -1.0); } @@ -276,8 +273,7 @@ public: } int find(int x) { - if (p[x] != x) - { + if (p[x] != x) { int origin = p[x]; p[x] = find(p[x]); w[x] *= w[origin]; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/Solution.cpp" index d20d3c1cf842a..739cebf1caa7c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 111. \350\256\241\347\256\227\351\231\244\346\263\225/Solution.cpp" @@ -5,15 +5,13 @@ class Solution { vector calcEquation(vector>& equations, vector& values, vector>& queries) { int n = equations.size(); - for (int i = 0; i < (n << 1); ++i) - { + for (int i = 0; i < (n << 1); ++i) { p.push_back(i); w.push_back(1.0); } unordered_map mp; int idx = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { auto e = equations[i]; string a = e[0], b = e[1]; if (mp.find(a) == mp.end()) mp[a] = idx++; @@ -25,12 +23,11 @@ class Solution { } int m = queries.size(); vector res; - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { string c = queries[i][0], d = queries[i][1]; - if (mp.find(c) == mp.end() || mp.find(d) == mp.end()) res.push_back(-1.0); - else - { + if (mp.find(c) == mp.end() || mp.find(d) == mp.end()) + res.push_back(-1.0); + else { int pa = find(mp[c]), pb = find(mp[d]); res.push_back(pa == pb ? w[mp[c]] / w[mp[d]] : -1.0); } @@ -39,8 +36,7 @@ class Solution { } int find(int x) { - if (p[x] != x) - { + if (p[x] != x) { int origin = p[x]; p[x] = find(p[x]); w[x] *= w[origin]; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" index 13f8b3972e971..487aa2314fbe5 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/README.md" @@ -151,8 +151,7 @@ public: if (memo[i][j] != -1) return memo[i][j]; int ans = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]) ans = max(ans, dfs(x, y) + 1); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.cpp" index d373c482e751d..66a354cf6501a 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 112. \346\234\200\351\225\277\351\200\222\345\242\236\350\267\257\345\276\204/Solution.cpp" @@ -21,8 +21,7 @@ class Solution { if (memo[i][j] != -1) return memo[i][j]; int ans = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]) ans = max(ans, dfs(x, y) + 1); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" index f2aaf4762f80d..6628639b77f9d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/README.md" @@ -163,21 +163,21 @@ public: vector findOrder(int numCourses, vector>& prerequisites) { vector> g(numCourses); vector indeg(numCourses); - for (auto& p : prerequisites) - { + for (auto& p : prerequisites) { int a = p[0], b = p[1]; g[b].push_back(a); ++indeg[a]; } queue q; - for (int i = 0; i < numCourses; ++i) if (indeg[i] == 0) q.push(i); + for (int i = 0; i < numCourses; ++i) + if (indeg[i] == 0) q.push(i); vector ans; - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); ans.push_back(i); - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return ans.size() == numCourses ? ans : vector(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.cpp" index e56514aec406e..ab3af7299602d 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 113. \350\257\276\347\250\213\351\241\272\345\272\217/Solution.cpp" @@ -3,21 +3,21 @@ class Solution { vector findOrder(int numCourses, vector>& prerequisites) { vector> g(numCourses); vector indeg(numCourses); - for (auto& p : prerequisites) - { + for (auto& p : prerequisites) { int a = p[0], b = p[1]; g[b].push_back(a); ++indeg[a]; } queue q; - for (int i = 0; i < numCourses; ++i) if (indeg[i] == 0) q.push(i); + for (int i = 0; i < numCourses; ++i) + if (indeg[i] == 0) q.push(i); vector ans; - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); ans.push_back(i); - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return ans.size() == numCourses ? ans : vector(); } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" index 037e454584948..b4a41b78235c8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/README.md" @@ -232,21 +232,17 @@ public: vector s(26); int cnt = 0; int n = words.size(); - for (int i = 0; i < n - 1; ++i) - { - for (char c : words[i]) - { + for (int i = 0; i < n - 1; ++i) { + for (char c : words[i]) { if (cnt == 26) break; c -= 'a'; - if (!s[c]) - { + if (!s[c]) { ++cnt; s[c] = true; } } int m = words[i].size(); - for (int j = 0; j < m; ++j) - { + for (int j = 0; j < m; ++j) { if (j >= words[i + 1].size()) return ""; char c1 = words[i][j], c2 = words[i + 1][j]; if (c1 == c2) continue; @@ -255,12 +251,10 @@ public: break; } } - for (char c : words[n - 1]) - { + for (char c : words[n - 1]) { if (cnt == 26) break; c -= 'a'; - if (!s[c]) - { + if (!s[c]) { ++cnt; s[c] = true; } @@ -275,8 +269,7 @@ public: if (s[i] && indegree[i] == 0) q.push(i); string ans = ""; - while (!q.empty()) - { + while (!q.empty()) { int t = q.front(); ans += (t + 'a'); q.pop(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.cpp" index 16fd0e7386603..b0089714f20e8 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 114. \345\244\226\346\230\237\346\226\207\345\255\227\345\205\270/Solution.cpp" @@ -5,21 +5,17 @@ class Solution { vector s(26); int cnt = 0; int n = words.size(); - for (int i = 0; i < n - 1; ++i) - { - for (char c : words[i]) - { + for (int i = 0; i < n - 1; ++i) { + for (char c : words[i]) { if (cnt == 26) break; c -= 'a'; - if (!s[c]) - { + if (!s[c]) { ++cnt; s[c] = true; } } int m = words[i].size(); - for (int j = 0; j < m; ++j) - { + for (int j = 0; j < m; ++j) { if (j >= words[i + 1].size()) return ""; char c1 = words[i][j], c2 = words[i + 1][j]; if (c1 == c2) continue; @@ -28,12 +24,10 @@ class Solution { break; } } - for (char c : words[n - 1]) - { + for (char c : words[n - 1]) { if (cnt == 26) break; c -= 'a'; - if (!s[c]) - { + if (!s[c]) { ++cnt; s[c] = true; } @@ -48,8 +42,7 @@ class Solution { if (s[i] && indegree[i] == 0) q.push(i); string ans = ""; - while (!q.empty()) - { + while (!q.empty()) { int t = q.front(); ans += (t + 'a'); q.pop(); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" index 06fe6fbb13a5b..6686d42cd372c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/README.md" @@ -155,23 +155,22 @@ public: int n = nums.size(); vector> g(n); vector indeg(n); - for (auto& seq : sequences) - { - for (int i = 1; i < seq.size(); ++i) - { + for (auto& seq : sequences) { + for (int i = 1; i < seq.size(); ++i) { int a = seq[i - 1] - 1, b = seq[i] - 1; g[a].push_back(b); ++indeg[b]; } } queue q; - for (int i = 0; i < n; ++i) if (indeg[i] == 0) q.push(i); - while (!q.empty()) - { + for (int i = 0; i < n; ++i) + if (indeg[i] == 0) q.push(i); + while (!q.empty()) { if (q.size() > 1) return false; int i = q.front(); q.pop(); - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return true; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.cpp" index 82c93d32225f7..93f7e64eb5f51 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 115. \351\207\215\345\273\272\345\272\217\345\210\227/Solution.cpp" @@ -4,23 +4,22 @@ class Solution { int n = nums.size(); vector> g(n); vector indeg(n); - for (auto& seq : sequences) - { - for (int i = 1; i < seq.size(); ++i) - { + for (auto& seq : sequences) { + for (int i = 1; i < seq.size(); ++i) { int a = seq[i - 1] - 1, b = seq[i] - 1; g[a].push_back(b); ++indeg[b]; } } queue q; - for (int i = 0; i < n; ++i) if (indeg[i] == 0) q.push(i); - while (!q.empty()) - { + for (int i = 0; i < n; ++i) + if (indeg[i] == 0) q.push(i); + while (!q.empty()) { if (q.size() > 1) return false; int i = q.front(); q.pop(); - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return true; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" index 764e762a78b4c..d05f78b4c10f9 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" @@ -250,10 +250,8 @@ public: vis.resize(n); this->isConnected = isConnected; int ans = 0; - for (int i = 0; i < n; ++i) - { - if (!vis[i]) - { + for (int i = 0; i < n; ++i) { + if (!vis[i]) { dfs(i); ++ans; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/Solution.cpp" index 5e92b241a1dac..940a5267c4489 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/Solution.cpp" @@ -9,10 +9,8 @@ class Solution { vis.resize(n); this->isConnected = isConnected; int ans = 0; - for (int i = 0; i < n; ++i) - { - if (!vis[i]) - { + for (int i = 0; i < n; ++i) { + if (!vis[i]) { dfs(i); ++ans; } diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" index b7c38a0cb2b02..3a8c93506f6ab 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" @@ -177,8 +177,7 @@ public: vector findRedundantConnection(vector>& edges) { p.resize(1010); for (int i = 0; i < p.size(); ++i) p[i] = i; - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; if (find(a) == find(b)) return e; p[find(a)] = find(b); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/Solution.cpp" index 569576fb431e5..d16769f3fec15 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/Solution.cpp" @@ -5,8 +5,7 @@ class Solution { vector findRedundantConnection(vector>& edges) { p.resize(1010); for (int i = 0; i < p.size(); ++i) p[i] = i; - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; if (find(a) == find(b)) return e; p[find(a)] = find(b); diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" index 5b9ca56e6f09b..49d68856226aa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/README.md" @@ -189,15 +189,13 @@ public: ```cpp class Solution { public: - int longestConsecutive(vector &nums) { + int longestConsecutive(vector& nums) { unordered_set s; for (int num : nums) s.insert(num); int res = 0; - for (int num : nums) - { - if (!s.count(num - 1)) - { + for (int num : nums) { + if (!s.count(num - 1)) { int t = 1, next = num + 1; while (s.count(next++)) ++t; diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/Solution.cpp" index b541a92bef7d0..b14dd5e8a85e3 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/Solution.cpp" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 119. \346\234\200\351\225\277\350\277\236\347\273\255\345\272\217\345\210\227/Solution.cpp" @@ -1,14 +1,12 @@ class Solution { public: - int longestConsecutive(vector &nums) { + int longestConsecutive(vector& nums) { unordered_set s; for (int num : nums) s.insert(num); int res = 0; - for (int num : nums) - { - if (!s.count(num - 1)) - { + for (int num : nums) { + if (!s.count(num - 1)) { int t = 1, next = num + 1; while (s.count(next++)) ++t; diff --git "a/lcp/LCP 05. \345\217\221 LeetCoin/README.md" "b/lcp/LCP 05. \345\217\221 LeetCoin/README.md" index 6634222e53736..3b29eb43930a5 100644 --- "a/lcp/LCP 05. \345\217\221 LeetCoin/README.md" +++ "b/lcp/LCP 05. \345\217\221 LeetCoin/README.md" @@ -361,10 +361,9 @@ public: modify(l, r, v, root); } - void modify(int l, int r,int v, Node* node) { + void modify(int l, int r, int v, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v = (node->v + (node->r - node->l + 1) * v) % MOD; node->add += v; return; @@ -381,7 +380,7 @@ public: int query(int l, int r, Node* node) { if (l > r) return 0; - if (node->l >= l && node-> r <= r) return node->v; + if (node->l >= l && node->r <= r) return node->v; pushdown(node); int v = 0; if (l <= node->mid) v += query(l, r, node->left); @@ -396,8 +395,7 @@ public: void pushdown(Node* node) { if (!node->left) node->left = new Node(node->l, node->mid); if (!node->right) node->right = new Node(node->mid + 1, node->r); - if (node->add) - { + if (node->add) { Node* left = node->left; Node* right = node->right; left->v = (left->v + (left->r - left->l + 1) * node->add) % MOD; @@ -415,8 +413,7 @@ public: vector bonus(int n, vector>& leadership, vector>& operations) { vector> g(n + 1); - for (auto& l : leadership) - { + for (auto& l : leadership) { int a = l[0], b = l[1]; g[a].push_back(b); } @@ -426,12 +423,14 @@ public: dfs(1, begin, end, g); vector ans; SegmentTree* tree = new SegmentTree(n); - for (auto& op : operations) - { + for (auto& op : operations) { int p = op[0], v = op[1]; - if (p == 1) tree->modify(end[v], end[v], op[2]); - else if (p == 2) tree->modify(begin[v], end[v], op[2]); - else ans.push_back(tree->query(begin[v], end[v])); + if (p == 1) + tree->modify(end[v], end[v], op[2]); + else if (p == 2) + tree->modify(begin[v], end[v], op[2]); + else + ans.push_back(tree->query(begin[v], end[v])); } return ans; } diff --git "a/lcp/LCP 05. \345\217\221 LeetCoin/Solution.cpp" "b/lcp/LCP 05. \345\217\221 LeetCoin/Solution.cpp" index ef1c6708f1c2e..6615497722ca5 100644 --- "a/lcp/LCP 05. \345\217\221 LeetCoin/Solution.cpp" +++ "b/lcp/LCP 05. \345\217\221 LeetCoin/Solution.cpp" @@ -32,10 +32,9 @@ class SegmentTree { modify(l, r, v, root); } - void modify(int l, int r,int v, Node* node) { + void modify(int l, int r, int v, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v = (node->v + (node->r - node->l + 1) * v) % MOD; node->add += v; return; @@ -47,17 +46,17 @@ class SegmentTree { } int query(int l, int r) { - return query(l, r, root); + return query(l, r, root); } int query(int l, int r, Node* node) { if (l > r) return 0; - if (node->l >= l && node-> r <= r) return node->v; + if (node->l >= l && node->r <= r) return node->v; pushdown(node); int v = 0; if (l <= node->mid) v += query(l, r, node->left); if (r > node->mid) v += query(l, r, node->right); - return v % MOD; + return v % MOD; } void pushup(Node* node) { @@ -67,8 +66,7 @@ class SegmentTree { void pushdown(Node* node) { if (!node->left) node->left = new Node(node->l, node->mid); if (!node->right) node->right = new Node(node->mid + 1, node->r); - if (node->add) - { + if (node->add) { Node* left = node->left; Node* right = node->right; left->v = (left->v + (left->r - left->l + 1) * node->add) % MOD; @@ -86,8 +84,7 @@ class Solution { vector bonus(int n, vector>& leadership, vector>& operations) { vector> g(n + 1); - for (auto& l : leadership) - { + for (auto& l : leadership) { int a = l[0], b = l[1]; g[a].push_back(b); } @@ -97,12 +94,14 @@ class Solution { dfs(1, begin, end, g); vector ans; SegmentTree* tree = new SegmentTree(n); - for (auto& op : operations) - { + for (auto& op : operations) { int p = op[0], v = op[1]; - if (p == 1) tree->modify(end[v], end[v], op[2]); - else if (p == 2) tree->modify(begin[v], end[v], op[2]); - else ans.push_back(tree->query(begin[v], end[v])); + if (p == 1) + tree->modify(end[v], end[v], op[2]); + else if (p == 2) + tree->modify(begin[v], end[v], op[2]); + else + ans.push_back(tree->query(begin[v], end[v])); } return ans; } diff --git "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" index da8745e3e0970..707a0200e85d8 100644 --- "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" +++ "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/README.md" @@ -93,7 +93,7 @@ func minCount(coins []int) int { ```c int minCount(int* coins, int coinsSize) { int res = 0; - for (int i = 0 ; i < coinsSize; i++) { + for (int i = 0; i < coinsSize; i++) { int coin = coins[i]; if (coin % 2 == 1) { res++; diff --git "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.c" "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.c" index 484edaf7dcf09..f6298086d63be 100644 --- "a/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.c" +++ "b/lcp/LCP 06. \346\213\277\347\241\254\345\270\201/Solution.c" @@ -1,6 +1,6 @@ int minCount(int* coins, int coinsSize) { int res = 0; - for (int i = 0 ; i < coinsSize; i++) { + for (int i = 0; i < coinsSize; i++) { int coin = coins[i]; if (coin % 2 == 1) { res++; diff --git "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" index 13ec62bcb4677..88eb87e3dfee6 100644 --- "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" +++ "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/README.md" @@ -108,26 +108,21 @@ public: int minJump(vector& jump) { int n = jump.size(); vector vis(n + 1); - queue q{{0}}; + queue q {{0}}; vis[0] = true; int ans = 0, mx = 1; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { int i = q.front(); int j = i + jump[i]; if (j >= n) return ans + 1; q.pop(); - if (!vis[j]) - { + if (!vis[j]) { vis[j] = true; q.push(j); } - for (j = mx; j < i; ++j) - { - if (!vis[j]) - { + for (j = mx; j < i; ++j) { + if (!vis[j]) { vis[j] = true; q.push(j); } diff --git "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.cpp" "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.cpp" index 296e2bf62b0e9..be373797d4340 100644 --- "a/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.cpp" +++ "b/lcp/LCP 09. \346\234\200\345\260\217\350\267\263\350\267\203\346\254\241\346\225\260/Solution.cpp" @@ -3,26 +3,21 @@ class Solution { int minJump(vector& jump) { int n = jump.size(); vector vis(n + 1); - queue q{{0}}; + queue q {{0}}; vis[0] = true; int ans = 0, mx = 1; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { int i = q.front(); int j = i + jump[i]; if (j >= n) return ans + 1; q.pop(); - if (!vis[j]) - { + if (!vis[j]) { vis[j] = true; q.push(j); } - for (j = mx; j < i; ++j) - { - if (!vis[j]) - { + for (j = mx; j < i; ++j) { + if (!vis[j]) { vis[j] = true; q.push(j); } diff --git "a/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/README.md" "b/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/README.md" index 00f4baa4d756c..9a0a4885272a2 100644 --- "a/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/README.md" +++ "b/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/README.md" @@ -84,10 +84,11 @@ class Solution { public: int calculate(string s) { int x = 1, y = 0; - for (char& c : s) - { - if (c == 'A') x = x * 2 + y; - else y = y * 2 + x; + for (char& c : s) { + if (c == 'A') + x = x * 2 + y; + else + y = y * 2 + x; } return x + y; } diff --git "a/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/Solution.cpp" "b/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/Solution.cpp" index d9f725b2b7e21..faeaf5209cbbe 100644 --- "a/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/Solution.cpp" +++ "b/lcp/LCP 17. \351\200\237\347\256\227\346\234\272\345\231\250\344\272\272/Solution.cpp" @@ -2,10 +2,11 @@ class Solution { public: int calculate(string s) { int x = 1, y = 0; - for (char& c : s) - { - if (c == 'A') x = x * 2 + y; - else y = y * 2 + x; + for (char& c : s) { + if (c == 'A') + x = x * 2 + y; + else + y = y * 2 + x; } return x + y; } diff --git "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" index 823edd0e05d53..7b68f5b25f36c 100644 --- "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" +++ "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" @@ -138,17 +138,16 @@ public: int breakfastNumber(vector& staple, vector& drinks, int x) { int res = 0, n = drinks.size(); sort(drinks.begin(), drinks.end()); - for (int s : staple) - { + for (int s : staple) { int remain = x - s; - if (remain >= drinks[0]) - { + if (remain >= drinks[0]) { int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = left + right + 1 >> 1; - if (drinks[mid] <= remain) left = mid; - else right = mid - 1; + if (drinks[mid] <= remain) + left = mid; + else + right = mid - 1; } res = (res + left + 1) % 1000000007; } diff --git "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.cpp" "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.cpp" index e494e2c83123a..0989b1f83b5da 100644 --- "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.cpp" +++ "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.cpp" @@ -3,17 +3,16 @@ class Solution { int breakfastNumber(vector& staple, vector& drinks, int x) { int res = 0, n = drinks.size(); sort(drinks.begin(), drinks.end()); - for (int s : staple) - { + for (int s : staple) { int remain = x - s; - if (remain >= drinks[0]) - { + if (remain >= drinks[0]) { int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = left + right + 1 >> 1; - if (drinks[mid] <= remain) left = mid; - else right = mid - 1; + if (drinks[mid] <= remain) + left = mid; + else + right = mid - 1; } res = (res + left + 1) % 1000000007; } diff --git "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" index 044e45f647769..096cd710c3982 100644 --- "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" +++ "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" @@ -89,12 +89,11 @@ public: const int MOD = 1000000007; sort(nums.begin(), nums.end()); int res = 0; - for (int i = 0, j = nums.size() - 1; i < j; ++i) - { + for (int i = 0, j = nums.size() - 1; i < j; ++i) { while (i < j && nums[i] + nums[j] > target) --j; - if (i < j) res = (res + j - i) % MOD; + if (i < j) res = (res + j - i) % MOD; } - return res; + return res % MOD; } }; ``` diff --git "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.cpp" "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.cpp" index 83ca19297dab9..0c1d1b430e1ff 100644 --- "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.cpp" +++ "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.cpp" @@ -4,10 +4,9 @@ class Solution { const int MOD = 1000000007; sort(nums.begin(), nums.end()); int res = 0; - for (int i = 0, j = nums.size() - 1; i < j; ++i) - { + for (int i = 0, j = nums.size() - 1; i < j; ++i) { while (i < j && nums[i] + nums[j] > target) --j; - if (i < j) res = (res + j - i) % MOD; + if (i < j) res = (res + j - i) % MOD; } return res % MOD; } diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" index ed3bc7a51b4b8..bebae8635c6de 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/README.md" @@ -114,17 +114,19 @@ public: if (ans % 2 == 0) return ans; int inf = 0x3f3f3f3f; int a = inf, b = inf, c = -inf, d = -inf; - for (int i = 0; i < cnt; ++i) - { + for (int i = 0; i < cnt; ++i) { int v = cards[i]; - if (v % 2 == 1) a = min(a, v); - else b = min(b, v); + if (v % 2 == 1) + a = min(a, v); + else + b = min(b, v); } - for (int i = cnt; i < n; ++i) - { + for (int i = cnt; i < n; ++i) { int v = cards[i]; - if (v % 2 == 0) c = max(c, v); - else d = max(d, v); + if (v % 2 == 0) + c = max(c, v); + else + d = max(d, v); } return max(0, max(ans - a + c, ans - b + d)); } diff --git "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" index 800a25c9005fa..385484dd7f083 100644 --- "a/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" +++ "b/lcp/LCP 40. \345\277\203\347\256\227\346\214\221\346\210\230/Solution.cpp" @@ -8,17 +8,19 @@ class Solution { if (ans % 2 == 0) return ans; int inf = 0x3f3f3f3f; int a = inf, b = inf, c = -inf, d = -inf; - for (int i = 0; i < cnt; ++i) - { + for (int i = 0; i < cnt; ++i) { int v = cards[i]; - if (v % 2 == 1) a = min(a, v); - else b = min(b, v); + if (v % 2 == 1) + a = min(a, v); + else + b = min(b, v); } - for (int i = cnt; i < n; ++i) - { + for (int i = cnt; i < n; ++i) { int v = cards[i]; - if (v % 2 == 0) c = max(c, v); - else d = max(d, v); + if (v % 2 == 0) + c = max(c, v); + else + d = max(d, v); } return max(0, max(ans - a + c, ans - b + d)); } diff --git "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" index 55523574b535f..b1cdbd4789333 100644 --- "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" +++ "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/README.md" @@ -94,8 +94,7 @@ public: int n = questions.size() >> 1; sort(counter.begin(), counter.end()); int ans = 0; - for (int i = counter.size() - 1; n > 0; --i) - { + for (int i = counter.size() - 1; n > 0; --i) { ++ans; n -= counter[i]; } diff --git "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.cpp" "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.cpp" index fe64dcd10afe6..d911ab4adbf55 100644 --- "a/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.cpp" +++ "b/lcs/LCS 02. \345\256\214\346\210\220\344\270\200\345\215\212\351\242\230\347\233\256/Solution.cpp" @@ -6,8 +6,7 @@ class Solution { int n = questions.size() >> 1; sort(counter.begin(), counter.end()); int ans = 0; - for (int i = counter.size() - 1; n > 0; --i) - { + for (int i = counter.size() - 1; n > 0; --i) { ++ans; n -= counter[i]; } diff --git "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" index 6fc1f19642e9d..aa470b5fe3605 100644 --- "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" +++ "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/README.md" @@ -203,18 +203,14 @@ public: for (int i = 0; i < p.size(); ++i) p[i] = i; vector size(m * n + 1, 1); vector dirs = {-1, 0, 1, 0, -1}; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0') p[find(i * n + j)] = find(m * n); - else - { - for (int k = 0; k < 4; ++k) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0') + p[find(i * n + j)] = find(m * n); + else { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; - if ((grid[x][y] == '0' || grid[i][j] == grid[x][y]) && find(x * n + y) != find(i * n + j)) - { + if ((grid[x][y] == '0' || grid[i][j] == grid[x][y]) && find(x * n + y) != find(i * n + j)) { size[find(x * n + y)] += size[find(i * n + j)]; p[find(i * n + j)] = find(x * n + y); } diff --git "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.cpp" "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.cpp" index b8f6de9006aad..9587c5d99ea02 100644 --- "a/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.cpp" +++ "b/lcs/LCS 03. \344\270\273\351\242\230\347\251\272\351\227\264/Solution.cpp" @@ -8,18 +8,14 @@ class Solution { for (int i = 0; i < p.size(); ++i) p[i] = i; vector size(m * n + 1, 1); vector dirs = {-1, 0, 1, 0, -1}; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0') p[find(i * n + j)] = find(m * n); - else - { - for (int k = 0; k < 4; ++k) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (i == 0 || i == m - 1 || j == 0 || j == n - 1 || grid[i][j] == '0') + p[find(i * n + j)] = find(m * n); + else { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; - if ((grid[x][y] == '0' || grid[i][j] == grid[x][y]) && find(x * n + y) != find(i * n + j)) - { + if ((grid[x][y] == '0' || grid[i][j] == grid[x][y]) && find(x * n + y) != find(i * n + j)) { size[find(x * n + y)] += size[find(i * n + j)]; p[find(i * n + j)] = find(x * n + y); } diff --git a/run_format.py b/run_format.py new file mode 100644 index 0000000000000..51545eda20a68 --- /dev/null +++ b/run_format.py @@ -0,0 +1,10 @@ +import os.path + +path = os.getcwd() +for root, dirs, files in os.walk(path): + for name in files: + if name.endswith('.cpp') or name.endswith('.c'): + local_path = root + '/' + name + command = f'clang-format -i --style=file "{local_path}"' + print(command) + os.system(command) diff --git a/solution/0000-0099/0006.Zigzag Conversion/README.md b/solution/0000-0099/0006.Zigzag Conversion/README.md index 02df95405dc44..a1a165a36e0ba 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README.md @@ -127,12 +127,10 @@ public: if (numRows == 1) return s; string ans; int group = 2 * numRows - 2; - for (int i = 1; i <= numRows; ++i) - { + for (int i = 1; i <= numRows; ++i) { int interval = i == numRows ? group : 2 * numRows - 2 * i; int idx = i - 1; - while (idx < s.length()) - { + while (idx < s.length()) { ans.push_back(s[idx]); idx += interval; interval = group - interval; diff --git a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md index 4dd0cc93051bc..9b5fbbe48792e 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/README_EN.md +++ b/solution/0000-0099/0006.Zigzag Conversion/README_EN.md @@ -121,12 +121,10 @@ public: if (numRows == 1) return s; string ans; int group = 2 * numRows - 2; - for (int i = 1; i <= numRows; ++i) - { + for (int i = 1; i <= numRows; ++i) { int interval = i == numRows ? group : 2 * numRows - 2 * i; int idx = i - 1; - while (idx < s.length()) - { + while (idx < s.length()) { ans.push_back(s[idx]); idx += interval; interval = group - interval; diff --git a/solution/0000-0099/0006.Zigzag Conversion/Solution.cpp b/solution/0000-0099/0006.Zigzag Conversion/Solution.cpp index ab137f907945b..f66b5d959b459 100644 --- a/solution/0000-0099/0006.Zigzag Conversion/Solution.cpp +++ b/solution/0000-0099/0006.Zigzag Conversion/Solution.cpp @@ -4,12 +4,10 @@ class Solution { if (numRows == 1) return s; string ans; int group = 2 * numRows - 2; - for (int i = 1; i <= numRows; ++i) - { + for (int i = 1; i <= numRows; ++i) { int interval = i == numRows ? group : 2 * numRows - 2 * i; int idx = i - 1; - while (idx < s.length()) - { + while (idx < s.length()) { ans.push_back(s[idx]); idx += interval; interval = group - interval; diff --git a/solution/0000-0099/0007.Reverse Integer/README.md b/solution/0000-0099/0007.Reverse Integer/README.md index f623f66315fdb..85f5319781086 100644 --- a/solution/0000-0099/0007.Reverse Integer/README.md +++ b/solution/0000-0099/0007.Reverse Integer/README.md @@ -123,7 +123,7 @@ var reverse = function (x) { ### **C** ```c -int reverse(int x){ +int reverse(int x) { int res = 0; while (x != 0) { if (res > INT_MAX / 10 || res < INT_MIN / 10) { diff --git a/solution/0000-0099/0007.Reverse Integer/README_EN.md b/solution/0000-0099/0007.Reverse Integer/README_EN.md index ab32d0ba4cd57..42f6ab874e8c5 100644 --- a/solution/0000-0099/0007.Reverse Integer/README_EN.md +++ b/solution/0000-0099/0007.Reverse Integer/README_EN.md @@ -102,7 +102,7 @@ var reverse = function (x) { ### **C** ```c -int reverse(int x){ +int reverse(int x) { int res = 0; while (x != 0) { if (res > INT_MAX / 10 || res < INT_MIN / 10) { diff --git a/solution/0000-0099/0007.Reverse Integer/Solution.c b/solution/0000-0099/0007.Reverse Integer/Solution.c index 32231c044fb6b..c37f54edd22a6 100644 --- a/solution/0000-0099/0007.Reverse Integer/Solution.c +++ b/solution/0000-0099/0007.Reverse Integer/Solution.c @@ -1,4 +1,4 @@ -int reverse(int x){ +int reverse(int x) { int res = 0; while (x != 0) { if (res > INT_MAX / 10 || res < INT_MIN / 10) { diff --git a/solution/0000-0099/0011.Container With Most Water/README.md b/solution/0000-0099/0011.Container With Most Water/README.md index f060180aadbc1..9a3e4df5312e0 100644 --- a/solution/0000-0099/0011.Container With Most Water/README.md +++ b/solution/0000-0099/0011.Container With Most Water/README.md @@ -112,8 +112,10 @@ public: while (i < j) { int t = (j - i) * min(height[i], height[j]); res = max(res, t); - if (height[i] < height[j]) ++i; - else --j; + if (height[i] < height[j]) + ++i; + else + --j; } return res; } diff --git a/solution/0000-0099/0011.Container With Most Water/README_EN.md b/solution/0000-0099/0011.Container With Most Water/README_EN.md index afa62007b40c1..486295e309566 100644 --- a/solution/0000-0099/0011.Container With Most Water/README_EN.md +++ b/solution/0000-0099/0011.Container With Most Water/README_EN.md @@ -87,8 +87,10 @@ public: while (i < j) { int t = (j - i) * min(height[i], height[j]); res = max(res, t); - if (height[i] < height[j]) ++i; - else --j; + if (height[i] < height[j]) + ++i; + else + --j; } return res; } diff --git a/solution/0000-0099/0011.Container With Most Water/Solution.cpp b/solution/0000-0099/0011.Container With Most Water/Solution.cpp index 8608fe21a4772..2d0256eb6c6ee 100644 --- a/solution/0000-0099/0011.Container With Most Water/Solution.cpp +++ b/solution/0000-0099/0011.Container With Most Water/Solution.cpp @@ -6,8 +6,10 @@ class Solution { while (i < j) { int t = (j - i) * min(height[i], height[j]); res = max(res, t); - if (height[i] < height[j]) ++i; - else --j; + if (height[i] < height[j]) + ++i; + else + --j; } return res; } diff --git a/solution/0000-0099/0012.Integer to Roman/README.md b/solution/0000-0099/0012.Integer to Roman/README.md index 7a839c8c20c0b..d11aec844ee07 100644 --- a/solution/0000-0099/0012.Integer to Roman/README.md +++ b/solution/0000-0099/0012.Integer to Roman/README.md @@ -124,8 +124,8 @@ class Solution { class Solution { public: string intToRoman(int num) { - vector nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; - vector romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; + vector nums {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; + vector romans {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; string ans; for (int i = 0; i < nums.size(); ++i) { while (num >= nums[i]) { diff --git a/solution/0000-0099/0012.Integer to Roman/README_EN.md b/solution/0000-0099/0012.Integer to Roman/README_EN.md index 3e45ff021e2c2..034398c9ac7e6 100644 --- a/solution/0000-0099/0012.Integer to Roman/README_EN.md +++ b/solution/0000-0099/0012.Integer to Roman/README_EN.md @@ -103,8 +103,8 @@ class Solution { class Solution { public: string intToRoman(int num) { - vector nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; - vector romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; + vector nums {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; + vector romans {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; string ans; for (int i = 0; i < nums.size(); ++i) { while (num >= nums[i]) { diff --git a/solution/0000-0099/0012.Integer to Roman/Solution.cpp b/solution/0000-0099/0012.Integer to Roman/Solution.cpp index 60bad782ccf77..95d5732c5db65 100644 --- a/solution/0000-0099/0012.Integer to Roman/Solution.cpp +++ b/solution/0000-0099/0012.Integer to Roman/Solution.cpp @@ -1,8 +1,8 @@ class Solution { public: string intToRoman(int num) { - vector nums{1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; - vector romans{"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; + vector nums {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1}; + vector romans {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"}; string ans; for (int i = 0; i < nums.size(); ++i) { while (num >= nums[i]) { diff --git a/solution/0000-0099/0013.Roman to Integer/README.md b/solution/0000-0099/0013.Roman to Integer/README.md index bc5b5edab69f5..1bca93c071120 100644 --- a/solution/0000-0099/0013.Roman to Integer/README.md +++ b/solution/0000-0099/0013.Roman to Integer/README.md @@ -157,9 +157,9 @@ class Solution { ```cpp class Solution { - public: +public: int romanToInt(string s) { - unordered_map nums{ + unordered_map nums { {'I', 1}, {'V', 5}, {'X', 10}, diff --git a/solution/0000-0099/0013.Roman to Integer/README_EN.md b/solution/0000-0099/0013.Roman to Integer/README_EN.md index bcea5a2ab5b5f..d93427c45fc9f 100644 --- a/solution/0000-0099/0013.Roman to Integer/README_EN.md +++ b/solution/0000-0099/0013.Roman to Integer/README_EN.md @@ -135,9 +135,9 @@ class Solution { ```cpp class Solution { - public: +public: int romanToInt(string s) { - unordered_map nums{ + unordered_map nums { {'I', 1}, {'V', 5}, {'X', 10}, diff --git a/solution/0000-0099/0013.Roman to Integer/Solution.cpp b/solution/0000-0099/0013.Roman to Integer/Solution.cpp index 142ddfb4bfab0..9dc652ab51512 100644 --- a/solution/0000-0099/0013.Roman to Integer/Solution.cpp +++ b/solution/0000-0099/0013.Roman to Integer/Solution.cpp @@ -1,7 +1,7 @@ class Solution { - public: +public: int romanToInt(string s) { - unordered_map nums{ + unordered_map nums { {'I', 1}, {'V', 5}, {'X', 10}, diff --git a/solution/0000-0099/0020.Valid Parentheses/README.md b/solution/0000-0099/0020.Valid Parentheses/README.md index 13b02303021c3..a13f5ff1e7567 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README.md +++ b/solution/0000-0099/0020.Valid Parentheses/README.md @@ -130,12 +130,16 @@ public: bool isValid(string s) { stack q; for (int i = 0, n = s.length(); i < n; ++i) { - if (s[i] == '{' || s[i] == '[' || s[i] == '(') q.push(s[i]); - else if (q.empty() || !match(q.top(), s[i])) return false; - else q.pop(); + if (s[i] == '{' || s[i] == '[' || s[i] == '(') + q.push(s[i]); + else if (q.empty() || !match(q.top(), s[i])) + return false; + else + q.pop(); } return q.empty(); } + private: bool match(char l, char r) { return (l == '(' && r == ')') || (l == '[' && r == ']') || (l == '{' && r == '}'); diff --git a/solution/0000-0099/0020.Valid Parentheses/README_EN.md b/solution/0000-0099/0020.Valid Parentheses/README_EN.md index ed83c74d2357c..52132f48e0873 100644 --- a/solution/0000-0099/0020.Valid Parentheses/README_EN.md +++ b/solution/0000-0099/0020.Valid Parentheses/README_EN.md @@ -91,12 +91,16 @@ public: bool isValid(string s) { stack q; for (int i = 0, n = s.length(); i < n; ++i) { - if (s[i] == '{' || s[i] == '[' || s[i] == '(') q.push(s[i]); - else if (q.empty() || !match(q.top(), s[i])) return false; - else q.pop(); + if (s[i] == '{' || s[i] == '[' || s[i] == '(') + q.push(s[i]); + else if (q.empty() || !match(q.top(), s[i])) + return false; + else + q.pop(); } return q.empty(); } + private: bool match(char l, char r) { return (l == '(' && r == ')') || (l == '[' && r == ']') || (l == '{' && r == '}'); diff --git a/solution/0000-0099/0020.Valid Parentheses/Solution.cpp b/solution/0000-0099/0020.Valid Parentheses/Solution.cpp index 3ac88b2dc397e..087c988cd3dc9 100644 --- a/solution/0000-0099/0020.Valid Parentheses/Solution.cpp +++ b/solution/0000-0099/0020.Valid Parentheses/Solution.cpp @@ -3,12 +3,16 @@ class Solution { bool isValid(string s) { stack q; for (int i = 0, n = s.length(); i < n; ++i) { - if (s[i] == '{' || s[i] == '[' || s[i] == '(') q.push(s[i]); - else if (q.empty() || !match(q.top(), s[i])) return false; - else q.pop(); + if (s[i] == '{' || s[i] == '[' || s[i] == '(') + q.push(s[i]); + else if (q.empty() || !match(q.top(), s[i])) + return false; + else + q.pop(); } return q.empty(); } + private: bool match(char l, char r) { return (l == '(' && r == ')') || (l == '[' && r == ']') || (l == '{' && r == '}'); diff --git a/solution/0000-0099/0021.Merge Two Sorted Lists/README.md b/solution/0000-0099/0021.Merge Two Sorted Lists/README.md index ee72563972f84..a20eff0198491 100644 --- a/solution/0000-0099/0021.Merge Two Sorted Lists/README.md +++ b/solution/0000-0099/0021.Merge Two Sorted Lists/README.md @@ -180,13 +180,10 @@ public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { if (!list1) return list2; if (!list2) return list1; - if (list1->val <= list2->val) - { + if (list1->val <= list2->val) { list1->next = mergeTwoLists(list1->next, list2); return list1; - } - else - { + } else { list2->next = mergeTwoLists(list1, list2->next); return list2; } @@ -210,15 +207,11 @@ public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { ListNode* dummy = new ListNode(); ListNode* curr = dummy; - while (list1 && list2) - { - if (list1->val <= list2->val) - { + while (list1 && list2) { + if (list1->val <= list2->val) { curr->next = list1; list1 = list1->next; - } - else - { + } else { curr->next = list2; list2 = list2->next; } diff --git a/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md b/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md index 6f261430334ee..3d0904e06aef1 100644 --- a/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md +++ b/solution/0000-0099/0021.Merge Two Sorted Lists/README_EN.md @@ -168,13 +168,10 @@ public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { if (!list1) return list2; if (!list2) return list1; - if (list1->val <= list2->val) - { + if (list1->val <= list2->val) { list1->next = mergeTwoLists(list1->next, list2); return list1; - } - else - { + } else { list2->next = mergeTwoLists(list1, list2->next); return list2; } @@ -198,15 +195,11 @@ public: ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { ListNode* dummy = new ListNode(); ListNode* curr = dummy; - while (list1 && list2) - { - if (list1->val <= list2->val) - { + while (list1 && list2) { + if (list1->val <= list2->val) { curr->next = list1; list1 = list1->next; - } - else - { + } else { curr->next = list2; list2 = list2->next; } diff --git a/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.cpp b/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.cpp index 4befa2c4082d0..29bc8c5bf002c 100644 --- a/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.cpp +++ b/solution/0000-0099/0021.Merge Two Sorted Lists/Solution.cpp @@ -13,15 +13,11 @@ class Solution { ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) { ListNode* dummy = new ListNode(); ListNode* curr = dummy; - while (list1 && list2) - { - if (list1->val <= list2->val) - { + while (list1 && list2) { + if (list1->val <= list2->val) { curr->next = list1; list1 = list1->next; - } - else - { + } else { curr->next = list2; list2 = list2->next; } diff --git a/solution/0000-0099/0022.Generate Parentheses/README.md b/solution/0000-0099/0022.Generate Parentheses/README.md index 7303891e10d2a..288036fd2130e 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README.md +++ b/solution/0000-0099/0022.Generate Parentheses/README.md @@ -100,8 +100,7 @@ public: } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git a/solution/0000-0099/0022.Generate Parentheses/README_EN.md b/solution/0000-0099/0022.Generate Parentheses/README_EN.md index 69e56df8e7439..4f9d0ade48e42 100644 --- a/solution/0000-0099/0022.Generate Parentheses/README_EN.md +++ b/solution/0000-0099/0022.Generate Parentheses/README_EN.md @@ -83,8 +83,7 @@ public: } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git a/solution/0000-0099/0022.Generate Parentheses/Solution.cpp b/solution/0000-0099/0022.Generate Parentheses/Solution.cpp index 2af50ad429a32..37d8d0850aaf4 100644 --- a/solution/0000-0099/0022.Generate Parentheses/Solution.cpp +++ b/solution/0000-0099/0022.Generate Parentheses/Solution.cpp @@ -7,8 +7,7 @@ class Solution { } void dfs(int left, int right, int n, string t, vector& ans) { - if (left == n && right == n) - { + if (left == n && right == n) { ans.push_back(t); return; } diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/README.md b/solution/0000-0099/0023.Merge k Sorted Lists/README.md index bd3934394b56d..a9dd7e6eb7bba 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/README.md +++ b/solution/0000-0099/0023.Merge k Sorted Lists/README.md @@ -173,15 +173,11 @@ private: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md b/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md index 4b663624b34be..8eafc379c3c4b 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md +++ b/solution/0000-0099/0023.Merge k Sorted Lists/README_EN.md @@ -156,15 +156,11 @@ private: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git a/solution/0000-0099/0023.Merge k Sorted Lists/Solution.cpp b/solution/0000-0099/0023.Merge k Sorted Lists/Solution.cpp index 1c23143159e04..a6aad3f61836f 100644 --- a/solution/0000-0099/0023.Merge k Sorted Lists/Solution.cpp +++ b/solution/0000-0099/0023.Merge k Sorted Lists/Solution.cpp @@ -21,15 +21,11 @@ class Solution { ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { ListNode* dummy = new ListNode(); ListNode* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/README.md b/solution/0000-0099/0024.Swap Nodes in Pairs/README.md index 59e9a67278e2a..53459084bfd2f 100644 --- a/solution/0000-0099/0024.Swap Nodes in Pairs/README.md +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/README.md @@ -149,10 +149,10 @@ var swapPairs = function (head) { class Solution { public: ListNode* swapPairs(ListNode* head) { - ListNode *dummy = new ListNode(0, head); + ListNode* dummy = new ListNode(0, head); ListNode *pre = dummy, *cur = head; while (cur != nullptr && cur->next != nullptr) { - ListNode *t = cur->next; + ListNode* t = cur->next; cur->next = t->next; t->next = cur; pre->next = t; diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md b/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md index ff604fc8f48fe..de415ab59ab21 100644 --- a/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/README_EN.md @@ -137,10 +137,10 @@ var swapPairs = function (head) { class Solution { public: ListNode* swapPairs(ListNode* head) { - ListNode *dummy = new ListNode(0, head); + ListNode* dummy = new ListNode(0, head); ListNode *pre = dummy, *cur = head; while (cur != nullptr && cur->next != nullptr) { - ListNode *t = cur->next; + ListNode* t = cur->next; cur->next = t->next; t->next = cur; pre->next = t; diff --git a/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cpp b/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cpp index cc35b3c693fb2..17ad78cd71521 100644 --- a/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cpp +++ b/solution/0000-0099/0024.Swap Nodes in Pairs/Solution.cpp @@ -11,10 +11,10 @@ class Solution { public: ListNode* swapPairs(ListNode* head) { - ListNode *dummy = new ListNode(0, head); + ListNode* dummy = new ListNode(0, head); ListNode *pre = dummy, *cur = head; while (cur != nullptr && cur->next != nullptr) { - ListNode *t = cur->next; + ListNode* t = cur->next; cur->next = t->next; t->next = cur; pre->next = t; diff --git a/solution/0000-0099/0027.Remove Element/README.md b/solution/0000-0099/0027.Remove Element/README.md index d24bbf093b4e3..f1a34cc9cb5e7 100644 --- a/solution/0000-0099/0027.Remove Element/README.md +++ b/solution/0000-0099/0027.Remove Element/README.md @@ -108,8 +108,10 @@ public: int removeElement(vector& nums, int val) { int cnt = 0, n = nums.size(); for (int i = 0; i < n; ++i) { - if (nums[i] == val) ++cnt; - else nums[i - cnt] = nums[i]; + if (nums[i] == val) + ++cnt; + else + nums[i - cnt] = nums[i]; } return n - cnt; } diff --git a/solution/0000-0099/0027.Remove Element/README_EN.md b/solution/0000-0099/0027.Remove Element/README_EN.md index ec8d70c3f47c1..7f30147c7681f 100644 --- a/solution/0000-0099/0027.Remove Element/README_EN.md +++ b/solution/0000-0099/0027.Remove Element/README_EN.md @@ -103,8 +103,10 @@ public: int removeElement(vector& nums, int val) { int cnt = 0, n = nums.size(); for (int i = 0; i < n; ++i) { - if (nums[i] == val) ++cnt; - else nums[i - cnt] = nums[i]; + if (nums[i] == val) + ++cnt; + else + nums[i - cnt] = nums[i]; } return n - cnt; } diff --git a/solution/0000-0099/0027.Remove Element/Solution.cpp b/solution/0000-0099/0027.Remove Element/Solution.cpp index ad1d1fbad7136..837619453503b 100644 --- a/solution/0000-0099/0027.Remove Element/Solution.cpp +++ b/solution/0000-0099/0027.Remove Element/Solution.cpp @@ -3,8 +3,10 @@ class Solution { int removeElement(vector& nums, int val) { int cnt = 0, n = nums.size(); for (int i = 0; i < n; ++i) { - if (nums[i] == val) ++cnt; - else nums[i - cnt] = nums[i]; + if (nums[i] == val) + ++cnt; + else + nums[i - cnt] = nums[i]; } return n - cnt; } diff --git a/solution/0000-0099/0028.Implement strStr()/README.md b/solution/0000-0099/0028.Implement strStr()/README.md index d10c2df9603bc..76233b8e6a01a 100644 --- a/solution/0000-0099/0028.Implement strStr()/README.md +++ b/solution/0000-0099/0028.Implement strStr()/README.md @@ -113,56 +113,50 @@ class Solution { ```cpp class Solution { private: - vector Next(string str) - { - vector n(str.length()) ; - n[0] = -1 ; - int i = 0, pre = -1 ; - int len = str.length() ; - while (i < len) - { + vector Next(string str) { + vector n(str.length()); + n[0] = -1; + int i = 0, pre = -1; + int len = str.length(); + while (i < len) { while (pre >= 0 && str[i] != str[pre]) - pre = n[pre] ; - ++i, ++pre ; + pre = n[pre]; + ++i, ++pre; if (i >= len) - break ; + break; if (str[i] == str[pre]) - n[i] = n[pre] ; + n[i] = n[pre]; else - n[i] = pre ; + n[i] = pre; } - return n ; + return n; } + public: int strStr(string haystack, string needle) { if (0 == needle.length()) - return 0 ; - - vector n(Next(needle)) ; + return 0; - int len = haystack.length() - needle.length() + 1 ; - for (int i = 0; i < len; ++i) - { - int j = 0, k = i ; - while (j < needle.length() && k < haystack.length()) - { - if (haystack[k] != needle[j]) - { - if (n[j] >= 0) - { - j = n[j] ; - continue ; - } - else - break ; + vector n(Next(needle)); + + int len = haystack.length() - needle.length() + 1; + for (int i = 0; i < len; ++i) { + int j = 0, k = i; + while (j < needle.length() && k < haystack.length()) { + if (haystack[k] != needle[j]) { + if (n[j] >= 0) { + j = n[j]; + continue; + } else + break; } - ++k, ++j ; + ++k, ++j; } if (j >= needle.length()) - return k-j ; + return k - j; } - return -1 ; + return -1; } }; ``` diff --git a/solution/0000-0099/0028.Implement strStr()/README_EN.md b/solution/0000-0099/0028.Implement strStr()/README_EN.md index f5b319a3fe3b3..cfd29d1ee8a08 100644 --- a/solution/0000-0099/0028.Implement strStr()/README_EN.md +++ b/solution/0000-0099/0028.Implement strStr()/README_EN.md @@ -103,56 +103,50 @@ class Solution { ```cpp class Solution { private: - vector Next(string str) - { - vector n(str.length()) ; - n[0] = -1 ; - int i = 0, pre = -1 ; - int len = str.length() ; - while (i < len) - { + vector Next(string str) { + vector n(str.length()); + n[0] = -1; + int i = 0, pre = -1; + int len = str.length(); + while (i < len) { while (pre >= 0 && str[i] != str[pre]) - pre = n[pre] ; - ++i, ++pre ; + pre = n[pre]; + ++i, ++pre; if (i >= len) - break ; + break; if (str[i] == str[pre]) - n[i] = n[pre] ; + n[i] = n[pre]; else - n[i] = pre ; + n[i] = pre; } - return n ; + return n; } + public: int strStr(string haystack, string needle) { if (0 == needle.length()) - return 0 ; - - vector n(Next(needle)) ; + return 0; - int len = haystack.length() - needle.length() + 1 ; - for (int i = 0; i < len; ++i) - { - int j = 0, k = i ; - while (j < needle.length() && k < haystack.length()) - { - if (haystack[k] != needle[j]) - { - if (n[j] >= 0) - { - j = n[j] ; - continue ; - } - else - break ; + vector n(Next(needle)); + + int len = haystack.length() - needle.length() + 1; + for (int i = 0; i < len; ++i) { + int j = 0, k = i; + while (j < needle.length() && k < haystack.length()) { + if (haystack[k] != needle[j]) { + if (n[j] >= 0) { + j = n[j]; + continue; + } else + break; } - ++k, ++j ; + ++k, ++j; } if (j >= needle.length()) - return k-j ; + return k - j; } - return -1 ; + return -1; } }; ``` diff --git a/solution/0000-0099/0028.Implement strStr()/Solution.cpp b/solution/0000-0099/0028.Implement strStr()/Solution.cpp index 3ac257af76ad7..493c1e8d6eccf 100644 --- a/solution/0000-0099/0028.Implement strStr()/Solution.cpp +++ b/solution/0000-0099/0028.Implement strStr()/Solution.cpp @@ -1,54 +1,48 @@ class Solution { private: - vector Next(string str) - { - vector n(str.length()) ; - n[0] = -1 ; - int i = 0, pre = -1 ; - int len = str.length() ; - while (i < len) - { + vector Next(string str) { + vector n(str.length()); + n[0] = -1; + int i = 0, pre = -1; + int len = str.length(); + while (i < len) { while (pre >= 0 && str[i] != str[pre]) - pre = n[pre] ; - ++i, ++pre ; + pre = n[pre]; + ++i, ++pre; if (i >= len) - break ; + break; if (str[i] == str[pre]) - n[i] = n[pre] ; + n[i] = n[pre]; else - n[i] = pre ; + n[i] = pre; } - return n ; + return n; } + public: int strStr(string haystack, string needle) { if (0 == needle.length()) - return 0 ; - - vector n(Next(needle)) ; - - int len = haystack.length() - needle.length() + 1 ; - for (int i = 0; i < len; ++i) - { - int j = 0, k = i ; - while (j < needle.length() && k < haystack.length()) - { - if (haystack[k] != needle[j]) - { - if (n[j] >= 0) - { - j = n[j] ; - continue ; - } - else - break ; + return 0; + + vector n(Next(needle)); + + int len = haystack.length() - needle.length() + 1; + for (int i = 0; i < len; ++i) { + int j = 0, k = i; + while (j < needle.length() && k < haystack.length()) { + if (haystack[k] != needle[j]) { + if (n[j] >= 0) { + j = n[j]; + continue; + } else + break; } - ++k, ++j ; + ++k, ++j; } if (j >= needle.length()) - return k-j ; + return k - j; } - - return -1 ; + + return -1; } }; \ No newline at end of file diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md b/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md index 436bdd72d91be..97d08afe82a9d 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/README.md @@ -147,17 +147,14 @@ public: int subLen = words[0].size(); int n = s.size(), m = words.size(); vector ans; - for (int i = 0; i < subLen; ++i) - { + for (int i = 0; i < subLen; ++i) { unordered_map cnt1; int l = i, r = i; int t = 0; - while (r + subLen <= n) - { + while (r + subLen <= n) { string w = s.substr(r, subLen); r += subLen; - if (!cnt.count(w)) - { + if (!cnt.count(w)) { l = r; t = 0; cnt1.clear(); @@ -165,8 +162,7 @@ public: } cnt1[w]++; t++; - while (cnt1[w] > cnt[w]) - { + while (cnt1[w] > cnt[w]) { string remove = s.substr(l, subLen); l += subLen; cnt1[remove]--; diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md b/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md index 5354dae85cbd0..310d1eeb455ca 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md @@ -133,17 +133,14 @@ public: int subLen = words[0].size(); int n = s.size(), m = words.size(); vector ans; - for (int i = 0; i < subLen; ++i) - { + for (int i = 0; i < subLen; ++i) { unordered_map cnt1; int l = i, r = i; int t = 0; - while (r + subLen <= n) - { + while (r + subLen <= n) { string w = s.substr(r, subLen); r += subLen; - if (!cnt.count(w)) - { + if (!cnt.count(w)) { l = r; t = 0; cnt1.clear(); @@ -151,8 +148,7 @@ public: } cnt1[w]++; t++; - while (cnt1[w] > cnt[w]) - { + while (cnt1[w] > cnt[w]) { string remove = s.substr(l, subLen); l += subLen; cnt1[remove]--; diff --git a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp index 194d0f2a2fae5..664f02514cc80 100644 --- a/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp +++ b/solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.cpp @@ -6,17 +6,14 @@ class Solution { int subLen = words[0].size(); int n = s.size(), m = words.size(); vector ans; - for (int i = 0; i < subLen; ++i) - { + for (int i = 0; i < subLen; ++i) { unordered_map cnt1; int l = i, r = i; int t = 0; - while (r + subLen <= n) - { + while (r + subLen <= n) { string w = s.substr(r, subLen); r += subLen; - if (!cnt.count(w)) - { + if (!cnt.count(w)) { l = r; t = 0; cnt1.clear(); @@ -24,8 +21,7 @@ class Solution { } cnt1[w]++; t++; - while (cnt1[w] > cnt[w]) - { + while (cnt1[w] > cnt[w]) { string remove = s.substr(l, subLen); l += subLen; cnt1[remove]--; diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md index 98f455fe02bdf..5763f2f1203bb 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README.md @@ -133,18 +133,18 @@ public: int search(vector& nums, int target) { int n = nums.size(); int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[0] <= nums[mid]) - { - if (nums[0] <= target && target <= nums[mid]) right = mid; - else left = mid + 1; - } - else - { - if (nums[mid] < target && target <= nums[n - 1]) left = mid + 1; - else right = mid; + if (nums[0] <= nums[mid]) { + if (nums[0] <= target && target <= nums[mid]) + right = mid; + else + left = mid + 1; + } else { + if (nums[mid] < target && target <= nums[n - 1]) + left = mid + 1; + else + right = mid; } } return nums[left] == target ? left : -1; diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md b/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md index 8c6a19e511242..d969a4466a7e1 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/README_EN.md @@ -100,18 +100,18 @@ public: int search(vector& nums, int target) { int n = nums.size(); int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[0] <= nums[mid]) - { - if (nums[0] <= target && target <= nums[mid]) right = mid; - else left = mid + 1; - } - else - { - if (nums[mid] < target && target <= nums[n - 1]) left = mid + 1; - else right = mid; + if (nums[0] <= nums[mid]) { + if (nums[0] <= target && target <= nums[mid]) + right = mid; + else + left = mid + 1; + } else { + if (nums[mid] < target && target <= nums[n - 1]) + left = mid + 1; + else + right = mid; } } return nums[left] == target ? left : -1; diff --git a/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cpp b/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cpp index a57d1aea80d5a..0e6381f92c526 100644 --- a/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cpp +++ b/solution/0000-0099/0033.Search in Rotated Sorted Array/Solution.cpp @@ -3,18 +3,18 @@ class Solution { int search(vector& nums, int target) { int n = nums.size(); int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[0] <= nums[mid]) - { - if (nums[0] <= target && target <= nums[mid]) right = mid; - else left = mid + 1; - } - else - { - if (nums[mid] < target && target <= nums[n - 1]) left = mid + 1; - else right = mid; + if (nums[0] <= nums[mid]) { + if (nums[0] <= target && target <= nums[mid]) + right = mid; + else + left = mid + 1; + } else { + if (nums[mid] < target && target <= nums[n - 1]) + left = mid + 1; + else + right = mid; } } return nums[left] == target ? left : -1; diff --git a/solution/0000-0099/0035.Search Insert Position/README.md b/solution/0000-0099/0035.Search Insert Position/README.md index 110333a116924..fde4c93912e2f 100644 --- a/solution/0000-0099/0035.Search Insert Position/README.md +++ b/solution/0000-0099/0035.Search Insert Position/README.md @@ -97,11 +97,12 @@ class Solution { public: int searchInsert(vector& nums, int target) { int left = 0, right = nums.size(); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] >= target) right = mid; - else left = mid + 1; + if (nums[mid] >= target) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0000-0099/0035.Search Insert Position/README_EN.md b/solution/0000-0099/0035.Search Insert Position/README_EN.md index 66e56ec655ee4..e32aa66853d6c 100644 --- a/solution/0000-0099/0035.Search Insert Position/README_EN.md +++ b/solution/0000-0099/0035.Search Insert Position/README_EN.md @@ -87,11 +87,12 @@ class Solution { public: int searchInsert(vector& nums, int target) { int left = 0, right = nums.size(); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] >= target) right = mid; - else left = mid + 1; + if (nums[mid] >= target) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0000-0099/0035.Search Insert Position/Solution.cpp b/solution/0000-0099/0035.Search Insert Position/Solution.cpp index 2d90abd508aa0..6849c3ca0a42f 100644 --- a/solution/0000-0099/0035.Search Insert Position/Solution.cpp +++ b/solution/0000-0099/0035.Search Insert Position/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: int searchInsert(vector& nums, int target) { int left = 0, right = nums.size(); - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] >= target) right = mid; - else left = mid + 1; + if (nums[mid] >= target) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0000-0099/0038.Count and Say/README.md b/solution/0000-0099/0038.Count and Say/README.md index 81aa91510a1ef..a4e96b3a88f8f 100644 --- a/solution/0000-0099/0038.Count and Say/README.md +++ b/solution/0000-0099/0038.Count and Say/README.md @@ -130,11 +130,9 @@ class Solution { public: string countAndSay(int n) { string s = "1"; - while (--n) - { + while (--n) { string t = ""; - for (int i = 0; i < s.size();) - { + for (int i = 0; i < s.size();) { int j = i; while (j < s.size() && s[j] == s[i]) ++j; t += to_string(j - i); diff --git a/solution/0000-0099/0038.Count and Say/README_EN.md b/solution/0000-0099/0038.Count and Say/README_EN.md index c1bfcdf5e60fc..03be489fbb252 100644 --- a/solution/0000-0099/0038.Count and Say/README_EN.md +++ b/solution/0000-0099/0038.Count and Say/README_EN.md @@ -100,11 +100,9 @@ class Solution { public: string countAndSay(int n) { string s = "1"; - while (--n) - { + while (--n) { string t = ""; - for (int i = 0; i < s.size();) - { + for (int i = 0; i < s.size();) { int j = i; while (j < s.size() && s[j] == s[i]) ++j; t += to_string(j - i); diff --git a/solution/0000-0099/0038.Count and Say/Solution.cpp b/solution/0000-0099/0038.Count and Say/Solution.cpp index 697bd1cb41d6c..d766e9ecc6f88 100644 --- a/solution/0000-0099/0038.Count and Say/Solution.cpp +++ b/solution/0000-0099/0038.Count and Say/Solution.cpp @@ -2,11 +2,9 @@ class Solution { public: string countAndSay(int n) { string s = "1"; - while (--n) - { + while (--n) { string t = ""; - for (int i = 0; i < s.size();) - { + for (int i = 0; i < s.size();) { int j = i; while (j < s.size() && s[j] == s[i]) ++j; t += to_string(j - i); diff --git a/solution/0000-0099/0039.Combination Sum/README.md b/solution/0000-0099/0039.Combination Sum/README.md index e87a490ff1524..a464b8df56f06 100644 --- a/solution/0000-0099/0039.Combination Sum/README.md +++ b/solution/0000-0099/0039.Combination Sum/README.md @@ -136,14 +136,12 @@ public: } void dfs(int s, int u, vector& t) { - if (s == target) - { + if (s == target) { ans.push_back(t); return; } if (s > target) return; - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { int c = candidates[i]; t.push_back(c); dfs(s + c, i, t); diff --git a/solution/0000-0099/0039.Combination Sum/README_EN.md b/solution/0000-0099/0039.Combination Sum/README_EN.md index b03d390edfc51..f2228a1197010 100644 --- a/solution/0000-0099/0039.Combination Sum/README_EN.md +++ b/solution/0000-0099/0039.Combination Sum/README_EN.md @@ -126,14 +126,12 @@ public: } void dfs(int s, int u, vector& t) { - if (s == target) - { + if (s == target) { ans.push_back(t); return; } if (s > target) return; - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { int c = candidates[i]; t.push_back(c); dfs(s + c, i, t); diff --git a/solution/0000-0099/0039.Combination Sum/Solution.cpp b/solution/0000-0099/0039.Combination Sum/Solution.cpp index 67384a822a2fc..3810f6ce654c2 100644 --- a/solution/0000-0099/0039.Combination Sum/Solution.cpp +++ b/solution/0000-0099/0039.Combination Sum/Solution.cpp @@ -13,14 +13,12 @@ class Solution { } void dfs(int s, int u, vector& t) { - if (s == target) - { + if (s == target) { ans.push_back(t); return; } if (s > target) return; - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { int c = candidates[i]; t.push_back(c); dfs(s + c, i, t); diff --git a/solution/0000-0099/0040.Combination Sum II/README.md b/solution/0000-0099/0040.Combination Sum II/README.md index ac056a9051834..ce7fbc62e1a7a 100644 --- a/solution/0000-0099/0040.Combination Sum II/README.md +++ b/solution/0000-0099/0040.Combination Sum II/README.md @@ -147,13 +147,11 @@ public: void dfs(int u, int s, vector& t) { if (s > target) return; - if (s == target) - { + if (s == target) { ans.push_back(t); return; } - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { if (i > u && candidates[i] == candidates[i - 1]) continue; t.push_back(candidates[i]); dfs(i + 1, s + candidates[i], t); diff --git a/solution/0000-0099/0040.Combination Sum II/README_EN.md b/solution/0000-0099/0040.Combination Sum II/README_EN.md index 3cdee825bce3c..a4d9bbb9ca6cb 100644 --- a/solution/0000-0099/0040.Combination Sum II/README_EN.md +++ b/solution/0000-0099/0040.Combination Sum II/README_EN.md @@ -132,13 +132,11 @@ public: void dfs(int u, int s, vector& t) { if (s > target) return; - if (s == target) - { + if (s == target) { ans.push_back(t); return; } - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { if (i > u && candidates[i] == candidates[i - 1]) continue; t.push_back(candidates[i]); dfs(i + 1, s + candidates[i], t); diff --git a/solution/0000-0099/0040.Combination Sum II/Solution.cpp b/solution/0000-0099/0040.Combination Sum II/Solution.cpp index dfd70d3a3d34e..f41905337945e 100644 --- a/solution/0000-0099/0040.Combination Sum II/Solution.cpp +++ b/solution/0000-0099/0040.Combination Sum II/Solution.cpp @@ -16,13 +16,11 @@ class Solution { void dfs(int u, int s, vector& t) { if (s > target) return; - if (s == target) - { + if (s == target) { ans.push_back(t); return; } - for (int i = u; i < candidates.size(); ++i) - { + for (int i = u; i < candidates.size(); ++i) { if (i > u && candidates[i] == candidates[i - 1]) continue; t.push_back(candidates[i]); dfs(i + 1, s + candidates[i], t); diff --git a/solution/0000-0099/0041.First Missing Positive/README.md b/solution/0000-0099/0041.First Missing Positive/README.md index 4829dd7b14af4..909ea512b5689 100644 --- a/solution/0000-0099/0041.First Missing Positive/README.md +++ b/solution/0000-0099/0041.First Missing Positive/README.md @@ -134,17 +134,17 @@ public class Solution { class Solution { public: int firstMissingPositive(vector& nums) { - sort(nums.begin(),nums.end()); + sort(nums.begin(), nums.end()); int len = nums.size(); - if(len == 0)return 1; + if (len == 0) return 1; int i = 0; - while(nums[i] <= 0 && i < len)i++; - if(i == len)return 1; + while (nums[i] <= 0 && i < len) i++; + if (i == len) return 1; int tmp = 1; - while(ii+1 && nums[i] == nums[i+1])i++;//去重 + while (i < len) { + if (nums[i] != tmp) return tmp; + while (len > i + 1 && nums[i] == nums[i + 1]) i++; //去重 i++; tmp++; } @@ -160,19 +160,19 @@ int firstMissingPositive(int* nums, int numsSize) { int Max = nums[0], i, *Count; - for(i = 1; i 0){ + Count = (int*)calloc(Max + 1, sizeof(int)); + for (i = 0; i < numsSize; i++) { + if (nums[i] > 0) { Count[nums[i]]++; } } i = 1; - while(Count[i] != 0){ + while (Count[i] != 0) { i++; } diff --git a/solution/0000-0099/0041.First Missing Positive/README_EN.md b/solution/0000-0099/0041.First Missing Positive/README_EN.md index 8d32767cd1fdf..3e9e6fed5f362 100644 --- a/solution/0000-0099/0041.First Missing Positive/README_EN.md +++ b/solution/0000-0099/0041.First Missing Positive/README_EN.md @@ -82,17 +82,17 @@ public class Solution { class Solution { public: int firstMissingPositive(vector& nums) { - sort(nums.begin(),nums.end()); + sort(nums.begin(), nums.end()); int len = nums.size(); - if(len == 0)return 1; + if (len == 0) return 1; int i = 0; - while(nums[i] <= 0 && i < len)i++; - if(i == len)return 1; + while (nums[i] <= 0 && i < len) i++; + if (i == len) return 1; int tmp = 1; - while(ii+1 && nums[i] == nums[i+1])i++;//去重 + while (i < len) { + if (nums[i] != tmp) return tmp; + while (len > i + 1 && nums[i] == nums[i + 1]) i++; //去重 i++; tmp++; } @@ -108,19 +108,19 @@ int firstMissingPositive(int* nums, int numsSize) { int Max = nums[0], i, *Count; - for(i = 1; i 0){ + Count = (int*)calloc(Max + 1, sizeof(int)); + for (i = 0; i < numsSize; i++) { + if (nums[i] > 0) { Count[nums[i]]++; } } i = 1; - while(Count[i] != 0){ + while (Count[i] != 0) { i++; } diff --git a/solution/0000-0099/0041.First Missing Positive/Solution.c b/solution/0000-0099/0041.First Missing Positive/Solution.c index 6e859a40cb28e..ae162be6322cc 100644 --- a/solution/0000-0099/0041.First Missing Positive/Solution.c +++ b/solution/0000-0099/0041.First Missing Positive/Solution.c @@ -1,22 +1,22 @@ int firstMissingPositive(int* nums, int numsSize) { - + int Max = nums[0], i, *Count; - - for(i = 1; i 0){ + + Count = (int*)calloc(Max + 1, sizeof(int)); + for (i = 0; i < numsSize; i++) { + if (nums[i] > 0) { Count[nums[i]]++; } } - + i = 1; - while(Count[i] != 0){ + while (Count[i] != 0) { i++; } - + return i; } diff --git a/solution/0000-0099/0041.First Missing Positive/Solution.cpp b/solution/0000-0099/0041.First Missing Positive/Solution.cpp index 113ec3e3042d2..8ee719ed97c03 100644 --- a/solution/0000-0099/0041.First Missing Positive/Solution.cpp +++ b/solution/0000-0099/0041.First Missing Positive/Solution.cpp @@ -1,17 +1,17 @@ class Solution { public: int firstMissingPositive(vector& nums) { - sort(nums.begin(),nums.end()); + sort(nums.begin(), nums.end()); int len = nums.size(); - if(len == 0)return 1; + if (len == 0) return 1; int i = 0; - while(nums[i] <= 0 && i < len)i++; - if(i == len)return 1; + while (nums[i] <= 0 && i < len) i++; + if (i == len) return 1; int tmp = 1; - while(ii+1 && nums[i] == nums[i+1])i++;//去重 + while (i < len) { + if (nums[i] != tmp) return tmp; + while (len > i + 1 && nums[i] == nums[i + 1]) i++; //去重 i++; tmp++; } diff --git a/solution/0000-0099/0042.Trapping Rain Water/Solution.cpp b/solution/0000-0099/0042.Trapping Rain Water/Solution.cpp index 8f96aadae21d9..199d47d5afc25 100644 --- a/solution/0000-0099/0042.Trapping Rain Water/Solution.cpp +++ b/solution/0000-0099/0042.Trapping Rain Water/Solution.cpp @@ -12,7 +12,7 @@ class Solution { lmx[i] = max(lmx[i - 1], height[i]); rmx[n - 1 - i] = max(rmx[n - i], height[n - 1 - i]); } - + int res = 0; for (int i = 0; i < n; ++i) { res += min(lmx[i], rmx[i]) - height[i]; diff --git a/solution/0000-0099/0043.Multiply Strings/README.md b/solution/0000-0099/0043.Multiply Strings/README.md index 2934d93df81df..3a5de81e00b35 100644 --- a/solution/0000-0099/0043.Multiply Strings/README.md +++ b/solution/0000-0099/0043.Multiply Strings/README.md @@ -118,8 +118,7 @@ public: string multiply(string num1, string num2) { int m = num1.size(), n = num2.size(); vector res(m + n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int b = num2[n - 1 - i] - '0'; mul(num1, b, i, res); } @@ -131,16 +130,13 @@ public: } void mul(string A, int b, int i, vector& res) { - for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) - { - if (j >= 0) - { + for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) { + if (j >= 0) { int a = A[j] - '0'; t += a * b; } res[i++] += t % 10; - if (res[i - 1] >= 10) - { + if (res[i - 1] >= 10) { res[i - 1] %= 10; ++res[i]; } diff --git a/solution/0000-0099/0043.Multiply Strings/README_EN.md b/solution/0000-0099/0043.Multiply Strings/README_EN.md index 4cb59fb4737a4..6b3a7fe0e334e 100644 --- a/solution/0000-0099/0043.Multiply Strings/README_EN.md +++ b/solution/0000-0099/0043.Multiply Strings/README_EN.md @@ -103,8 +103,7 @@ public: string multiply(string num1, string num2) { int m = num1.size(), n = num2.size(); vector res(m + n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int b = num2[n - 1 - i] - '0'; mul(num1, b, i, res); } @@ -116,16 +115,13 @@ public: } void mul(string A, int b, int i, vector& res) { - for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) - { - if (j >= 0) - { + for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) { + if (j >= 0) { int a = A[j] - '0'; t += a * b; } res[i++] += t % 10; - if (res[i - 1] >= 10) - { + if (res[i - 1] >= 10) { res[i - 1] %= 10; ++res[i]; } diff --git a/solution/0000-0099/0043.Multiply Strings/Solution.cpp b/solution/0000-0099/0043.Multiply Strings/Solution.cpp index 7a90e294193e9..9eb41bfb7fb31 100644 --- a/solution/0000-0099/0043.Multiply Strings/Solution.cpp +++ b/solution/0000-0099/0043.Multiply Strings/Solution.cpp @@ -3,8 +3,7 @@ class Solution { string multiply(string num1, string num2) { int m = num1.size(), n = num2.size(); vector res(m + n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int b = num2[n - 1 - i] - '0'; mul(num1, b, i, res); } @@ -16,16 +15,13 @@ class Solution { } void mul(string A, int b, int i, vector& res) { - for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) - { - if (j >= 0) - { + for (int j = A.size() - 1, t = 0; j >= 0 || t > 0; --j) { + if (j >= 0) { int a = A[j] - '0'; t += a * b; } res[i++] += t % 10; - if (res[i - 1] >= 10) - { + if (res[i - 1] >= 10) { res[i - 1] %= 10; ++res[i]; } diff --git a/solution/0000-0099/0045.Jump Game II/README.md b/solution/0000-0099/0045.Jump Game II/README.md index 2e5e415de737a..c8583a485e1e8 100644 --- a/solution/0000-0099/0045.Jump Game II/README.md +++ b/solution/0000-0099/0045.Jump Game II/README.md @@ -157,7 +157,7 @@ public class Solution { ```c #define min(a, b) a < b ? a : b -int jump(int* nums, int numsSize){ +int jump(int* nums, int numsSize) { int dp[numsSize]; for (int i = 0; i < numsSize; i++) { dp[i] = numsSize; diff --git a/solution/0000-0099/0045.Jump Game II/README_EN.md b/solution/0000-0099/0045.Jump Game II/README_EN.md index 85eb1df6e99ec..a171dae8d4e0e 100644 --- a/solution/0000-0099/0045.Jump Game II/README_EN.md +++ b/solution/0000-0099/0045.Jump Game II/README_EN.md @@ -142,7 +142,7 @@ public class Solution { ```c #define min(a, b) a < b ? a : b -int jump(int* nums, int numsSize){ +int jump(int* nums, int numsSize) { int dp[numsSize]; for (int i = 0; i < numsSize; i++) { dp[i] = numsSize; diff --git a/solution/0000-0099/0045.Jump Game II/Solution.c b/solution/0000-0099/0045.Jump Game II/Solution.c index 6da48bcb4d191..241e3ddcbf6dd 100644 --- a/solution/0000-0099/0045.Jump Game II/Solution.c +++ b/solution/0000-0099/0045.Jump Game II/Solution.c @@ -1,5 +1,5 @@ #define min(a, b) a < b ? a : b -int jump(int* nums, int numsSize){ +int jump(int* nums, int numsSize) { int dp[numsSize]; for (int i = 0; i < numsSize; i++) { dp[i] = numsSize; diff --git a/solution/0000-0099/0046.Permutations/README.md b/solution/0000-0099/0046.Permutations/README.md index fc42a3714b2e5..32952e03fa7eb 100644 --- a/solution/0000-0099/0046.Permutations/README.md +++ b/solution/0000-0099/0046.Permutations/README.md @@ -157,15 +157,12 @@ public: } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { - if (!used[i]) - { + for (int i = 0; i < n; ++i) { + if (!used[i]) { path[u] = nums[i]; used[i] = true; dfs(u + 1, n, nums, used, path, res); diff --git a/solution/0000-0099/0046.Permutations/README_EN.md b/solution/0000-0099/0046.Permutations/README_EN.md index 3c4ca6cd4853c..c9cd4abaf2c43 100644 --- a/solution/0000-0099/0046.Permutations/README_EN.md +++ b/solution/0000-0099/0046.Permutations/README_EN.md @@ -136,15 +136,12 @@ public: } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { - if (!used[i]) - { + for (int i = 0; i < n; ++i) { + if (!used[i]) { path[u] = nums[i]; used[i] = true; dfs(u + 1, n, nums, used, path, res); diff --git a/solution/0000-0099/0046.Permutations/Solution.cpp b/solution/0000-0099/0046.Permutations/Solution.cpp index db79849dfcc15..a269f66aec666 100644 --- a/solution/0000-0099/0046.Permutations/Solution.cpp +++ b/solution/0000-0099/0046.Permutations/Solution.cpp @@ -10,15 +10,12 @@ class Solution { } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { - if (!used[i]) - { + for (int i = 0; i < n; ++i) { + if (!used[i]) { path[u] = nums[i]; used[i] = true; dfs(u + 1, n, nums, used, path, res); diff --git a/solution/0000-0099/0047.Permutations II/README.md b/solution/0000-0099/0047.Permutations II/README.md index f935e0ffd9acf..cb92cc0fa6da1 100644 --- a/solution/0000-0099/0047.Permutations II/README.md +++ b/solution/0000-0099/0047.Permutations II/README.md @@ -124,13 +124,11 @@ public: } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) continue; path[u] = nums[i]; used[i] = true; diff --git a/solution/0000-0099/0047.Permutations II/README_EN.md b/solution/0000-0099/0047.Permutations II/README_EN.md index 2575724a4e8e0..4d16d36b171b0 100644 --- a/solution/0000-0099/0047.Permutations II/README_EN.md +++ b/solution/0000-0099/0047.Permutations II/README_EN.md @@ -114,13 +114,11 @@ public: } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) continue; path[u] = nums[i]; used[i] = true; diff --git a/solution/0000-0099/0047.Permutations II/Solution.cpp b/solution/0000-0099/0047.Permutations II/Solution.cpp index d39f639ee147c..8c0ea42c608fa 100644 --- a/solution/0000-0099/0047.Permutations II/Solution.cpp +++ b/solution/0000-0099/0047.Permutations II/Solution.cpp @@ -11,13 +11,11 @@ class Solution { } void dfs(int u, int n, vector& nums, vector& used, vector& path, vector>& res) { - if (u == n) - { + if (u == n) { res.emplace_back(path); return; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (used[i] || (i > 0 && nums[i] == nums[i - 1] && !used[i - 1])) continue; path[u] = nums[i]; used[i] = true; diff --git a/solution/0000-0099/0048.Rotate Image/README.md b/solution/0000-0099/0048.Rotate Image/README.md index c76878f1329e4..195d21fa2bd23 100644 --- a/solution/0000-0099/0048.Rotate Image/README.md +++ b/solution/0000-0099/0048.Rotate Image/README.md @@ -97,19 +97,19 @@ public: void rotate(vector>& matrix) { int n = matrix.size(); - if(n <= 1)return ; + if (n <= 1) return; - //先做转置 - for(int i = 0 ; i < n ; i++){ - for(int j = i;j < n ;j++){ - swap(matrix[i][j],matrix[j][i]); + // 先做转置 + for (int i = 0; i < n; i++) { + for (int j = i; j < n; j++) { + swap(matrix[i][j], matrix[j][i]); } } - //再做水平互换 - for(int i = 0 ; i < n ; i++){ - for(int j = 0;j < n/2;j++){ - swap(matrix[i][j],matrix[i][n-1-j]); + // 再做水平互换 + for (int i = 0; i < n; i++) { + for (int j = 0; j < n / 2; j++) { + swap(matrix[i][j], matrix[i][n - 1 - j]); } } } diff --git a/solution/0000-0099/0048.Rotate Image/README_EN.md b/solution/0000-0099/0048.Rotate Image/README_EN.md index af854f1d4e55c..25334de16b53b 100644 --- a/solution/0000-0099/0048.Rotate Image/README_EN.md +++ b/solution/0000-0099/0048.Rotate Image/README_EN.md @@ -85,17 +85,17 @@ public: void rotate(vector>& matrix) { int n = matrix.size(); - if(n <= 1)return ; + if (n <= 1) return; - for(int i = 0 ; i < n ; i++){ - for(int j = i;j < n ;j++){ - swap(matrix[i][j],matrix[j][i]); + for (int i = 0; i < n; i++) { + for (int j = i; j < n; j++) { + swap(matrix[i][j], matrix[j][i]); } } - for(int i = 0 ; i < n ; i++){ - for(int j = 0;j < n/2;j++){ - swap(matrix[i][j],matrix[i][n-1-j]); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n / 2; j++) { + swap(matrix[i][j], matrix[i][n - 1 - j]); } } } diff --git a/solution/0000-0099/0048.Rotate Image/Solution.cpp b/solution/0000-0099/0048.Rotate Image/Solution.cpp index 5c5418ad91e61..60c0e22180d7e 100644 --- a/solution/0000-0099/0048.Rotate Image/Solution.cpp +++ b/solution/0000-0099/0048.Rotate Image/Solution.cpp @@ -1,21 +1,21 @@ class Solution { public: void rotate(vector>& matrix) { - + int n = matrix.size(); - if(n <= 1)return ; - - //先做转置 - for(int i = 0 ; i < n ; i++){ - for(int j = i;j < n ;j++){ - swap(matrix[i][j],matrix[j][i]); + if (n <= 1) return; + + // 先做转置 + for (int i = 0; i < n; i++) { + for (int j = i; j < n; j++) { + swap(matrix[i][j], matrix[j][i]); } } - - //再做水平互换 - for(int i = 0 ; i < n ; i++){ - for(int j = 0;j < n/2;j++){ - swap(matrix[i][j],matrix[i][n-1-j]); + + // 再做水平互换 + for (int i = 0; i < n; i++) { + for (int j = 0; j < n / 2; j++) { + swap(matrix[i][j], matrix[i][n - 1 - j]); } } } diff --git a/solution/0000-0099/0049.Group Anagrams/README.md b/solution/0000-0099/0049.Group Anagrams/README.md index 880322f68b7b5..eb06c0cfe62e2 100644 --- a/solution/0000-0099/0049.Group Anagrams/README.md +++ b/solution/0000-0099/0049.Group Anagrams/README.md @@ -127,17 +127,15 @@ function groupAnagrams(strs: string[]): string[][] { ```cpp class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git a/solution/0000-0099/0049.Group Anagrams/README_EN.md b/solution/0000-0099/0049.Group Anagrams/README_EN.md index b7db8cf395b6f..fafaa9b146ec5 100644 --- a/solution/0000-0099/0049.Group Anagrams/README_EN.md +++ b/solution/0000-0099/0049.Group Anagrams/README_EN.md @@ -94,17 +94,15 @@ function groupAnagrams(strs: string[]): string[][] { ```cpp class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git a/solution/0000-0099/0049.Group Anagrams/Solution.cpp b/solution/0000-0099/0049.Group Anagrams/Solution.cpp index 5b530c70aa9ec..2e25d4fe06c4f 100644 --- a/solution/0000-0099/0049.Group Anagrams/Solution.cpp +++ b/solution/0000-0099/0049.Group Anagrams/Solution.cpp @@ -1,16 +1,14 @@ class Solution { public: - vector> groupAnagrams(vector &strs) { + vector> groupAnagrams(vector& strs) { unordered_map> chars; - for (auto s : strs) - { + for (auto s : strs) { string k = s; sort(k.begin(), k.end()); chars[k].emplace_back(s); } vector> res; - for (auto it = chars.begin(); it != chars.end(); ++it) - { + for (auto it = chars.begin(); it != chars.end(); ++it) { res.emplace_back(it->second); } return res; diff --git a/solution/0000-0099/0051.N-Queens/README.md b/solution/0000-0099/0051.N-Queens/README.md index ef35559c9c875..233d28c4afdc2 100644 --- a/solution/0000-0099/0051.N-Queens/README.md +++ b/solution/0000-0099/0051.N-Queens/README.md @@ -143,15 +143,12 @@ public: } void dfs(int u, int n, vector& col, vector& dg, vector& udg, vector& g, vector>& res) { - if (u == n) - { + if (u == n) { res.push_back(g); return; } - for (int i = 0; i < n; ++i) - { - if (!col[i] && !dg[u + i] && !udg[n - u + i]) - { + for (int i = 0; i < n; ++i) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { g[u][i] = 'Q'; col[i] = dg[u + i] = udg[n - u + i] = true; dfs(u + 1, n, col, dg, udg, g, res); diff --git a/solution/0000-0099/0051.N-Queens/README_EN.md b/solution/0000-0099/0051.N-Queens/README_EN.md index 1016940b1c9d7..58cf3fbb82e54 100644 --- a/solution/0000-0099/0051.N-Queens/README_EN.md +++ b/solution/0000-0099/0051.N-Queens/README_EN.md @@ -123,15 +123,12 @@ public: } void dfs(int u, int n, vector& col, vector& dg, vector& udg, vector& g, vector>& res) { - if (u == n) - { + if (u == n) { res.push_back(g); return; } - for (int i = 0; i < n; ++i) - { - if (!col[i] && !dg[u + i] && !udg[n - u + i]) - { + for (int i = 0; i < n; ++i) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { g[u][i] = 'Q'; col[i] = dg[u + i] = udg[n - u + i] = true; dfs(u + 1, n, col, dg, udg, g, res); diff --git a/solution/0000-0099/0051.N-Queens/Solution.cpp b/solution/0000-0099/0051.N-Queens/Solution.cpp index c4893270b9fdf..21409d1fb3c38 100644 --- a/solution/0000-0099/0051.N-Queens/Solution.cpp +++ b/solution/0000-0099/0051.N-Queens/Solution.cpp @@ -11,15 +11,12 @@ class Solution { } void dfs(int u, int n, vector& col, vector& dg, vector& udg, vector& g, vector>& res) { - if (u == n) - { + if (u == n) { res.push_back(g); return; } - for (int i = 0; i < n; ++i) - { - if (!col[i] && !dg[u + i] && !udg[n - u + i]) - { + for (int i = 0; i < n; ++i) { + if (!col[i] && !dg[u + i] && !udg[n - u + i]) { g[u][i] = 'Q'; col[i] = dg[u + i] = udg[n - u + i] = true; dfs(u + 1, n, col, dg, udg, g, res); diff --git a/solution/0000-0099/0054.Spiral Matrix/README.md b/solution/0000-0099/0054.Spiral Matrix/README.md index 2310dde01dfd1..e0e5670d2ee32 100644 --- a/solution/0000-0099/0054.Spiral Matrix/README.md +++ b/solution/0000-0099/0054.Spiral Matrix/README.md @@ -175,12 +175,10 @@ public: int m = matrix.size(), n = matrix[0].size(); int top = 0, bottom = m - 1, left = 0, right = n - 1; vector ans; - while (top <= bottom && left <= right) - { + while (top <= bottom && left <= right) { for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]); for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]); - if (left < right && top < bottom) - { + if (left < right && top < bottom) { for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]); for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]); } diff --git a/solution/0000-0099/0054.Spiral Matrix/README_EN.md b/solution/0000-0099/0054.Spiral Matrix/README_EN.md index 72ed5e56c0f9e..6a99c01b97760 100644 --- a/solution/0000-0099/0054.Spiral Matrix/README_EN.md +++ b/solution/0000-0099/0054.Spiral Matrix/README_EN.md @@ -163,12 +163,10 @@ public: int m = matrix.size(), n = matrix[0].size(); int top = 0, bottom = m - 1, left = 0, right = n - 1; vector ans; - while (top <= bottom && left <= right) - { + while (top <= bottom && left <= right) { for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]); for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]); - if (left < right && top < bottom) - { + if (left < right && top < bottom) { for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]); for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]); } diff --git a/solution/0000-0099/0054.Spiral Matrix/Solution.cpp b/solution/0000-0099/0054.Spiral Matrix/Solution.cpp index 7c5e4aefeaeed..a477f31d16f40 100644 --- a/solution/0000-0099/0054.Spiral Matrix/Solution.cpp +++ b/solution/0000-0099/0054.Spiral Matrix/Solution.cpp @@ -4,12 +4,10 @@ class Solution { int m = matrix.size(), n = matrix[0].size(); int top = 0, bottom = m - 1, left = 0, right = n - 1; vector ans; - while (top <= bottom && left <= right) - { + while (top <= bottom && left <= right) { for (int j = left; j <= right; ++j) ans.push_back(matrix[top][j]); for (int i = top + 1; i <= bottom; ++i) ans.push_back(matrix[i][right]); - if (left < right && top < bottom) - { + if (left < right && top < bottom) { for (int j = right - 1; j >= left; --j) ans.push_back(matrix[bottom][j]); for (int i = bottom - 1; i > top; --i) ans.push_back(matrix[i][left]); } diff --git a/solution/0000-0099/0056.Merge Intervals/README.md b/solution/0000-0099/0056.Merge Intervals/README.md index 3abd6e3f9e269..c92d7e78369e3 100644 --- a/solution/0000-0099/0056.Merge Intervals/README.md +++ b/solution/0000-0099/0056.Merge Intervals/README.md @@ -117,15 +117,13 @@ public: sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git a/solution/0000-0099/0056.Merge Intervals/README_EN.md b/solution/0000-0099/0056.Merge Intervals/README_EN.md index 1db26493d52e7..5f5f639fbf774 100644 --- a/solution/0000-0099/0056.Merge Intervals/README_EN.md +++ b/solution/0000-0099/0056.Merge Intervals/README_EN.md @@ -87,15 +87,13 @@ public: sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git a/solution/0000-0099/0056.Merge Intervals/Solution.cpp b/solution/0000-0099/0056.Merge Intervals/Solution.cpp index 2b669b600d3fd..6def29301f002 100644 --- a/solution/0000-0099/0056.Merge Intervals/Solution.cpp +++ b/solution/0000-0099/0056.Merge Intervals/Solution.cpp @@ -4,15 +4,13 @@ class Solution { sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git a/solution/0000-0099/0057.Insert Interval/README.md b/solution/0000-0099/0057.Insert Interval/README.md index 02396f8777676..15fa222a2a060 100644 --- a/solution/0000-0099/0057.Insert Interval/README.md +++ b/solution/0000-0099/0057.Insert Interval/README.md @@ -130,15 +130,13 @@ public: sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git a/solution/0000-0099/0057.Insert Interval/README_EN.md b/solution/0000-0099/0057.Insert Interval/README_EN.md index e8642fe285fbd..ce374d8a8ddbf 100644 --- a/solution/0000-0099/0057.Insert Interval/README_EN.md +++ b/solution/0000-0099/0057.Insert Interval/README_EN.md @@ -98,15 +98,13 @@ public: sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git a/solution/0000-0099/0057.Insert Interval/Solution.cpp b/solution/0000-0099/0057.Insert Interval/Solution.cpp index 6e3a5a6ac93db..effd761a48191 100644 --- a/solution/0000-0099/0057.Insert Interval/Solution.cpp +++ b/solution/0000-0099/0057.Insert Interval/Solution.cpp @@ -9,15 +9,13 @@ class Solution { sort(intervals.begin(), intervals.end()); int st = intervals[0][0], ed = intervals[0][1]; vector> ans; - for (int i = 1; i < intervals.size(); ++i) - { + for (int i = 1; i < intervals.size(); ++i) { int s = intervals[i][0], e = intervals[i][1]; - if (ed < s) - { + if (ed < s) { ans.push_back({st, ed}); st = s, ed = e; - } - else ed = max(ed, e); + } else + ed = max(ed, e); } ans.push_back({st, ed}); return ans; diff --git a/solution/0000-0099/0061.Rotate List/README.md b/solution/0000-0099/0061.Rotate List/README.md index 967217f81cd39..4c50b8436204d 100644 --- a/solution/0000-0099/0061.Rotate List/README.md +++ b/solution/0000-0099/0061.Rotate List/README.md @@ -230,7 +230,7 @@ public: return head; } int n = 0; - for (ListNode *cur = head; !!cur; cur = cur->next) { + for (ListNode* cur = head; !!cur; cur = cur->next) { ++n; } k %= n; @@ -246,7 +246,7 @@ public: fast = fast->next; } - ListNode *start = slow->next; + ListNode* start = slow->next; slow->next = nullptr; fast->next = head; return start; diff --git a/solution/0000-0099/0061.Rotate List/README_EN.md b/solution/0000-0099/0061.Rotate List/README_EN.md index fa9373490c898..103d0c6477136 100644 --- a/solution/0000-0099/0061.Rotate List/README_EN.md +++ b/solution/0000-0099/0061.Rotate List/README_EN.md @@ -216,7 +216,7 @@ public: return head; } int n = 0; - for (ListNode *cur = head; !!cur; cur = cur->next) { + for (ListNode* cur = head; !!cur; cur = cur->next) { ++n; } k %= n; @@ -232,7 +232,7 @@ public: fast = fast->next; } - ListNode *start = slow->next; + ListNode* start = slow->next; slow->next = nullptr; fast->next = head; return start; diff --git a/solution/0000-0099/0061.Rotate List/Solution.cpp b/solution/0000-0099/0061.Rotate List/Solution.cpp index 11aa5fa1bd375..32aeb2111db0e 100644 --- a/solution/0000-0099/0061.Rotate List/Solution.cpp +++ b/solution/0000-0099/0061.Rotate List/Solution.cpp @@ -15,7 +15,7 @@ class Solution { return head; } int n = 0; - for (ListNode *cur = head; !!cur; cur = cur->next) { + for (ListNode* cur = head; !!cur; cur = cur->next) { ++n; } k %= n; @@ -31,7 +31,7 @@ class Solution { fast = fast->next; } - ListNode *start = slow->next; + ListNode* start = slow->next; slow->next = nullptr; fast->next = head; return start; diff --git a/solution/0000-0099/0062.Unique Paths/README.md b/solution/0000-0099/0062.Unique Paths/README.md index f017b71c3c18a..65ed50e72f19d 100644 --- a/solution/0000-0099/0062.Unique Paths/README.md +++ b/solution/0000-0099/0062.Unique Paths/README.md @@ -120,10 +120,8 @@ class Solution { public: int uniquePaths(int m, int n) { vector> dp(m, vector(n, 1)); - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } diff --git a/solution/0000-0099/0062.Unique Paths/README_EN.md b/solution/0000-0099/0062.Unique Paths/README_EN.md index dc696174137a1..09fa651c21fe4 100644 --- a/solution/0000-0099/0062.Unique Paths/README_EN.md +++ b/solution/0000-0099/0062.Unique Paths/README_EN.md @@ -94,10 +94,8 @@ class Solution { public: int uniquePaths(int m, int n) { vector> dp(m, vector(n, 1)); - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } diff --git a/solution/0000-0099/0062.Unique Paths/Solution.cpp b/solution/0000-0099/0062.Unique Paths/Solution.cpp index 04f6829f5d650..8aeef0fdfdcce 100644 --- a/solution/0000-0099/0062.Unique Paths/Solution.cpp +++ b/solution/0000-0099/0062.Unique Paths/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: int uniquePaths(int m, int n) { vector> dp(m, vector(n, 1)); - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } } diff --git a/solution/0000-0099/0064.Minimum Path Sum/README.md b/solution/0000-0099/0064.Minimum Path Sum/README.md index 7818584f47f84..e0cf2ac6e5ee5 100644 --- a/solution/0000-0099/0064.Minimum Path Sum/README.md +++ b/solution/0000-0099/0064.Minimum Path Sum/README.md @@ -123,21 +123,17 @@ function minPathSum(grid: number[][]): number { ```cpp class Solution { public: - int minPathSum(vector> &grid) { + int minPathSum(vector>& grid) { int m = grid.size(), n = grid[0].size(); vector> dp(m, vector(n, grid[0][0])); - for (int i = 1; i < m; ++i) - { + for (int i = 1; i < m; ++i) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } - for (int j = 1; j < n; ++j) - { + for (int j = 1; j < n; ++j) { dp[0][j] = dp[0][j - 1] + grid[0][j]; } - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } } diff --git a/solution/0000-0099/0064.Minimum Path Sum/README_EN.md b/solution/0000-0099/0064.Minimum Path Sum/README_EN.md index b525f2753dd44..314730d1da72d 100644 --- a/solution/0000-0099/0064.Minimum Path Sum/README_EN.md +++ b/solution/0000-0099/0064.Minimum Path Sum/README_EN.md @@ -109,21 +109,17 @@ function minPathSum(grid: number[][]): number { ```cpp class Solution { public: - int minPathSum(vector> &grid) { + int minPathSum(vector>& grid) { int m = grid.size(), n = grid[0].size(); vector> dp(m, vector(n, grid[0][0])); - for (int i = 1; i < m; ++i) - { + for (int i = 1; i < m; ++i) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } - for (int j = 1; j < n; ++j) - { + for (int j = 1; j < n; ++j) { dp[0][j] = dp[0][j - 1] + grid[0][j]; } - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } } diff --git a/solution/0000-0099/0064.Minimum Path Sum/Solution.cpp b/solution/0000-0099/0064.Minimum Path Sum/Solution.cpp index 87f905dcee1ec..a772d1d3ce168 100644 --- a/solution/0000-0099/0064.Minimum Path Sum/Solution.cpp +++ b/solution/0000-0099/0064.Minimum Path Sum/Solution.cpp @@ -1,20 +1,16 @@ class Solution { public: - int minPathSum(vector> &grid) { + int minPathSum(vector>& grid) { int m = grid.size(), n = grid[0].size(); vector> dp(m, vector(n, grid[0][0])); - for (int i = 1; i < m; ++i) - { + for (int i = 1; i < m; ++i) { dp[i][0] = dp[i - 1][0] + grid[i][0]; } - for (int j = 1; j < n; ++j) - { + for (int j = 1; j < n; ++j) { dp[0][j] = dp[0][j - 1] + grid[0][j]; } - for (int i = 1; i < m; ++i) - { - for (int j = 1; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 1; j < n; ++j) { dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]; } } diff --git a/solution/0000-0099/0066.Plus One/README.md b/solution/0000-0099/0066.Plus One/README.md index d05cb1ccd7ca2..fc0bbad05eb7b 100644 --- a/solution/0000-0099/0066.Plus One/README.md +++ b/solution/0000-0099/0066.Plus One/README.md @@ -115,8 +115,7 @@ var plusOne = function (digits) { class Solution { public: vector plusOne(vector& digits) { - for (int i = digits.size() - 1; i >= 0; --i) - { + for (int i = digits.size() - 1; i >= 0; --i) { ++digits[i]; digits[i] %= 10; if (digits[i] != 0) return digits; diff --git a/solution/0000-0099/0066.Plus One/README_EN.md b/solution/0000-0099/0066.Plus One/README_EN.md index 996dca91404eb..e0e0a0c64a369 100644 --- a/solution/0000-0099/0066.Plus One/README_EN.md +++ b/solution/0000-0099/0066.Plus One/README_EN.md @@ -111,8 +111,7 @@ var plusOne = function (digits) { class Solution { public: vector plusOne(vector& digits) { - for (int i = digits.size() - 1; i >= 0; --i) - { + for (int i = digits.size() - 1; i >= 0; --i) { ++digits[i]; digits[i] %= 10; if (digits[i] != 0) return digits; diff --git a/solution/0000-0099/0066.Plus One/Solution.cpp b/solution/0000-0099/0066.Plus One/Solution.cpp index 3720cb5480cac..c94b457065320 100644 --- a/solution/0000-0099/0066.Plus One/Solution.cpp +++ b/solution/0000-0099/0066.Plus One/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: vector plusOne(vector& digits) { - for (int i = digits.size() - 1; i >= 0; --i) - { + for (int i = digits.size() - 1; i >= 0; --i) { ++digits[i]; digits[i] %= 10; if (digits[i] != 0) return digits; diff --git a/solution/0000-0099/0068.Text Justification/README.md b/solution/0000-0099/0068.Text Justification/README.md index 5d71335d15041..5ac1439dd7545 100644 --- a/solution/0000-0099/0068.Text Justification/README.md +++ b/solution/0000-0099/0068.Text Justification/README.md @@ -198,35 +198,29 @@ public: vector fullJustify(vector& words, int maxWidth) { int n = words.size(); vector result; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int begin = i; int wordLen = words[i].size(); - while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) - { + while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) { wordLen += words[++i].size() + 1; } int numberofWords = i - begin + 1; int space = 1; int extraSpace = 0; - if (numberofWords > 1 && i < n - 1) - { + if (numberofWords > 1 && i < n - 1) { int remaining = maxWidth - wordLen; space = remaining / (numberofWords - 1) + 1; extraSpace = remaining % (numberofWords - 1); } string line = words[begin]; - for (int j = 1; j < numberofWords; j++) - { + for (int j = 1; j < numberofWords; j++) { line.append(space, ' '); - if (j <= extraSpace) - { + if (j <= extraSpace) { line.push_back(' '); } line += words[begin + j]; } - if (line.size() < maxWidth) - { + if (line.size() < maxWidth) { line.append(maxWidth - line.size(), ' '); } result.emplace_back(line); diff --git a/solution/0000-0099/0068.Text Justification/README_EN.md b/solution/0000-0099/0068.Text Justification/README_EN.md index 528dda2dd54dc..2c7ab52ecca6d 100644 --- a/solution/0000-0099/0068.Text Justification/README_EN.md +++ b/solution/0000-0099/0068.Text Justification/README_EN.md @@ -186,35 +186,29 @@ public: vector fullJustify(vector& words, int maxWidth) { int n = words.size(); vector result; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int begin = i; int wordLen = words[i].size(); - while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) - { + while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) { wordLen += words[++i].size() + 1; } int numberofWords = i - begin + 1; int space = 1; int extraSpace = 0; - if (numberofWords > 1 && i < n - 1) - { + if (numberofWords > 1 && i < n - 1) { int remaining = maxWidth - wordLen; space = remaining / (numberofWords - 1) + 1; extraSpace = remaining % (numberofWords - 1); } string line = words[begin]; - for (int j = 1; j < numberofWords; j++) - { + for (int j = 1; j < numberofWords; j++) { line.append(space, ' '); - if (j <= extraSpace) - { + if (j <= extraSpace) { line.push_back(' '); } line += words[begin + j]; } - if (line.size() < maxWidth) - { + if (line.size() < maxWidth) { line.append(maxWidth - line.size(), ' '); } result.emplace_back(line); diff --git a/solution/0000-0099/0068.Text Justification/Solution.cpp b/solution/0000-0099/0068.Text Justification/Solution.cpp index f19ab8bcd7582..85962f5b3c288 100644 --- a/solution/0000-0099/0068.Text Justification/Solution.cpp +++ b/solution/0000-0099/0068.Text Justification/Solution.cpp @@ -3,35 +3,29 @@ class Solution { vector fullJustify(vector& words, int maxWidth) { int n = words.size(); vector result; - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { int begin = i; int wordLen = words[i].size(); - while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) - { + while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth) { wordLen += words[++i].size() + 1; } int numberofWords = i - begin + 1; int space = 1; int extraSpace = 0; - if (numberofWords > 1 && i < n - 1) - { + if (numberofWords > 1 && i < n - 1) { int remaining = maxWidth - wordLen; space = remaining / (numberofWords - 1) + 1; extraSpace = remaining % (numberofWords - 1); } string line = words[begin]; - for (int j = 1; j < numberofWords; j++) - { + for (int j = 1; j < numberofWords; j++) { line.append(space, ' '); - if (j <= extraSpace) - { + if (j <= extraSpace) { line.push_back(' '); } line += words[begin + j]; } - if (line.size() < maxWidth) - { + if (line.size() < maxWidth) { line.append(maxWidth - line.size(), ' '); } result.emplace_back(line); diff --git a/solution/0000-0099/0069.Sqrt(x)/README.md b/solution/0000-0099/0069.Sqrt(x)/README.md index 986995f3e6299..87fc844afc95d 100644 --- a/solution/0000-0099/0069.Sqrt(x)/README.md +++ b/solution/0000-0099/0069.Sqrt(x)/README.md @@ -92,13 +92,14 @@ class Solution { public: int mySqrt(int x) { long long left = 0, right = x; - while (left < right) - { + while (left < right) { long long mid = left + ((right - left + 1) >> 1); - if (mid <= x / mid) left = mid; - else right = mid - 1; + if (mid <= x / mid) + left = mid; + else + right = mid - 1; } - return (int) left; + return (int)left; } }; ``` diff --git a/solution/0000-0099/0069.Sqrt(x)/README_EN.md b/solution/0000-0099/0069.Sqrt(x)/README_EN.md index 1b07caa86de2e..decc33252cfc2 100644 --- a/solution/0000-0099/0069.Sqrt(x)/README_EN.md +++ b/solution/0000-0099/0069.Sqrt(x)/README_EN.md @@ -81,13 +81,14 @@ class Solution { public: int mySqrt(int x) { long long left = 0, right = x; - while (left < right) - { + while (left < right) { long long mid = left + ((right - left + 1) >> 1); - if (mid <= x / mid) left = mid; - else right = mid - 1; + if (mid <= x / mid) + left = mid; + else + right = mid - 1; } - return (int) left; + return (int)left; } }; ``` diff --git a/solution/0000-0099/0069.Sqrt(x)/Solution.cpp b/solution/0000-0099/0069.Sqrt(x)/Solution.cpp index a95563389590a..106f2218862a4 100644 --- a/solution/0000-0099/0069.Sqrt(x)/Solution.cpp +++ b/solution/0000-0099/0069.Sqrt(x)/Solution.cpp @@ -2,12 +2,13 @@ class Solution { public: int mySqrt(int x) { long long left = 0, right = x; - while (left < right) - { + while (left < right) { long long mid = left + ((right - left + 1) >> 1); - if (mid <= x / mid) left = mid; - else right = mid - 1; + if (mid <= x / mid) + left = mid; + else + right = mid - 1; } - return (int) left; + return (int)left; } }; \ No newline at end of file diff --git a/solution/0000-0099/0071.Simplify Path/README.md b/solution/0000-0099/0071.Simplify Path/README.md index 3dfd7d4a194a8..9a6972958e534 100644 --- a/solution/0000-0099/0071.Simplify Path/README.md +++ b/solution/0000-0099/0071.Simplify Path/README.md @@ -240,9 +240,10 @@ public: if (tmp == "..") { if (!stk.empty()) stk.pop_back(); - } else stk.push_back(tmp); + } else + stk.push_back(tmp); } - for (auto str: stk) + for (auto str : stk) res += "/" + str; return res.empty() ? "/" : res; } diff --git a/solution/0000-0099/0071.Simplify Path/README_EN.md b/solution/0000-0099/0071.Simplify Path/README_EN.md index a1e2a9050fd09..fc4816d9fe6a4 100644 --- a/solution/0000-0099/0071.Simplify Path/README_EN.md +++ b/solution/0000-0099/0071.Simplify Path/README_EN.md @@ -224,9 +224,10 @@ public: if (tmp == "..") { if (!stk.empty()) stk.pop_back(); - } else stk.push_back(tmp); + } else + stk.push_back(tmp); } - for (auto str: stk) + for (auto str : stk) res += "/" + str; return res.empty() ? "/" : res; } diff --git a/solution/0000-0099/0071.Simplify Path/Solution.cpp b/solution/0000-0099/0071.Simplify Path/Solution.cpp index ff588d90596f7..0facf87b484af 100644 --- a/solution/0000-0099/0071.Simplify Path/Solution.cpp +++ b/solution/0000-0099/0071.Simplify Path/Solution.cpp @@ -9,9 +9,10 @@ class Solution { if (tmp == "..") { if (!stk.empty()) stk.pop_back(); - } else stk.push_back(tmp); + } else + stk.push_back(tmp); } - for (auto str: stk) + for (auto str : stk) res += "/" + str; return res.empty() ? "/" : res; } diff --git a/solution/0000-0099/0076.Minimum Window Substring/README.md b/solution/0000-0099/0076.Minimum Window Substring/README.md index 85596baac5b74..ffa54a0da4df1 100644 --- a/solution/0000-0099/0076.Minimum Window Substring/README.md +++ b/solution/0000-0099/0076.Minimum Window Substring/README.md @@ -188,10 +188,10 @@ function minWindow(s: string, t: string): string { ```cpp class Solution { public: - string minWindow(string s, string t){ + string minWindow(string s, string t) { unordered_map m; int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size(); - for (auto c: t) + for (auto c : t) m[c]++; while (end < size) { diff --git a/solution/0000-0099/0076.Minimum Window Substring/README_EN.md b/solution/0000-0099/0076.Minimum Window Substring/README_EN.md index 7a477c98c1ac4..efef8a97acd18 100644 --- a/solution/0000-0099/0076.Minimum Window Substring/README_EN.md +++ b/solution/0000-0099/0076.Minimum Window Substring/README_EN.md @@ -176,10 +176,10 @@ function minWindow(s: string, t: string): string { ```cpp class Solution { public: - string minWindow(string s, string t){ + string minWindow(string s, string t) { unordered_map m; int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size(); - for (auto c: t) + for (auto c : t) m[c]++; while (end < size) { diff --git a/solution/0000-0099/0076.Minimum Window Substring/Solution.cpp b/solution/0000-0099/0076.Minimum Window Substring/Solution.cpp index fbac9948bf301..68c35c2fd144a 100644 --- a/solution/0000-0099/0076.Minimum Window Substring/Solution.cpp +++ b/solution/0000-0099/0076.Minimum Window Substring/Solution.cpp @@ -1,9 +1,9 @@ class Solution { public: - string minWindow(string s, string t){ + string minWindow(string s, string t) { unordered_map m; int begin = 0, end = 0, minlen = INT_MAX, minStart = 0, size = s.size(), counter = t.size(); - for (auto c: t) + for (auto c : t) m[c]++; while (end < size) { diff --git a/solution/0000-0099/0077.Combinations/README.md b/solution/0000-0099/0077.Combinations/README.md index 58e0df3df0ea3..3a3768ec2c72b 100644 --- a/solution/0000-0099/0077.Combinations/README.md +++ b/solution/0000-0099/0077.Combinations/README.md @@ -110,13 +110,11 @@ public: } void dfs(int i, int n, int k, vector t, vector>& res) { - if (t.size() == k) - { + if (t.size() == k) { res.push_back(t); return; } - for (int j = i; j <= n; ++j) - { + for (int j = i; j <= n; ++j) { t.push_back(j); dfs(j + 1, n, k, t, res); t.pop_back(); diff --git a/solution/0000-0099/0077.Combinations/README_EN.md b/solution/0000-0099/0077.Combinations/README_EN.md index d064ab0b713ee..4b628592230f9 100644 --- a/solution/0000-0099/0077.Combinations/README_EN.md +++ b/solution/0000-0099/0077.Combinations/README_EN.md @@ -102,13 +102,11 @@ public: } void dfs(int i, int n, int k, vector t, vector>& res) { - if (t.size() == k) - { + if (t.size() == k) { res.push_back(t); return; } - for (int j = i; j <= n; ++j) - { + for (int j = i; j <= n; ++j) { t.push_back(j); dfs(j + 1, n, k, t, res); t.pop_back(); diff --git a/solution/0000-0099/0077.Combinations/Solution.cpp b/solution/0000-0099/0077.Combinations/Solution.cpp index 234e38fa007c8..a01fbf6f7c01c 100644 --- a/solution/0000-0099/0077.Combinations/Solution.cpp +++ b/solution/0000-0099/0077.Combinations/Solution.cpp @@ -8,13 +8,11 @@ class Solution { } void dfs(int i, int n, int k, vector t, vector>& res) { - if (t.size() == k) - { + if (t.size() == k) { res.push_back(t); return; } - for (int j = i; j <= n; ++j) - { + for (int j = i; j <= n; ++j) { t.push_back(j); dfs(j + 1, n, k, t, res); t.pop_back(); diff --git a/solution/0000-0099/0078.Subsets/README.md b/solution/0000-0099/0078.Subsets/README.md index d148154126bbf..4c9d6029889b7 100644 --- a/solution/0000-0099/0078.Subsets/README.md +++ b/solution/0000-0099/0078.Subsets/README.md @@ -141,8 +141,7 @@ public: } void dfs(int u, vector& nums, vector& t, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { ans.push_back(t); return; } diff --git a/solution/0000-0099/0078.Subsets/README_EN.md b/solution/0000-0099/0078.Subsets/README_EN.md index e36e2c7553cef..3253985bbf4d8 100644 --- a/solution/0000-0099/0078.Subsets/README_EN.md +++ b/solution/0000-0099/0078.Subsets/README_EN.md @@ -129,8 +129,7 @@ public: } void dfs(int u, vector& nums, vector& t, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { ans.push_back(t); return; } diff --git a/solution/0000-0099/0078.Subsets/Solution.cpp b/solution/0000-0099/0078.Subsets/Solution.cpp index c75cc6bd69ad6..46e0677f2c149 100644 --- a/solution/0000-0099/0078.Subsets/Solution.cpp +++ b/solution/0000-0099/0078.Subsets/Solution.cpp @@ -8,8 +8,7 @@ class Solution { } void dfs(int u, vector& nums, vector& t, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { ans.push_back(t); return; } diff --git a/solution/0000-0099/0079.Word Search/README.md b/solution/0000-0099/0079.Word Search/README.md index 9d982ee010673..8e77b826757c1 100644 --- a/solution/0000-0099/0079.Word Search/README.md +++ b/solution/0000-0099/0079.Word Search/README.md @@ -199,8 +199,7 @@ public: char t = board[i][j]; board[i][j] = '0'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (dfs(x, y, cur + 1, m, n, board, word)) return true; } diff --git a/solution/0000-0099/0079.Word Search/README_EN.md b/solution/0000-0099/0079.Word Search/README_EN.md index 43266dae12119..376d9c1211a50 100644 --- a/solution/0000-0099/0079.Word Search/README_EN.md +++ b/solution/0000-0099/0079.Word Search/README_EN.md @@ -186,8 +186,7 @@ public: char t = board[i][j]; board[i][j] = '0'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (dfs(x, y, cur + 1, m, n, board, word)) return true; } diff --git a/solution/0000-0099/0079.Word Search/Solution.cpp b/solution/0000-0099/0079.Word Search/Solution.cpp index a370cea1ddd5b..ddfb14b7617e1 100644 --- a/solution/0000-0099/0079.Word Search/Solution.cpp +++ b/solution/0000-0099/0079.Word Search/Solution.cpp @@ -15,8 +15,7 @@ class Solution { char t = board[i][j]; board[i][j] = '0'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (dfs(x, y, cur + 1, m, n, board, word)) return true; } diff --git a/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp b/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp index 60baa6f98e0ae..cbc05c3cdd7a8 100644 --- a/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp +++ b/solution/0000-0099/0080.Remove Duplicates from Sorted Array II/Solution.cpp @@ -3,7 +3,7 @@ class Solution { int removeDuplicates(vector& nums) { int i = 0; for (int& num : nums) - if (i < 2 || num != nums[i - 2]) + if (i < 2 || num != nums[i - 2]) nums[i++] = num; return i; } diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md index f247f243de9a7..337dfb87d506c 100644 --- a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README.md @@ -123,12 +123,17 @@ public: int mid = (l + r) >> 1; if (nums[mid] == target) return true; if (nums[mid] < nums[r] || nums[mid] < nums[l]) { - if (target > nums[mid] && target <= nums[r]) l = mid + 1; - else r = mid - 1; + if (target > nums[mid] && target <= nums[r]) + l = mid + 1; + else + r = mid - 1; } else if (nums[mid] > nums[l] || nums[mid] > nums[r]) { - if (target < nums[mid] && target >= nums[l]) r = mid - 1; - else l = mid + 1; - } else r--; + if (target < nums[mid] && target >= nums[l]) + r = mid - 1; + else + l = mid + 1; + } else + r--; } return false; } diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md index f8d0f5ac7b71e..42a8f7b978488 100644 --- a/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/README_EN.md @@ -97,12 +97,17 @@ public: int mid = (l + r) >> 1; if (nums[mid] == target) return true; if (nums[mid] < nums[r] || nums[mid] < nums[l]) { - if (target > nums[mid] && target <= nums[r]) l = mid + 1; - else r = mid - 1; + if (target > nums[mid] && target <= nums[r]) + l = mid + 1; + else + r = mid - 1; } else if (nums[mid] > nums[l] || nums[mid] > nums[r]) { - if (target < nums[mid] && target >= nums[l]) r = mid - 1; - else l = mid + 1; - } else r--; + if (target < nums[mid] && target >= nums[l]) + r = mid - 1; + else + l = mid + 1; + } else + r--; } return false; } diff --git a/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.cpp b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.cpp index fd7b6af6930b2..eefc21a467c33 100644 --- a/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.cpp +++ b/solution/0000-0099/0081.Search in Rotated Sorted Array II/Solution.cpp @@ -6,12 +6,17 @@ class Solution { int mid = (l + r) >> 1; if (nums[mid] == target) return true; if (nums[mid] < nums[r] || nums[mid] < nums[l]) { - if (target > nums[mid] && target <= nums[r]) l = mid + 1; - else r = mid - 1; + if (target > nums[mid] && target <= nums[r]) + l = mid + 1; + else + r = mid - 1; } else if (nums[mid] > nums[l] || nums[mid] > nums[r]) { - if (target < nums[mid] && target >= nums[l]) r = mid - 1; - else l = mid + 1; - } else r--; + if (target < nums[mid] && target >= nums[l]) + r = mid - 1; + else + l = mid + 1; + } else + r--; } return false; } diff --git a/solution/0000-0099/0084.Largest Rectangle in Histogram/README.md b/solution/0000-0099/0084.Largest Rectangle in Histogram/README.md index c5aed47c671e4..5ed84fdb5e16b 100644 --- a/solution/0000-0099/0084.Largest Rectangle in Histogram/README.md +++ b/solution/0000-0099/0084.Largest Rectangle in Histogram/README.md @@ -143,10 +143,8 @@ public: stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git a/solution/0000-0099/0084.Largest Rectangle in Histogram/README_EN.md b/solution/0000-0099/0084.Largest Rectangle in Histogram/README_EN.md index c148794bbacc8..189adf8c14028 100644 --- a/solution/0000-0099/0084.Largest Rectangle in Histogram/README_EN.md +++ b/solution/0000-0099/0084.Largest Rectangle in Histogram/README_EN.md @@ -113,10 +113,8 @@ public: stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git a/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.cpp b/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.cpp index 57bbd61b4560d..783b516dce913 100644 --- a/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.cpp +++ b/solution/0000-0099/0084.Largest Rectangle in Histogram/Solution.cpp @@ -5,10 +5,8 @@ class Solution { stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git a/solution/0000-0099/0085.Maximal Rectangle/README.md b/solution/0000-0099/0085.Maximal Rectangle/README.md index e12d079a74be3..fddc24368771c 100644 --- a/solution/0000-0099/0085.Maximal Rectangle/README.md +++ b/solution/0000-0099/0085.Maximal Rectangle/README.md @@ -162,12 +162,12 @@ public: int n = matrix[0].size(); vector heights(n); int ans = 0; - for (auto& row : matrix) - { - for (int j = 0; j < n; ++j) - { - if (row[j] == '1') ++heights[j]; - else heights[j] = 0; + for (auto& row : matrix) { + for (int j = 0; j < n; ++j) { + if (row[j] == '1') + ++heights[j]; + else + heights[j] = 0; } ans = max(ans, largestRectangleArea(heights)); } @@ -179,10 +179,8 @@ public: stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git a/solution/0000-0099/0085.Maximal Rectangle/README_EN.md b/solution/0000-0099/0085.Maximal Rectangle/README_EN.md index 8b323e2cd3172..7652f6cf55926 100644 --- a/solution/0000-0099/0085.Maximal Rectangle/README_EN.md +++ b/solution/0000-0099/0085.Maximal Rectangle/README_EN.md @@ -132,12 +132,12 @@ public: int n = matrix[0].size(); vector heights(n); int ans = 0; - for (auto& row : matrix) - { - for (int j = 0; j < n; ++j) - { - if (row[j] == '1') ++heights[j]; - else heights[j] = 0; + for (auto& row : matrix) { + for (int j = 0; j < n; ++j) { + if (row[j] == '1') + ++heights[j]; + else + heights[j] = 0; } ans = max(ans, largestRectangleArea(heights)); } @@ -149,10 +149,8 @@ public: stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git a/solution/0000-0099/0085.Maximal Rectangle/Solution.cpp b/solution/0000-0099/0085.Maximal Rectangle/Solution.cpp index 96f00dd923b7a..94fd8b42801df 100644 --- a/solution/0000-0099/0085.Maximal Rectangle/Solution.cpp +++ b/solution/0000-0099/0085.Maximal Rectangle/Solution.cpp @@ -4,12 +4,12 @@ class Solution { int n = matrix[0].size(); vector heights(n); int ans = 0; - for (auto& row : matrix) - { - for (int j = 0; j < n; ++j) - { - if (row[j] == '1') ++heights[j]; - else heights[j] = 0; + for (auto& row : matrix) { + for (int j = 0; j < n; ++j) { + if (row[j] == '1') + ++heights[j]; + else + heights[j] = 0; } ans = max(ans, largestRectangleArea(heights)); } @@ -21,10 +21,8 @@ class Solution { stack stk; vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && heights[stk.top()] >= heights[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && heights[stk.top()] >= heights[i]) { right[stk.top()] = i; stk.pop(); } diff --git a/solution/0000-0099/0086.Partition List/README.md b/solution/0000-0099/0086.Partition List/README.md index c09ec691f6e1e..26e2b9056e86a 100644 --- a/solution/0000-0099/0086.Partition List/README.md +++ b/solution/0000-0099/0086.Partition List/README.md @@ -128,15 +128,11 @@ public: ListNode* d2 = new ListNode(); ListNode* t1 = d1; ListNode* t2 = d2; - while (head) - { - if (head->val < x) - { + while (head) { + if (head->val < x) { t1->next = head; t1 = t1->next; - } - else - { + } else { t2->next = head; t2 = t2->next; } diff --git a/solution/0000-0099/0086.Partition List/README_EN.md b/solution/0000-0099/0086.Partition List/README_EN.md index e7b20afb38936..41c5800391466 100644 --- a/solution/0000-0099/0086.Partition List/README_EN.md +++ b/solution/0000-0099/0086.Partition List/README_EN.md @@ -116,15 +116,11 @@ public: ListNode* d2 = new ListNode(); ListNode* t1 = d1; ListNode* t2 = d2; - while (head) - { - if (head->val < x) - { + while (head) { + if (head->val < x) { t1->next = head; t1 = t1->next; - } - else - { + } else { t2->next = head; t2 = t2->next; } diff --git a/solution/0000-0099/0086.Partition List/Solution.cpp b/solution/0000-0099/0086.Partition List/Solution.cpp index 7757157b96711..d04f17777dbe3 100644 --- a/solution/0000-0099/0086.Partition List/Solution.cpp +++ b/solution/0000-0099/0086.Partition List/Solution.cpp @@ -15,15 +15,11 @@ class Solution { ListNode* d2 = new ListNode(); ListNode* t1 = d1; ListNode* t2 = d2; - while (head) - { - if (head->val < x) - { + while (head) { + if (head->val < x) { t1->next = head; t1 = t1->next; - } - else - { + } else { t2->next = head; t2 = t2->next; } diff --git a/solution/0000-0099/0090.Subsets II/README.md b/solution/0000-0099/0090.Subsets II/README.md index f1b9f0af79f8d..632d684de96ea 100644 --- a/solution/0000-0099/0090.Subsets II/README.md +++ b/solution/0000-0099/0090.Subsets II/README.md @@ -113,8 +113,7 @@ public: void dfs(int u, vector& t, vector& nums, vector>& ans) { ans.push_back(t); - for (int i = u; i < nums.size(); ++i) - { + for (int i = u; i < nums.size(); ++i) { if (i != u && nums[i] == nums[i - 1]) continue; t.push_back(nums[i]); dfs(i + 1, t, nums, ans); diff --git a/solution/0000-0099/0090.Subsets II/README_EN.md b/solution/0000-0099/0090.Subsets II/README_EN.md index 67c821f68f242..9d95386301530 100644 --- a/solution/0000-0099/0090.Subsets II/README_EN.md +++ b/solution/0000-0099/0090.Subsets II/README_EN.md @@ -92,8 +92,7 @@ public: void dfs(int u, vector& t, vector& nums, vector>& ans) { ans.push_back(t); - for (int i = u; i < nums.size(); ++i) - { + for (int i = u; i < nums.size(); ++i) { if (i != u && nums[i] == nums[i - 1]) continue; t.push_back(nums[i]); dfs(i + 1, t, nums, ans); diff --git a/solution/0000-0099/0090.Subsets II/Solution.cpp b/solution/0000-0099/0090.Subsets II/Solution.cpp index 083144fd0cd6d..0e6912965a5ff 100644 --- a/solution/0000-0099/0090.Subsets II/Solution.cpp +++ b/solution/0000-0099/0090.Subsets II/Solution.cpp @@ -10,8 +10,7 @@ class Solution { void dfs(int u, vector& t, vector& nums, vector>& ans) { ans.push_back(t); - for (int i = u; i < nums.size(); ++i) - { + for (int i = u; i < nums.size(); ++i) { if (i != u && nums[i] == nums[i - 1]) continue; t.push_back(nums[i]); dfs(i + 1, t, nums, ans); diff --git a/solution/0000-0099/0092.Reverse Linked List II/README.md b/solution/0000-0099/0092.Reverse Linked List II/README.md index f22d84345ae22..9eafe05a864ea 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/README.md +++ b/solution/0000-0099/0092.Reverse Linked List II/README.md @@ -174,15 +174,15 @@ public: if (head == nullptr || head->next == nullptr || left == right) { return head; } - ListNode *dummy = new ListNode(0, head); - ListNode *pre = dummy; + ListNode* dummy = new ListNode(0, head); + ListNode* pre = dummy; for (int i = 0; i < left - 1; ++i) { pre = pre->next; } ListNode *p = pre, *q = pre->next; - ListNode *cur = q; + ListNode* cur = q; for (int i = 0; i < right - left + 1; ++i) { - ListNode *t = cur->next; + ListNode* t = cur->next; cur->next = pre; pre = cur; cur = t; diff --git a/solution/0000-0099/0092.Reverse Linked List II/README_EN.md b/solution/0000-0099/0092.Reverse Linked List II/README_EN.md index d6905ef8ed81c..43c22229dafad 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/README_EN.md +++ b/solution/0000-0099/0092.Reverse Linked List II/README_EN.md @@ -163,15 +163,15 @@ public: if (head == nullptr || head->next == nullptr || left == right) { return head; } - ListNode *dummy = new ListNode(0, head); - ListNode *pre = dummy; + ListNode* dummy = new ListNode(0, head); + ListNode* pre = dummy; for (int i = 0; i < left - 1; ++i) { pre = pre->next; } ListNode *p = pre, *q = pre->next; - ListNode *cur = q; + ListNode* cur = q; for (int i = 0; i < right - left + 1; ++i) { - ListNode *t = cur->next; + ListNode* t = cur->next; cur->next = pre; pre = cur; cur = t; diff --git a/solution/0000-0099/0092.Reverse Linked List II/Solution.cpp b/solution/0000-0099/0092.Reverse Linked List II/Solution.cpp index 7fa59c476cf74..92347a5a4000f 100644 --- a/solution/0000-0099/0092.Reverse Linked List II/Solution.cpp +++ b/solution/0000-0099/0092.Reverse Linked List II/Solution.cpp @@ -14,15 +14,15 @@ class Solution { if (head == nullptr || head->next == nullptr || left == right) { return head; } - ListNode *dummy = new ListNode(0, head); - ListNode *pre = dummy; + ListNode* dummy = new ListNode(0, head); + ListNode* pre = dummy; for (int i = 0; i < left - 1; ++i) { pre = pre->next; } ListNode *p = pre, *q = pre->next; - ListNode *cur = q; + ListNode* cur = q; for (int i = 0; i < right - left + 1; ++i) { - ListNode *t = cur->next; + ListNode* t = cur->next; cur->next = pre; pre = cur; cur = t; diff --git a/solution/0000-0099/0093.Restore IP Addresses/README.md b/solution/0000-0099/0093.Restore IP Addresses/README.md index 74a56d4b7cbf7..641d844084657 100644 --- a/solution/0000-0099/0093.Restore IP Addresses/README.md +++ b/solution/0000-0099/0093.Restore IP Addresses/README.md @@ -144,10 +144,8 @@ public: } void dfs(string s, vector& t, vector& ans) { - if (t.size() == 4) - { - if (s == "") - { + if (t.size() == 4) { + if (s == "") { string p = ""; for (auto e : t) p += e + "."; p.pop_back(); @@ -155,11 +153,9 @@ public: } return; } - for (int i = 1; i < min(4, (int) s.size() + 1); ++i) - { + for (int i = 1; i < min(4, (int)s.size() + 1); ++i) { string c = s.substr(0, i); - if (check(c)) - { + if (check(c)) { t.push_back(c); dfs(s.substr(i, s.size() - i), t, ans); t.pop_back(); diff --git a/solution/0000-0099/0093.Restore IP Addresses/README_EN.md b/solution/0000-0099/0093.Restore IP Addresses/README_EN.md index 69aedd04ebf45..ae8bbc3443476 100644 --- a/solution/0000-0099/0093.Restore IP Addresses/README_EN.md +++ b/solution/0000-0099/0093.Restore IP Addresses/README_EN.md @@ -134,10 +134,8 @@ public: } void dfs(string s, vector& t, vector& ans) { - if (t.size() == 4) - { - if (s == "") - { + if (t.size() == 4) { + if (s == "") { string p = ""; for (auto e : t) p += e + "."; p.pop_back(); @@ -145,11 +143,9 @@ public: } return; } - for (int i = 1; i < min(4, (int) s.size() + 1); ++i) - { + for (int i = 1; i < min(4, (int)s.size() + 1); ++i) { string c = s.substr(0, i); - if (check(c)) - { + if (check(c)) { t.push_back(c); dfs(s.substr(i, s.size() - i), t, ans); t.pop_back(); diff --git a/solution/0000-0099/0093.Restore IP Addresses/Solution.cpp b/solution/0000-0099/0093.Restore IP Addresses/Solution.cpp index 3746aa1f91de7..3675f39f08d54 100644 --- a/solution/0000-0099/0093.Restore IP Addresses/Solution.cpp +++ b/solution/0000-0099/0093.Restore IP Addresses/Solution.cpp @@ -8,10 +8,8 @@ class Solution { } void dfs(string s, vector& t, vector& ans) { - if (t.size() == 4) - { - if (s == "") - { + if (t.size() == 4) { + if (s == "") { string p = ""; for (auto e : t) p += e + "."; p.pop_back(); @@ -19,11 +17,9 @@ class Solution { } return; } - for (int i = 1; i < min(4, (int) s.size() + 1); ++i) - { + for (int i = 1; i < min(4, (int)s.size() + 1); ++i) { string c = s.substr(0, i); - if (check(c)) - { + if (check(c)) { t.push_back(c); dfs(s.substr(i, s.size() - i), t, ans); t.pop_back(); diff --git a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md index 5b1ec669d80c3..54421af1c0293 100644 --- a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md +++ b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README.md @@ -400,27 +400,19 @@ class Solution { public: vector inorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->left) - { + while (root) { + if (!root->left) { ans.push_back(root->val); root = root->right; - } - else - { + } else { TreeNode* prev = root->left; - while (prev->right && prev->right != root) - { + while (prev->right && prev->right != root) { prev = prev->right; } - if (!prev->right) - { + if (!prev->right) { prev->right = root; root = root->left; - } - else - { + } else { ans.push_back(root->val); prev->right = nullptr; root = root->right; diff --git a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md index 931e26b51e933..f75f2b656e97f 100644 --- a/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md +++ b/solution/0000-0099/0094.Binary Tree Inorder Traversal/README_EN.md @@ -370,27 +370,19 @@ class Solution { public: vector inorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->left) - { + while (root) { + if (!root->left) { ans.push_back(root->val); root = root->right; - } - else - { + } else { TreeNode* prev = root->left; - while (prev->right && prev->right != root) - { + while (prev->right && prev->right != root) { prev = prev->right; } - if (!prev->right) - { + if (!prev->right) { prev->right = root; root = root->left; - } - else - { + } else { ans.push_back(root->val); prev->right = nullptr; root = root->right; diff --git a/solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.cpp b/solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.cpp index 43102c413c527..07e0991586f77 100644 --- a/solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.cpp +++ b/solution/0000-0099/0094.Binary Tree Inorder Traversal/Solution.cpp @@ -13,27 +13,19 @@ class Solution { public: vector inorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->left) - { + while (root) { + if (!root->left) { ans.push_back(root->val); root = root->right; - } - else - { + } else { TreeNode* prev = root->left; - while (prev->right && prev->right != root) - { + while (prev->right && prev->right != root) { prev = prev->right; } - if (!prev->right) - { + if (!prev->right) { prev->right = root; root = root->left; - } - else - { + } else { ans.push_back(root->val); prev->right = nullptr; root = root->right; diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/README.md b/solution/0000-0099/0095.Unique Binary Search Trees II/README.md index 817d59b7d693d..096a182eafbe2 100644 --- a/solution/0000-0099/0095.Unique Binary Search Trees II/README.md +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/README.md @@ -182,20 +182,14 @@ public: vector gen(int left, int right) { vector ans; - if (left > right) - { + if (left > right) { ans.push_back(nullptr); - } - else - { - for (int i = left; i <= right; ++i) - { + } else { + for (int i = left; i <= right; ++i) { auto leftTrees = gen(left, i - 1); auto rightTrees = gen(i + 1, right); - for (auto& l : leftTrees) - { - for (auto& r : rightTrees) - { + for (auto& l : leftTrees) { + for (auto& r : rightTrees) { TreeNode* node = new TreeNode(i, l, r); ans.push_back(node); } diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md b/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md index 2573a073ceb0e..e8800e1194c15 100644 --- a/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/README_EN.md @@ -168,20 +168,14 @@ public: vector gen(int left, int right) { vector ans; - if (left > right) - { + if (left > right) { ans.push_back(nullptr); - } - else - { - for (int i = left; i <= right; ++i) - { + } else { + for (int i = left; i <= right; ++i) { auto leftTrees = gen(left, i - 1); auto rightTrees = gen(i + 1, right); - for (auto& l : leftTrees) - { - for (auto& r : rightTrees) - { + for (auto& l : leftTrees) { + for (auto& r : rightTrees) { TreeNode* node = new TreeNode(i, l, r); ans.push_back(node); } diff --git a/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.cpp b/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.cpp index 48c881555e450..a88f9250b8106 100644 --- a/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.cpp +++ b/solution/0000-0099/0095.Unique Binary Search Trees II/Solution.cpp @@ -17,20 +17,14 @@ class Solution { vector gen(int left, int right) { vector ans; - if (left > right) - { + if (left > right) { ans.push_back(nullptr); - } - else - { - for (int i = left; i <= right; ++i) - { + } else { + for (int i = left; i <= right; ++i) { auto leftTrees = gen(left, i - 1); auto rightTrees = gen(i + 1, right); - for (auto& l : leftTrees) - { - for (auto& r : rightTrees) - { + for (auto& l : leftTrees) { + for (auto& r : rightTrees) { TreeNode* node = new TreeNode(i, l, r); ans.push_back(node); } diff --git a/solution/0000-0099/0097.Interleaving String/README.md b/solution/0000-0099/0097.Interleaving String/README.md index 84471967e9273..3fdd6c54b80e0 100644 --- a/solution/0000-0099/0097.Interleaving String/README.md +++ b/solution/0000-0099/0097.Interleaving String/README.md @@ -207,8 +207,7 @@ public: auto it = memo.find(i * 100 + j); if (it != memo.end()) return it->second; - bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || - (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); + bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); memo[i * 100 + j] = ret; return ret; diff --git a/solution/0000-0099/0097.Interleaving String/README_EN.md b/solution/0000-0099/0097.Interleaving String/README_EN.md index 4359faa387873..31c95c8641ef2 100644 --- a/solution/0000-0099/0097.Interleaving String/README_EN.md +++ b/solution/0000-0099/0097.Interleaving String/README_EN.md @@ -199,8 +199,7 @@ public: auto it = memo.find(i * 100 + j); if (it != memo.end()) return it->second; - bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || - (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); + bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); memo[i * 100 + j] = ret; return ret; diff --git a/solution/0000-0099/0097.Interleaving String/Solution.cpp b/solution/0000-0099/0097.Interleaving String/Solution.cpp index 1a0aed5cd4ddc..31f09f9279ac9 100644 --- a/solution/0000-0099/0097.Interleaving String/Solution.cpp +++ b/solution/0000-0099/0097.Interleaving String/Solution.cpp @@ -12,8 +12,7 @@ class Solution { auto it = memo.find(i * 100 + j); if (it != memo.end()) return it->second; - bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || - (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); + bool ret = (i < m && s1[i] == s3[i + j] && dfs(i + 1, j)) || (j < n && s2[j] == s3[i + j] && dfs(i, j + 1)); memo[i * 100 + j] = ret; return ret; diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/README.md b/solution/0000-0099/0099.Recover Binary Search Tree/README.md index ff892e02af72f..79806ab8573dd 100644 --- a/solution/0000-0099/0099.Recover Binary Search Tree/README.md +++ b/solution/0000-0099/0099.Recover Binary Search Tree/README.md @@ -156,8 +156,7 @@ public: void dfs(TreeNode* root) { if (!root) return; dfs(root->left); - if (prev) - { + if (prev) { if (!first && prev->val > root->val) first = prev; if (first && prev->val > root->val) second = root; } diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md b/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md index beb73be0a6ec4..7514b47ce10c1 100644 --- a/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md +++ b/solution/0000-0099/0099.Recover Binary Search Tree/README_EN.md @@ -146,8 +146,7 @@ public: void dfs(TreeNode* root) { if (!root) return; dfs(root->left); - if (prev) - { + if (prev) { if (!first && prev->val > root->val) first = prev; if (first && prev->val > root->val) second = root; } diff --git a/solution/0000-0099/0099.Recover Binary Search Tree/Solution.cpp b/solution/0000-0099/0099.Recover Binary Search Tree/Solution.cpp index da6be9ecda0f8..2d621fc2eee6a 100644 --- a/solution/0000-0099/0099.Recover Binary Search Tree/Solution.cpp +++ b/solution/0000-0099/0099.Recover Binary Search Tree/Solution.cpp @@ -23,8 +23,7 @@ class Solution { void dfs(TreeNode* root) { if (!root) return; dfs(root->left); - if (prev) - { + if (prev) { if (!first && prev->val > root->val) first = prev; if (first && prev->val > root->val) second = root; } diff --git a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md index c97d7fe9e6b63..0fafe98626837 100644 --- a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md +++ b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README.md @@ -144,12 +144,10 @@ public: vector> levelOrder(TreeNode* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; - for (int n = q.size(); n; --n) - { + for (int n = q.size(); n; --n) { auto node = q.front(); q.pop(); t.push_back(node->val); diff --git a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md index 8767f3f4f0b0f..c4acee51e06a2 100644 --- a/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md +++ b/solution/0100-0199/0102.Binary Tree Level Order Traversal/README_EN.md @@ -134,12 +134,10 @@ public: vector> levelOrder(TreeNode* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; - for (int n = q.size(); n; --n) - { + for (int n = q.size(); n; --n) { auto node = q.front(); q.pop(); t.push_back(node->val); diff --git a/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.cpp b/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.cpp index e1f0e005ab203..7326d8d746983 100644 --- a/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.cpp +++ b/solution/0100-0199/0102.Binary Tree Level Order Traversal/Solution.cpp @@ -14,12 +14,10 @@ class Solution { vector> levelOrder(TreeNode* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; - for (int n = q.size(); n; --n) - { + for (int n = q.size(); n; --n) { auto node = q.front(); q.pop(); t.push_back(node->val); diff --git a/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README.md b/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README.md index 4ccb7462aee74..b64800bf3b7f0 100644 --- a/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README.md +++ b/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README.md @@ -150,14 +150,12 @@ class Solution { public: vector> zigzagLevelOrder(TreeNode* root) { if (!root) return {}; - queue q{{root}}; + queue q {{root}}; vector> ans; bool left = false; - while (!q.empty()) - { + while (!q.empty()) { vector t; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { auto node = q.front(); q.pop(); t.push_back(node->val); diff --git a/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README_EN.md b/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README_EN.md index f3e5f289eba2e..f9d74d7fb57ae 100644 --- a/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README_EN.md +++ b/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/README_EN.md @@ -140,14 +140,12 @@ class Solution { public: vector> zigzagLevelOrder(TreeNode* root) { if (!root) return {}; - queue q{{root}}; + queue q {{root}}; vector> ans; bool left = false; - while (!q.empty()) - { + while (!q.empty()) { vector t; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { auto node = q.front(); q.pop(); t.push_back(node->val); diff --git a/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.cpp b/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.cpp index c38b99f9686ed..eae856aebc52d 100644 --- a/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.cpp +++ b/solution/0100-0199/0103.Binary Tree Zigzag Level Order Traversal/Solution.cpp @@ -13,14 +13,12 @@ class Solution { public: vector> zigzagLevelOrder(TreeNode* root) { if (!root) return {}; - queue q{{root}}; + queue q {{root}}; vector> ans; bool left = false; - while (!q.empty()) - { + while (!q.empty()) { vector t; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { auto node = q.front(); q.pop(); t.push_back(node->val); diff --git a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md index ab06604d930b2..6b31118bea39d 100644 --- a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md +++ b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README.md @@ -145,9 +145,8 @@ public: vector> levelOrderBottom(TreeNode* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; for (int i = q.size(); i > 0; --i) { TreeNode* node = q.front(); diff --git a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md index f7ce86a124042..20e4008eb820b 100644 --- a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md +++ b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/README_EN.md @@ -133,9 +133,8 @@ public: vector> levelOrderBottom(TreeNode* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; for (int i = q.size(); i > 0; --i) { TreeNode* node = q.front(); diff --git a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.cpp b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.cpp index e201b01fcc49c..81d532743a516 100644 --- a/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.cpp +++ b/solution/0100-0199/0107.Binary Tree Level Order Traversal II/Solution.cpp @@ -14,9 +14,8 @@ class Solution { vector> levelOrderBottom(TreeNode* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; for (int i = q.size(); i > 0; --i) { TreeNode* node = q.front(); diff --git a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md index e08af8c999a4b..925d99db91dba 100644 --- a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md +++ b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README.md @@ -121,16 +121,16 @@ class Solution { */ class Solution { public: - TreeNode *sortedArrayToBST(vector &nums) { + TreeNode* sortedArrayToBST(vector& nums) { return buildBST(nums, 0, nums.size() - 1); } private: - TreeNode *buildBST(vector &nums, int start, int end) { + TreeNode* buildBST(vector& nums, int start, int end) { if (start > end) return nullptr; int mid = start + end >> 1; - TreeNode *root = new TreeNode(nums[mid]); + TreeNode* root = new TreeNode(nums[mid]); root->left = buildBST(nums, start, mid - 1); root->right = buildBST(nums, mid + 1, end); return root; diff --git a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md index 5fd6452a2b92d..20d93a5ab8b10 100644 --- a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md +++ b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/README_EN.md @@ -111,16 +111,16 @@ class Solution { */ class Solution { public: - TreeNode *sortedArrayToBST(vector &nums) { + TreeNode* sortedArrayToBST(vector& nums) { return buildBST(nums, 0, nums.size() - 1); } private: - TreeNode *buildBST(vector &nums, int start, int end) { + TreeNode* buildBST(vector& nums, int start, int end) { if (start > end) return nullptr; int mid = start + end >> 1; - TreeNode *root = new TreeNode(nums[mid]); + TreeNode* root = new TreeNode(nums[mid]); root->left = buildBST(nums, start, mid - 1); root->right = buildBST(nums, mid + 1, end); return root; diff --git a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.cpp b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.cpp index 2b783289d4bb6..40491e13941d1 100644 --- a/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.cpp +++ b/solution/0100-0199/0108.Convert Sorted Array to Binary Search Tree/Solution.cpp @@ -11,16 +11,16 @@ */ class Solution { public: - TreeNode *sortedArrayToBST(vector &nums) { + TreeNode* sortedArrayToBST(vector& nums) { return buildBST(nums, 0, nums.size() - 1); } private: - TreeNode *buildBST(vector &nums, int start, int end) { + TreeNode* buildBST(vector& nums, int start, int end) { if (start > end) return nullptr; int mid = start + end >> 1; - TreeNode *root = new TreeNode(nums[mid]); + TreeNode* root = new TreeNode(nums[mid]); root->left = buildBST(nums, start, mid - 1); root->right = buildBST(nums, mid + 1, end); return root; diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md index 132de1eb2774c..769b61d7ace52 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README.md @@ -167,7 +167,7 @@ private: return nullptr; } int mid = (start + end) / 2; - TreeNode *root = new TreeNode(nums[mid]); + TreeNode* root = new TreeNode(nums[mid]); root->left = buildBST(nums, start, mid - 1); root->right = buildBST(nums, mid + 1, end); return root; diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md index 9c9bbf804255c..03e3260612399 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/README_EN.md @@ -155,7 +155,7 @@ private: return nullptr; } int mid = (start + end) / 2; - TreeNode *root = new TreeNode(nums[mid]); + TreeNode* root = new TreeNode(nums[mid]); root->left = buildBST(nums, start, mid - 1); root->right = buildBST(nums, mid + 1, end); return root; diff --git a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp index d1ee823b1f1e9..ee5cfcc48ece5 100644 --- a/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp +++ b/solution/0100-0199/0109.Convert Sorted List to Binary Search Tree/Solution.cpp @@ -35,7 +35,7 @@ class Solution { return nullptr; } int mid = (start + end) / 2; - TreeNode *root = new TreeNode(nums[mid]); + TreeNode* root = new TreeNode(nums[mid]); root->left = buildBST(nums, start, mid - 1); root->right = buildBST(nums, mid + 1, end); return root; diff --git a/solution/0100-0199/0112.Path Sum/README.md b/solution/0100-0199/0112.Path Sum/README.md index 7853075f20371..1ba2627ba6f72 100644 --- a/solution/0100-0199/0112.Path Sum/README.md +++ b/solution/0100-0199/0112.Path Sum/README.md @@ -113,11 +113,11 @@ class Solution { public: bool hasPathSum(TreeNode* root, int sum) { - if(root == NULL)return false; - if(root->right == NULL && root->left == NULL && sum == root->val)return true; + if (root == NULL) return false; + if (root->right == NULL && root->left == NULL && sum == root->val) return true; - bool leftTrue = hasPathSum(root->left,sum - root->val); - bool rightTrue = hasPathSum(root->right,sum - root->val); + bool leftTrue = hasPathSum(root->left, sum - root->val); + bool rightTrue = hasPathSum(root->right, sum - root->val); return (leftTrue || rightTrue); } diff --git a/solution/0100-0199/0112.Path Sum/README_EN.md b/solution/0100-0199/0112.Path Sum/README_EN.md index c12819322c42d..16fdfc7ba9cc6 100644 --- a/solution/0100-0199/0112.Path Sum/README_EN.md +++ b/solution/0100-0199/0112.Path Sum/README_EN.md @@ -102,11 +102,11 @@ class Solution { public: bool hasPathSum(TreeNode* root, int sum) { - if(root == NULL)return false; - if(root->right == NULL && root->left == NULL && sum == root->val)return true; + if (root == NULL) return false; + if (root->right == NULL && root->left == NULL && sum == root->val) return true; - bool leftTrue = hasPathSum(root->left,sum - root->val); - bool rightTrue = hasPathSum(root->right,sum - root->val); + bool leftTrue = hasPathSum(root->left, sum - root->val); + bool rightTrue = hasPathSum(root->right, sum - root->val); return (leftTrue || rightTrue); } diff --git a/solution/0100-0199/0112.Path Sum/Solution.cpp b/solution/0100-0199/0112.Path Sum/Solution.cpp index 642a371330813..c9704fa057c8e 100644 --- a/solution/0100-0199/0112.Path Sum/Solution.cpp +++ b/solution/0100-0199/0112.Path Sum/Solution.cpp @@ -1,13 +1,13 @@ class Solution { public: bool hasPathSum(TreeNode* root, int sum) { - - if(root == NULL)return false; - if(root->right == NULL && root->left == NULL && sum == root->val)return true; - - bool leftTrue = hasPathSum(root->left,sum - root->val); - bool rightTrue = hasPathSum(root->right,sum - root->val); - + + if (root == NULL) return false; + if (root->right == NULL && root->left == NULL && sum == root->val) return true; + + bool leftTrue = hasPathSum(root->left, sum - root->val); + bool rightTrue = hasPathSum(root->right, sum - root->val); + return (leftTrue || rightTrue); } }; \ No newline at end of file diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md index 0d85ac6020817..b344cb8c1657d 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README.md @@ -180,7 +180,7 @@ public: void flatten(TreeNode* root) { while (root) { if (root->left) { - TreeNode *pre = root->left; + TreeNode* pre = root->left; while (pre->right) { pre = pre->right; } diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md index 371c06da7890d..64d360ccfc7b5 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/README_EN.md @@ -164,7 +164,7 @@ public: void flatten(TreeNode* root) { while (root) { if (root->left) { - TreeNode *pre = root->left; + TreeNode* pre = root->left; while (pre->right) { pre = pre->right; } diff --git a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.cpp b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.cpp index 1a311b5b42a6c..bf23a74a22baf 100644 --- a/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.cpp +++ b/solution/0100-0199/0114.Flatten Binary Tree to Linked List/Solution.cpp @@ -14,7 +14,7 @@ class Solution { void flatten(TreeNode* root) { while (root) { if (root->left) { - TreeNode *pre = root->left; + TreeNode* pre = root->left; while (pre->right) { pre = pre->right; } diff --git a/solution/0100-0199/0118.Pascal's Triangle/README.md b/solution/0100-0199/0118.Pascal's Triangle/README.md index 6aeae161e4fec..bae05cf8a2ccb 100644 --- a/solution/0100-0199/0118.Pascal's Triangle/README.md +++ b/solution/0100-0199/0118.Pascal's Triangle/README.md @@ -106,8 +106,7 @@ class Solution { public: vector> generate(int numRows) { vector> ans; - for (int i = 0; i < numRows; ++i) - { + for (int i = 0; i < numRows; ++i) { vector t(i + 1, 1); for (int j = 1; j < i; ++j) t[j] = ans[i - 1][j] + ans[i - 1][j - 1]; ans.push_back(t); diff --git a/solution/0100-0199/0118.Pascal's Triangle/README_EN.md b/solution/0100-0199/0118.Pascal's Triangle/README_EN.md index 6ef7c5bd71a93..a1b670e71b3dd 100644 --- a/solution/0100-0199/0118.Pascal's Triangle/README_EN.md +++ b/solution/0100-0199/0118.Pascal's Triangle/README_EN.md @@ -85,8 +85,7 @@ class Solution { public: vector> generate(int numRows) { vector> ans; - for (int i = 0; i < numRows; ++i) - { + for (int i = 0; i < numRows; ++i) { vector t(i + 1, 1); for (int j = 1; j < i; ++j) t[j] = ans[i - 1][j] + ans[i - 1][j - 1]; ans.push_back(t); diff --git a/solution/0100-0199/0118.Pascal's Triangle/Solution.cpp b/solution/0100-0199/0118.Pascal's Triangle/Solution.cpp index ed4d55f64a43f..7b1589450c12b 100644 --- a/solution/0100-0199/0118.Pascal's Triangle/Solution.cpp +++ b/solution/0100-0199/0118.Pascal's Triangle/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: vector> generate(int numRows) { vector> ans; - for (int i = 0; i < numRows; ++i) - { + for (int i = 0; i < numRows; ++i) { vector t(i + 1, 1); for (int j = 1; j < i; ++j) t[j] = ans[i - 1][j] + ans[i - 1][j - 1]; ans.push_back(t); diff --git a/solution/0100-0199/0125.Valid Palindrome/README.md b/solution/0100-0199/0125.Valid Palindrome/README.md index 600108c28a928..4845729aced50 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README.md +++ b/solution/0100-0199/0125.Valid Palindrome/README.md @@ -97,9 +97,12 @@ public: bool isPalindrome(string s) { int i = 0, j = s.size() - 1; while (i < j) { - if (!isAlphaNum(s[i])) ++i; - else if (!isAlphaNum(s[j])) --j; - else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32) return false; + if (!isAlphaNum(s[i])) + ++i; + else if (!isAlphaNum(s[j])) + --j; + else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32) + return false; else { ++i; --j; @@ -109,7 +112,7 @@ public: } private: - bool isAlphaNum(char &ch) { + bool isAlphaNum(char& ch) { if (ch >= 'a' && ch <= 'z') return true; if (ch >= 'A' && ch <= 'Z') return true; if (ch >= '0' && ch <= '9') return true; diff --git a/solution/0100-0199/0125.Valid Palindrome/README_EN.md b/solution/0100-0199/0125.Valid Palindrome/README_EN.md index 517371c1ee4a0..ce55bcdd5d839 100644 --- a/solution/0100-0199/0125.Valid Palindrome/README_EN.md +++ b/solution/0100-0199/0125.Valid Palindrome/README_EN.md @@ -96,9 +96,12 @@ public: bool isPalindrome(string s) { int i = 0, j = s.size() - 1; while (i < j) { - if (!isAlphaNum(s[i])) ++i; - else if (!isAlphaNum(s[j])) --j; - else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32) return false; + if (!isAlphaNum(s[i])) + ++i; + else if (!isAlphaNum(s[j])) + --j; + else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32) + return false; else { ++i; --j; @@ -108,7 +111,7 @@ public: } private: - bool isAlphaNum(char &ch) { + bool isAlphaNum(char& ch) { if (ch >= 'a' && ch <= 'z') return true; if (ch >= 'A' && ch <= 'Z') return true; if (ch >= '0' && ch <= '9') return true; diff --git a/solution/0100-0199/0125.Valid Palindrome/Solution.cpp b/solution/0100-0199/0125.Valid Palindrome/Solution.cpp index 55d9ee8dd7d07..242b625b1c58b 100644 --- a/solution/0100-0199/0125.Valid Palindrome/Solution.cpp +++ b/solution/0100-0199/0125.Valid Palindrome/Solution.cpp @@ -3,9 +3,12 @@ class Solution { bool isPalindrome(string s) { int i = 0, j = s.size() - 1; while (i < j) { - if (!isAlphaNum(s[i])) ++i; - else if (!isAlphaNum(s[j])) --j; - else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32) return false; + if (!isAlphaNum(s[i])) + ++i; + else if (!isAlphaNum(s[j])) + --j; + else if ((s[i] + 32 - 'a') % 32 != (s[j] + 32 - 'a') % 32) + return false; else { ++i; --j; @@ -15,7 +18,7 @@ class Solution { } private: - bool isAlphaNum(char &ch) { + bool isAlphaNum(char& ch) { if (ch >= 'a' && ch <= 'z') return true; if (ch >= 'A' && ch <= 'Z') return true; if (ch >= '0' && ch <= '9') return true; diff --git a/solution/0100-0199/0127.Word Ladder/README.md b/solution/0100-0199/0127.Word Ladder/README.md index fba6203d48146..4388ba7bf96ab 100644 --- a/solution/0100-0199/0127.Word Ladder/README.md +++ b/solution/0100-0199/0127.Word Ladder/README.md @@ -296,14 +296,13 @@ public: int ladderLength(string beginWord, string endWord, vector& wordList) { unordered_set words(wordList.begin(), wordList.end()); if (!words.count(endWord)) return 0; - queue q1{{beginWord}}; - queue q2{{endWord}}; + queue q1 {{beginWord}}; + queue q2 {{endWord}}; unordered_map m1; unordered_map m2; m1[beginWord] = 0; m2[endWord] = 0; - while (!q1.empty() && !q2.empty()) - { + while (!q1.empty() && !q2.empty()) { int t = q1.size() <= q2.size() ? extend(m1, m2, q1, words) : extend(m2, m1, q2, words); if (t != -1) return t + 1; } @@ -311,16 +310,13 @@ public: } int extend(unordered_map& m1, unordered_map& m2, queue& q, unordered_set& words) { - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { string s = q.front(); int step = m1[s]; q.pop(); - for (int j = 0; j < s.size(); ++j) - { + for (int j = 0; j < s.size(); ++j) { char ch = s[j]; - for (char k = 'a'; k <= 'z'; ++k) - { + for (char k = 'a'; k <= 'z'; ++k) { s[j] = k; if (!words.count(s) || m1.count(s)) continue; if (m2.count(s)) return step + 1 + m2[s]; diff --git a/solution/0100-0199/0127.Word Ladder/README_EN.md b/solution/0100-0199/0127.Word Ladder/README_EN.md index f963a3fb511ee..53f4072c6bf5d 100644 --- a/solution/0100-0199/0127.Word Ladder/README_EN.md +++ b/solution/0100-0199/0127.Word Ladder/README_EN.md @@ -245,14 +245,13 @@ public: int ladderLength(string beginWord, string endWord, vector& wordList) { unordered_set words(wordList.begin(), wordList.end()); if (!words.count(endWord)) return 0; - queue q1{{beginWord}}; - queue q2{{endWord}}; + queue q1 {{beginWord}}; + queue q2 {{endWord}}; unordered_map m1; unordered_map m2; m1[beginWord] = 0; m2[endWord] = 0; - while (!q1.empty() && !q2.empty()) - { + while (!q1.empty() && !q2.empty()) { int t = q1.size() <= q2.size() ? extend(m1, m2, q1, words) : extend(m2, m1, q2, words); if (t != -1) return t + 1; } @@ -260,16 +259,13 @@ public: } int extend(unordered_map& m1, unordered_map& m2, queue& q, unordered_set& words) { - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { string s = q.front(); int step = m1[s]; q.pop(); - for (int j = 0; j < s.size(); ++j) - { + for (int j = 0; j < s.size(); ++j) { char ch = s[j]; - for (char k = 'a'; k <= 'z'; ++k) - { + for (char k = 'a'; k <= 'z'; ++k) { s[j] = k; if (!words.count(s) || m1.count(s)) continue; if (m2.count(s)) return step + 1 + m2[s]; diff --git a/solution/0100-0199/0127.Word Ladder/Solution.cpp b/solution/0100-0199/0127.Word Ladder/Solution.cpp index 9768be15af7b6..843ce9027c772 100644 --- a/solution/0100-0199/0127.Word Ladder/Solution.cpp +++ b/solution/0100-0199/0127.Word Ladder/Solution.cpp @@ -3,14 +3,13 @@ class Solution { int ladderLength(string beginWord, string endWord, vector& wordList) { unordered_set words(wordList.begin(), wordList.end()); if (!words.count(endWord)) return 0; - queue q1{{beginWord}}; - queue q2{{endWord}}; + queue q1 {{beginWord}}; + queue q2 {{endWord}}; unordered_map m1; unordered_map m2; m1[beginWord] = 0; m2[endWord] = 0; - while (!q1.empty() && !q2.empty()) - { + while (!q1.empty() && !q2.empty()) { int t = q1.size() <= q2.size() ? extend(m1, m2, q1, words) : extend(m2, m1, q2, words); if (t != -1) return t + 1; } @@ -18,16 +17,13 @@ class Solution { } int extend(unordered_map& m1, unordered_map& m2, queue& q, unordered_set& words) { - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { string s = q.front(); int step = m1[s]; q.pop(); - for (int j = 0; j < s.size(); ++j) - { + for (int j = 0; j < s.size(); ++j) { char ch = s[j]; - for (char k = 'a'; k <= 'z'; ++k) - { + for (char k = 'a'; k <= 'z'; ++k) { s[j] = k; if (!words.count(s) || m1.count(s)) continue; if (m2.count(s)) return step + 1 + m2[s]; diff --git a/solution/0100-0199/0128.Longest Consecutive Sequence/README.md b/solution/0100-0199/0128.Longest Consecutive Sequence/README.md index f6ae94d93fb1c..e4adfdd77226b 100644 --- a/solution/0100-0199/0128.Longest Consecutive Sequence/README.md +++ b/solution/0100-0199/0128.Longest Consecutive Sequence/README.md @@ -183,15 +183,13 @@ public: ```cpp class Solution { public: - int longestConsecutive(vector &nums) { + int longestConsecutive(vector& nums) { unordered_set s; for (int num : nums) s.insert(num); int res = 0; - for (int num : nums) - { - if (!s.count(num - 1)) - { + for (int num : nums) { + if (!s.count(num - 1)) { int t = 1, next = num + 1; while (s.count(next++)) ++t; diff --git a/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md b/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md index 96c17cfbd836f..2701af58d0539 100644 --- a/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md +++ b/solution/0100-0199/0128.Longest Consecutive Sequence/README_EN.md @@ -160,15 +160,13 @@ public: ```cpp class Solution { public: - int longestConsecutive(vector &nums) { + int longestConsecutive(vector& nums) { unordered_set s; for (int num : nums) s.insert(num); int res = 0; - for (int num : nums) - { - if (!s.count(num - 1)) - { + for (int num : nums) { + if (!s.count(num - 1)) { int t = 1, next = num + 1; while (s.count(next++)) ++t; diff --git a/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.cpp b/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.cpp index b541a92bef7d0..b14dd5e8a85e3 100644 --- a/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.cpp +++ b/solution/0100-0199/0128.Longest Consecutive Sequence/Solution.cpp @@ -1,14 +1,12 @@ class Solution { public: - int longestConsecutive(vector &nums) { + int longestConsecutive(vector& nums) { unordered_set s; for (int num : nums) s.insert(num); int res = 0; - for (int num : nums) - { - if (!s.count(num - 1)) - { + for (int num : nums) { + if (!s.count(num - 1)) { int t = 1, next = num + 1; while (s.count(next++)) ++t; diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md index 77831cb5f68c6..0c5799dfed223 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md @@ -171,11 +171,11 @@ function dfs(root: TreeNode | null, preSum: number = 0): number { */ class Solution { public: - int sumNumbers(TreeNode *root) { + int sumNumbers(TreeNode* root) { return dfs(root, 0); } - int dfs(TreeNode *root, int presum) { + int dfs(TreeNode* root, int presum) { if (!root) return 0; int s = presum * 10 + root->val; if (!root->left && !root->right) return s; diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md index d1881506dd7dd..ea688543d229f 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md @@ -158,11 +158,11 @@ function dfs(root: TreeNode | null, preSum: number = 0): number { */ class Solution { public: - int sumNumbers(TreeNode *root) { + int sumNumbers(TreeNode* root) { return dfs(root, 0); } - int dfs(TreeNode *root, int presum) { + int dfs(TreeNode* root, int presum) { if (!root) return 0; int s = presum * 10 + root->val; if (!root->left && !root->right) return s; diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp index 0fbf8c6053ac6..35410a7c10e89 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp @@ -11,11 +11,11 @@ */ class Solution { public: - int sumNumbers(TreeNode *root) { + int sumNumbers(TreeNode* root) { return dfs(root, 0); } - int dfs(TreeNode *root, int presum) { + int dfs(TreeNode* root, int presum) { if (!root) return 0; int s = presum * 10 + root->val; if (!root->left && !root->right) return s; diff --git a/solution/0100-0199/0130.Surrounded Regions/README.md b/solution/0100-0199/0130.Surrounded Regions/README.md index ef4da3957c143..68c034dd9ddd3 100644 --- a/solution/0100-0199/0130.Surrounded Regions/README.md +++ b/solution/0100-0199/0130.Surrounded Regions/README.md @@ -312,12 +312,12 @@ public: for (int j = 0; j < n; ++j) if ((i == 0 || i == m - 1 || j == 0 || j == n - 1) && board[i][j] == 'O') dfs(board, i, j); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (board[i][j] == '.') board[i][j] = 'O'; - else if (board[i][j] == 'O') board[i][j] = 'X'; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (board[i][j] == '.') + board[i][j] = 'O'; + else if (board[i][j] == 'O') + board[i][j] = 'X'; } } } @@ -325,8 +325,7 @@ public: void dfs(vector>& board, int i, int j) { board[i][j] = '.'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < board.size() && y >= 0 && y < board[0].size() && board[x][y] == 'O') dfs(board, x, y); diff --git a/solution/0100-0199/0130.Surrounded Regions/README_EN.md b/solution/0100-0199/0130.Surrounded Regions/README_EN.md index a5273fc3e7ddd..95afb22ec569f 100644 --- a/solution/0100-0199/0130.Surrounded Regions/README_EN.md +++ b/solution/0100-0199/0130.Surrounded Regions/README_EN.md @@ -302,12 +302,12 @@ public: for (int j = 0; j < n; ++j) if ((i == 0 || i == m - 1 || j == 0 || j == n - 1) && board[i][j] == 'O') dfs(board, i, j); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (board[i][j] == '.') board[i][j] = 'O'; - else if (board[i][j] == 'O') board[i][j] = 'X'; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (board[i][j] == '.') + board[i][j] = 'O'; + else if (board[i][j] == 'O') + board[i][j] = 'X'; } } } @@ -315,8 +315,7 @@ public: void dfs(vector>& board, int i, int j) { board[i][j] = '.'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < board.size() && y >= 0 && y < board[0].size() && board[x][y] == 'O') dfs(board, x, y); diff --git a/solution/0100-0199/0130.Surrounded Regions/Solution.cpp b/solution/0100-0199/0130.Surrounded Regions/Solution.cpp index e8dfcfb49d7df..824a92399a6b6 100644 --- a/solution/0100-0199/0130.Surrounded Regions/Solution.cpp +++ b/solution/0100-0199/0130.Surrounded Regions/Solution.cpp @@ -6,12 +6,12 @@ class Solution { for (int j = 0; j < n; ++j) if ((i == 0 || i == m - 1 || j == 0 || j == n - 1) && board[i][j] == 'O') dfs(board, i, j); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (board[i][j] == '.') board[i][j] = 'O'; - else if (board[i][j] == 'O') board[i][j] = 'X'; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (board[i][j] == '.') + board[i][j] = 'O'; + else if (board[i][j] == 'O') + board[i][j] = 'X'; } } } @@ -19,8 +19,7 @@ class Solution { void dfs(vector>& board, int i, int j) { board[i][j] = '.'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < board.size() && y >= 0 && y < board[0].size() && board[x][y] == 'O') dfs(board, x, y); diff --git a/solution/0100-0199/0131.Palindrome Partitioning/README.md b/solution/0100-0199/0131.Palindrome Partitioning/README.md index 041c912133769..11425a02a4948 100644 --- a/solution/0100-0199/0131.Palindrome Partitioning/README.md +++ b/solution/0100-0199/0131.Palindrome Partitioning/README.md @@ -124,10 +124,8 @@ public: vector> partition(string s) { n = s.size(); dp.assign(n, vector(n, true)); - for (int i = n - 1; i >= 0; --i) - { - for (int j = i + 1; j < n; ++j) - { + for (int i = n - 1; i >= 0; --i) { + for (int j = i + 1; j < n; ++j) { dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1]; } } @@ -137,15 +135,12 @@ public: } void dfs(string& s, int i, vector t) { - if (i == n) - { + if (i == n) { ans.push_back(t); return; } - for (int j = i; j < n; ++j) - { - if (dp[i][j]) - { + for (int j = i; j < n; ++j) { + if (dp[i][j]) { t.push_back(s.substr(i, j - i + 1)); dfs(s, j + 1, t); t.pop_back(); diff --git a/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md b/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md index cd4c3a8a4d6cb..2243b667b201e 100644 --- a/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md +++ b/solution/0100-0199/0131.Palindrome Partitioning/README_EN.md @@ -107,10 +107,8 @@ public: vector> partition(string s) { n = s.size(); dp.assign(n, vector(n, true)); - for (int i = n - 1; i >= 0; --i) - { - for (int j = i + 1; j < n; ++j) - { + for (int i = n - 1; i >= 0; --i) { + for (int j = i + 1; j < n; ++j) { dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1]; } } @@ -120,15 +118,12 @@ public: } void dfs(string& s, int i, vector t) { - if (i == n) - { + if (i == n) { ans.push_back(t); return; } - for (int j = i; j < n; ++j) - { - if (dp[i][j]) - { + for (int j = i; j < n; ++j) { + if (dp[i][j]) { t.push_back(s.substr(i, j - i + 1)); dfs(s, j + 1, t); t.pop_back(); diff --git a/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp b/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp index 9a0725108be44..de617ea30b03e 100644 --- a/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp +++ b/solution/0100-0199/0131.Palindrome Partitioning/Solution.cpp @@ -5,12 +5,10 @@ class Solution { int n; vector> partition(string s) { - n = s.size(); + n = s.size(); dp.assign(n, vector(n, true)); - for (int i = n - 1; i >= 0; --i) - { - for (int j = i + 1; j < n; ++j) - { + for (int i = n - 1; i >= 0; --i) { + for (int j = i + 1; j < n; ++j) { dp[i][j] = s[i] == s[j] && dp[i + 1][j - 1]; } } @@ -20,15 +18,12 @@ class Solution { } void dfs(string& s, int i, vector t) { - if (i == n) - { + if (i == n) { ans.push_back(t); return; } - for (int j = i; j < n; ++j) - { - if (dp[i][j]) - { + for (int j = i; j < n; ++j) { + if (dp[i][j]) { t.push_back(s.substr(i, j - i + 1)); dfs(s, j + 1, t); t.pop_back(); diff --git a/solution/0100-0199/0137.Single Number II/README.md b/solution/0100-0199/0137.Single Number II/README.md index cd612b2d1130b..03b4cbe6312c4 100644 --- a/solution/0100-0199/0137.Single Number II/README.md +++ b/solution/0100-0199/0137.Single Number II/README.md @@ -111,11 +111,9 @@ class Solution { public: int singleNumber(vector& nums) { int ans = 0; - for (int i = 0; i < 32; ++i) - { + for (int i = 0; i < 32; ++i) { int cnt = 0; - for (int num : nums) - { + for (int num : nums) { cnt += ((num >> i) & 1); } cnt %= 3; diff --git a/solution/0100-0199/0137.Single Number II/README_EN.md b/solution/0100-0199/0137.Single Number II/README_EN.md index 357c298cbcbfd..eccb956c76fcd 100644 --- a/solution/0100-0199/0137.Single Number II/README_EN.md +++ b/solution/0100-0199/0137.Single Number II/README_EN.md @@ -88,11 +88,9 @@ class Solution { public: int singleNumber(vector& nums) { int ans = 0; - for (int i = 0; i < 32; ++i) - { + for (int i = 0; i < 32; ++i) { int cnt = 0; - for (int num : nums) - { + for (int num : nums) { cnt += ((num >> i) & 1); } cnt %= 3; diff --git a/solution/0100-0199/0137.Single Number II/Solution.cpp b/solution/0100-0199/0137.Single Number II/Solution.cpp index 9b8a43b1904e2..689660fc4046a 100644 --- a/solution/0100-0199/0137.Single Number II/Solution.cpp +++ b/solution/0100-0199/0137.Single Number II/Solution.cpp @@ -2,11 +2,9 @@ class Solution { public: int singleNumber(vector& nums) { int ans = 0; - for (int i = 0; i < 32; ++i) - { + for (int i = 0; i < 32; ++i) { int cnt = 0; - for (int num : nums) - { + for (int num : nums) { cnt += ((num >> i) & 1); } cnt %= 3; diff --git a/solution/0100-0199/0139.Word Break/README.md b/solution/0100-0199/0139.Word Break/README.md index 1dd22f6700124..cbffa3c4d97dd 100644 --- a/solution/0100-0199/0139.Word Break/README.md +++ b/solution/0100-0199/0139.Word Break/README.md @@ -215,12 +215,9 @@ public: int n = s.size(); vector dp(n + 1); dp[0] = true; - for (int i = 1; i <= n; ++i) - { - for (int j = 0; j < i; ++j) - { - if (dp[j] && words.count(s.substr(j, i - j))) - { + for (int i = 1; i <= n; ++i) { + for (int j = 0; j < i; ++j) { + if (dp[j] && words.count(s.substr(j, i - j))) { dp[i] = true; break; } diff --git a/solution/0100-0199/0139.Word Break/README_EN.md b/solution/0100-0199/0139.Word Break/README_EN.md index f9f839eff2f17..b65602b84ca63 100644 --- a/solution/0100-0199/0139.Word Break/README_EN.md +++ b/solution/0100-0199/0139.Word Break/README_EN.md @@ -195,12 +195,9 @@ public: int n = s.size(); vector dp(n + 1); dp[0] = true; - for (int i = 1; i <= n; ++i) - { - for (int j = 0; j < i; ++j) - { - if (dp[j] && words.count(s.substr(j, i - j))) - { + for (int i = 1; i <= n; ++i) { + for (int j = 0; j < i; ++j) { + if (dp[j] && words.count(s.substr(j, i - j))) { dp[i] = true; break; } diff --git a/solution/0100-0199/0139.Word Break/Solution.cpp b/solution/0100-0199/0139.Word Break/Solution.cpp index 974ba73786404..0490603976360 100644 --- a/solution/0100-0199/0139.Word Break/Solution.cpp +++ b/solution/0100-0199/0139.Word Break/Solution.cpp @@ -5,12 +5,9 @@ class Solution { int n = s.size(); vector dp(n + 1); dp[0] = true; - for (int i = 1; i <= n; ++i) - { - for (int j = 0; j < i; ++j) - { - if (dp[j] && words.count(s.substr(j, i - j))) - { + for (int i = 1; i <= n; ++i) { + for (int j = 0; j < i; ++j) { + if (dp[j] && words.count(s.substr(j, i - j))) { dp[i] = true; break; } diff --git a/solution/0100-0199/0141.Linked List Cycle/README.md b/solution/0100-0199/0141.Linked List Cycle/README.md index e16df4e7ac3ce..5e7e43e99d88e 100644 --- a/solution/0100-0199/0141.Linked List Cycle/README.md +++ b/solution/0100-0199/0141.Linked List Cycle/README.md @@ -140,7 +140,7 @@ public class Solution { */ class Solution { public: - bool hasCycle(ListNode *head) { + bool hasCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while (fast && fast->next) { diff --git a/solution/0100-0199/0141.Linked List Cycle/README_EN.md b/solution/0100-0199/0141.Linked List Cycle/README_EN.md index f39cfd5b9a180..cca9e310b389c 100644 --- a/solution/0100-0199/0141.Linked List Cycle/README_EN.md +++ b/solution/0100-0199/0141.Linked List Cycle/README_EN.md @@ -113,7 +113,7 @@ public class Solution { */ class Solution { public: - bool hasCycle(ListNode *head) { + bool hasCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while (fast && fast->next) { diff --git a/solution/0100-0199/0141.Linked List Cycle/Solution.cpp b/solution/0100-0199/0141.Linked List Cycle/Solution.cpp index 87fd023dfa33c..cd489582fe452 100644 --- a/solution/0100-0199/0141.Linked List Cycle/Solution.cpp +++ b/solution/0100-0199/0141.Linked List Cycle/Solution.cpp @@ -8,7 +8,7 @@ */ class Solution { public: - bool hasCycle(ListNode *head) { + bool hasCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; while (fast && fast->next) { diff --git a/solution/0100-0199/0142.Linked List Cycle II/README.md b/solution/0100-0199/0142.Linked List Cycle II/README.md index 7776c31a36ca1..b783c3c78dffc 100644 --- a/solution/0100-0199/0142.Linked List Cycle II/README.md +++ b/solution/0100-0199/0142.Linked List Cycle II/README.md @@ -194,7 +194,7 @@ function detectCycle(head: ListNode | null): ListNode | null { */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; diff --git a/solution/0100-0199/0142.Linked List Cycle II/README_EN.md b/solution/0100-0199/0142.Linked List Cycle II/README_EN.md index f98e620098d10..5c43d766e80b4 100644 --- a/solution/0100-0199/0142.Linked List Cycle II/README_EN.md +++ b/solution/0100-0199/0142.Linked List Cycle II/README_EN.md @@ -160,7 +160,7 @@ function detectCycle(head: ListNode | null): ListNode | null { */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; diff --git a/solution/0100-0199/0142.Linked List Cycle II/Solution.cpp b/solution/0100-0199/0142.Linked List Cycle II/Solution.cpp index c5ce3fefb03b8..890916dbc2485 100644 --- a/solution/0100-0199/0142.Linked List Cycle II/Solution.cpp +++ b/solution/0100-0199/0142.Linked List Cycle II/Solution.cpp @@ -8,7 +8,7 @@ */ class Solution { public: - ListNode *detectCycle(ListNode *head) { + ListNode* detectCycle(ListNode* head) { ListNode* slow = head; ListNode* fast = head; bool hasCycle = false; diff --git a/solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md b/solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md index 120acb4f6f1ff..2880351772e9f 100644 --- a/solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md +++ b/solution/0100-0199/0144.Binary Tree Preorder Traversal/README.md @@ -391,28 +391,20 @@ class Solution { public: vector preorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->left) - { + while (root) { + if (!root->left) { ans.push_back(root->val); root = root->right; - } - else - { + } else { TreeNode* prev = root->left; - while (prev->right && prev->right != root) - { + while (prev->right && prev->right != root) { prev = prev->right; } - if (!prev->right) - { + if (!prev->right) { ans.push_back(root->val); prev->right = root; root = root->left; - } - else - { + } else { prev->right = nullptr; root = root->right; } diff --git a/solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md b/solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md index b71809589a99f..c020f12f08d5e 100644 --- a/solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md +++ b/solution/0100-0199/0144.Binary Tree Preorder Traversal/README_EN.md @@ -345,28 +345,20 @@ class Solution { public: vector preorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->left) - { + while (root) { + if (!root->left) { ans.push_back(root->val); root = root->right; - } - else - { + } else { TreeNode* prev = root->left; - while (prev->right && prev->right != root) - { + while (prev->right && prev->right != root) { prev = prev->right; } - if (!prev->right) - { + if (!prev->right) { ans.push_back(root->val); prev->right = root; root = root->left; - } - else - { + } else { prev->right = nullptr; root = root->right; } diff --git a/solution/0100-0199/0144.Binary Tree Preorder Traversal/Solution.cpp b/solution/0100-0199/0144.Binary Tree Preorder Traversal/Solution.cpp index 50eee43b253cb..e5d4e3e0adaa3 100644 --- a/solution/0100-0199/0144.Binary Tree Preorder Traversal/Solution.cpp +++ b/solution/0100-0199/0144.Binary Tree Preorder Traversal/Solution.cpp @@ -13,28 +13,20 @@ class Solution { public: vector preorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->left) - { + while (root) { + if (!root->left) { ans.push_back(root->val); root = root->right; - } - else - { + } else { TreeNode* prev = root->left; - while (prev->right && prev->right != root) - { + while (prev->right && prev->right != root) { prev = prev->right; } - if (!prev->right) - { + if (!prev->right) { ans.push_back(root->val); prev->right = root; root = root->left; - } - else - { + } else { prev->right = nullptr; root = root->right; } diff --git a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md index f881022e52443..06702deb44561 100644 --- a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md +++ b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README.md @@ -412,28 +412,20 @@ class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->right) - { + while (root) { + if (!root->right) { ans.push_back(root->val); root = root->left; - } - else - { + } else { TreeNode* next = root->right; - while (next->left && next->left != root) - { + while (next->left && next->left != root) { next = next->left; } - if (!next->left) - { + if (!next->left) { ans.push_back(root->val); next->left = root; root = root->right; - } - else - { + } else { next->left = nullptr; root = root->left; } diff --git a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md index ecee73a23ac80..dd498999e786d 100644 --- a/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md +++ b/solution/0100-0199/0145.Binary Tree Postorder Traversal/README_EN.md @@ -380,28 +380,20 @@ class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->right) - { + while (root) { + if (!root->right) { ans.push_back(root->val); root = root->left; - } - else - { + } else { TreeNode* next = root->right; - while (next->left && next->left != root) - { + while (next->left && next->left != root) { next = next->left; } - if (!next->left) - { + if (!next->left) { ans.push_back(root->val); next->left = root; root = root->right; - } - else - { + } else { next->left = nullptr; root = root->left; } diff --git a/solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.cpp b/solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.cpp index 1fd297894d41f..005e32dd17533 100644 --- a/solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.cpp +++ b/solution/0100-0199/0145.Binary Tree Postorder Traversal/Solution.cpp @@ -13,28 +13,20 @@ class Solution { public: vector postorderTraversal(TreeNode* root) { vector ans; - while (root) - { - if (!root->right) - { + while (root) { + if (!root->right) { ans.push_back(root->val); root = root->left; - } - else - { + } else { TreeNode* next = root->right; - while (next->left && next->left != root) - { + while (next->left && next->left != root) { next = next->left; } - if (!next->left) - { + if (!next->left) { ans.push_back(root->val); next->left = root; root = root->right; - } - else - { + } else { next->left = nullptr; root = root->left; } diff --git a/solution/0100-0199/0146.LRU Cache/README.md b/solution/0100-0199/0146.LRU Cache/README.md index f540b154b4834..a666ff8acc1de 100644 --- a/solution/0100-0199/0146.LRU Cache/README.md +++ b/solution/0100-0199/0146.LRU Cache/README.md @@ -484,13 +484,23 @@ struct Node { Node* prev; Node* next; - Node(): k(0), v(0), prev(nullptr), next(nullptr) {} - Node(int key, int val): k(key), v(val), prev(nullptr), next(nullptr) {} + Node() + : k(0) + , v(0) + , prev(nullptr) + , next(nullptr) { } + Node(int key, int val) + : k(key) + , v(val) + , prev(nullptr) + , next(nullptr) { } }; class LRUCache { public: - LRUCache(int capacity): cap(capacity), size(0) { + LRUCache(int capacity) + : cap(capacity) + , size(0) { head = new Node(); tail = new Node(); head->next = tail; @@ -505,20 +515,16 @@ public: } void put(int key, int value) { - if (cache.count(key)) - { + if (cache.count(key)) { Node* node = cache[key]; node->v = value; moveToHead(node); - } - else - { + } else { Node* node = new Node(key, value); cache[key] = node; addToHead(node); ++size; - if (size > cap) - { + if (size > cap) { node = removeTail(); cache.erase(node->k); --size; diff --git a/solution/0100-0199/0146.LRU Cache/README_EN.md b/solution/0100-0199/0146.LRU Cache/README_EN.md index b49e2bf87c661..f3d0d986d8a6b 100644 --- a/solution/0100-0199/0146.LRU Cache/README_EN.md +++ b/solution/0100-0199/0146.LRU Cache/README_EN.md @@ -431,13 +431,23 @@ struct Node { Node* prev; Node* next; - Node(): k(0), v(0), prev(nullptr), next(nullptr) {} - Node(int key, int val): k(key), v(val), prev(nullptr), next(nullptr) {} + Node() + : k(0) + , v(0) + , prev(nullptr) + , next(nullptr) { } + Node(int key, int val) + : k(key) + , v(val) + , prev(nullptr) + , next(nullptr) { } }; class LRUCache { public: - LRUCache(int capacity): cap(capacity), size(0) { + LRUCache(int capacity) + : cap(capacity) + , size(0) { head = new Node(); tail = new Node(); head->next = tail; @@ -452,20 +462,16 @@ public: } void put(int key, int value) { - if (cache.count(key)) - { + if (cache.count(key)) { Node* node = cache[key]; node->v = value; moveToHead(node); - } - else - { + } else { Node* node = new Node(key, value); cache[key] = node; addToHead(node); ++size; - if (size > cap) - { + if (size > cap) { node = removeTail(); cache.erase(node->k); --size; diff --git a/solution/0100-0199/0146.LRU Cache/Solution.cpp b/solution/0100-0199/0146.LRU Cache/Solution.cpp index cba6e7c238897..50247e42b3014 100644 --- a/solution/0100-0199/0146.LRU Cache/Solution.cpp +++ b/solution/0100-0199/0146.LRU Cache/Solution.cpp @@ -4,41 +4,47 @@ struct Node { Node* prev; Node* next; - Node(): k(0), v(0), prev(nullptr), next(nullptr) {} - Node(int key, int val): k(key), v(val), prev(nullptr), next(nullptr) {} + Node() + : k(0) + , v(0) + , prev(nullptr) + , next(nullptr) { } + Node(int key, int val) + : k(key) + , v(val) + , prev(nullptr) + , next(nullptr) { } }; class LRUCache { public: - LRUCache(int capacity): cap(capacity), size(0) { + LRUCache(int capacity) + : cap(capacity) + , size(0) { head = new Node(); tail = new Node(); head->next = tail; tail->prev = head; } - + int get(int key) { if (!cache.count(key)) return -1; Node* node = cache[key]; moveToHead(node); return node->v; } - + void put(int key, int value) { - if (cache.count(key)) - { + if (cache.count(key)) { Node* node = cache[key]; node->v = value; moveToHead(node); - } - else - { + } else { Node* node = new Node(key, value); cache[key] = node; addToHead(node); ++size; - if (size > cap) - { + if (size > cap) { node = removeTail(); cache.erase(node->k); --size; diff --git a/solution/0100-0199/0148.Sort List/README.md b/solution/0100-0199/0148.Sort List/README.md index 07cdc0dbccd3c..13413f458bdef 100644 --- a/solution/0100-0199/0148.Sort List/README.md +++ b/solution/0100-0199/0148.Sort List/README.md @@ -155,8 +155,7 @@ public: if (!head || !head->next) return head; auto* slow = head; auto* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -166,15 +165,11 @@ public: auto* l2 = sortList(t); auto* dummy = new ListNode(); auto* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git a/solution/0100-0199/0148.Sort List/README_EN.md b/solution/0100-0199/0148.Sort List/README_EN.md index 61f64c4d03e20..b8c2024c9ad8c 100644 --- a/solution/0100-0199/0148.Sort List/README_EN.md +++ b/solution/0100-0199/0148.Sort List/README_EN.md @@ -139,8 +139,7 @@ public: if (!head || !head->next) return head; auto* slow = head; auto* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -150,15 +149,11 @@ public: auto* l2 = sortList(t); auto* dummy = new ListNode(); auto* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git a/solution/0100-0199/0148.Sort List/Solution.cpp b/solution/0100-0199/0148.Sort List/Solution.cpp index b78b98175803c..f41cd39e5ddb7 100644 --- a/solution/0100-0199/0148.Sort List/Solution.cpp +++ b/solution/0100-0199/0148.Sort List/Solution.cpp @@ -14,8 +14,7 @@ class Solution { if (!head || !head->next) return head; auto* slow = head; auto* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -25,15 +24,11 @@ class Solution { auto* l2 = sortList(t); auto* dummy = new ListNode(); auto* cur = dummy; - while (l1 && l2) - { - if (l1->val <= l2->val) - { + while (l1 && l2) { + if (l1->val <= l2->val) { cur->next = l1; l1 = l1->next; - } - else - { + } else { cur->next = l2; l2 = l2->next; } diff --git a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md index 4d1430fe731f4..a3b89cd42f809 100644 --- a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md +++ b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README.md @@ -170,20 +170,21 @@ public: int evalRPN(vector& tokens) { stack stk; for (auto& t : tokens) { - if (t.size() > 1 || isdigit(t[0])) - { + if (t.size() > 1 || isdigit(t[0])) { stk.push(stoi(t)); - } - else - { + } else { int y = stk.top(); stk.pop(); int x = stk.top(); stk.pop(); - if (t[0] == '+') stk.push(x + y); - else if (t[0] == '-') stk.push(x - y); - else if (t[0] == '*') stk.push(x * y); - else stk.push(x / y); + if (t[0] == '+') + stk.push(x + y); + else if (t[0] == '-') + stk.push(x - y); + else if (t[0] == '*') + stk.push(x * y); + else + stk.push(x / y); } } return stk.top(); diff --git a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md index 47c35d7836058..f40599f225467 100644 --- a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md +++ b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/README_EN.md @@ -138,20 +138,21 @@ public: int evalRPN(vector& tokens) { stack stk; for (auto& t : tokens) { - if (t.size() > 1 || isdigit(t[0])) - { + if (t.size() > 1 || isdigit(t[0])) { stk.push(stoi(t)); - } - else - { + } else { int y = stk.top(); stk.pop(); int x = stk.top(); stk.pop(); - if (t[0] == '+') stk.push(x + y); - else if (t[0] == '-') stk.push(x - y); - else if (t[0] == '*') stk.push(x * y); - else stk.push(x / y); + if (t[0] == '+') + stk.push(x + y); + else if (t[0] == '-') + stk.push(x - y); + else if (t[0] == '*') + stk.push(x * y); + else + stk.push(x / y); } } return stk.top(); diff --git a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.cpp b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.cpp index 0c346abfadc81..67ebfcb3ec300 100644 --- a/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.cpp +++ b/solution/0100-0199/0150.Evaluate Reverse Polish Notation/Solution.cpp @@ -3,20 +3,21 @@ class Solution { int evalRPN(vector& tokens) { stack stk; for (auto& t : tokens) { - if (t.size() > 1 || isdigit(t[0])) - { + if (t.size() > 1 || isdigit(t[0])) { stk.push(stoi(t)); - } - else - { + } else { int y = stk.top(); stk.pop(); int x = stk.top(); stk.pop(); - if (t[0] == '+') stk.push(x + y); - else if (t[0] == '-') stk.push(x - y); - else if (t[0] == '*') stk.push(x * y); - else stk.push(x / y); + if (t[0] == '+') + stk.push(x + y); + else if (t[0] == '-') + stk.push(x - y); + else if (t[0] == '*') + stk.push(x * y); + else + stk.push(x / y); } } return stk.top(); diff --git a/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README.md b/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README.md index 02cb7b35d85f8..0b32d409c990f 100644 --- a/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README.md +++ b/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README.md @@ -128,11 +128,12 @@ public: int n = nums.size(); if (nums[0] <= nums[n - 1]) return nums[0]; int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[0] <= nums[mid]) left = mid + 1; - else right = mid; + if (nums[0] <= nums[mid]) + left = mid + 1; + else + right = mid; } return nums[left]; } diff --git a/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README_EN.md b/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README_EN.md index 46c7800dd0d09..62dc89d2acbe0 100644 --- a/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README_EN.md +++ b/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/README_EN.md @@ -106,11 +106,12 @@ public: int n = nums.size(); if (nums[0] <= nums[n - 1]) return nums[0]; int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[0] <= nums[mid]) left = mid + 1; - else right = mid; + if (nums[0] <= nums[mid]) + left = mid + 1; + else + right = mid; } return nums[left]; } diff --git a/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/Solution.cpp b/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/Solution.cpp index 3a9ae846e97fc..9d9e0d56518f6 100644 --- a/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/Solution.cpp +++ b/solution/0100-0199/0153.Find Minimum in Rotated Sorted Array/Solution.cpp @@ -4,11 +4,12 @@ class Solution { int n = nums.size(); if (nums[0] <= nums[n - 1]) return nums[0]; int left = 0, right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[0] <= nums[mid]) left = mid + 1; - else right = mid; + if (nums[0] <= nums[mid]) + left = mid + 1; + else + right = mid; } return nums[left]; } diff --git a/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README.md b/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README.md index dba74ddab0c4c..723abe843e145 100644 --- a/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README.md +++ b/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README.md @@ -113,12 +113,14 @@ class Solution { public: int findMin(vector& nums) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[mid] > nums[right]) left = mid + 1; - else if (nums[mid] < nums[right]) right = mid; - else --right; + if (nums[mid] > nums[right]) + left = mid + 1; + else if (nums[mid] < nums[right]) + right = mid; + else + --right; } return nums[left]; } diff --git a/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README_EN.md b/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README_EN.md index 40da7e8be350b..f89ff89c45448 100644 --- a/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README_EN.md +++ b/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/README_EN.md @@ -89,12 +89,14 @@ class Solution { public: int findMin(vector& nums) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[mid] > nums[right]) left = mid + 1; - else if (nums[mid] < nums[right]) right = mid; - else --right; + if (nums[mid] > nums[right]) + left = mid + 1; + else if (nums[mid] < nums[right]) + right = mid; + else + --right; } return nums[left]; } diff --git a/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/Solution.cpp b/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/Solution.cpp index ae1c9167bbfc2..3d316e8796b4f 100644 --- a/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/Solution.cpp +++ b/solution/0100-0199/0154.Find Minimum in Rotated Sorted Array II/Solution.cpp @@ -2,12 +2,14 @@ class Solution { public: int findMin(vector& nums) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (nums[mid] > nums[right]) left = mid + 1; - else if (nums[mid] < nums[right]) right = mid; - else --right; + if (nums[mid] > nums[right]) + left = mid + 1; + else if (nums[mid] < nums[right]) + right = mid; + else + --right; } return nums[left]; } diff --git a/solution/0100-0199/0155.Min Stack/Solution.cpp b/solution/0100-0199/0155.Min Stack/Solution.cpp index 7b1846553bbe9..b250922b8fe1e 100644 --- a/solution/0100-0199/0155.Min Stack/Solution.cpp +++ b/solution/0100-0199/0155.Min Stack/Solution.cpp @@ -8,21 +8,21 @@ class MinStack { MinStack() { mins.push(INT_MAX); } - + void push(int val) { s.push(val); mins.push(min(mins.top(), val)); } - + void pop() { s.pop(); mins.pop(); } - + int top() { return s.top(); } - + int getMin() { return mins.top(); } diff --git a/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README.md b/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README.md index 7359e4b20749f..911f1a558ebb6 100644 --- a/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README.md +++ b/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README.md @@ -99,11 +99,9 @@ public: int lengthOfLongestSubstringTwoDistinct(string s) { unordered_map mp; int i = 0, j = 0, ans = 0; - for (char& c : s) - { + for (char& c : s) { ++mp[c]; - while (mp.size() > 2) - { + while (mp.size() > 2) { --mp[s[i]]; if (mp[s[i]] == 0) mp.erase(s[i]); ++i; diff --git a/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README_EN.md b/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README_EN.md index d3cf681ac73d0..993c7af5ee50c 100644 --- a/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README_EN.md +++ b/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/README_EN.md @@ -87,11 +87,9 @@ public: int lengthOfLongestSubstringTwoDistinct(string s) { unordered_map mp; int i = 0, j = 0, ans = 0; - for (char& c : s) - { + for (char& c : s) { ++mp[c]; - while (mp.size() > 2) - { + while (mp.size() > 2) { --mp[s[i]]; if (mp[s[i]] == 0) mp.erase(s[i]); ++i; diff --git a/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/Solution.cpp b/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/Solution.cpp index e85f734eacfd6..2d4fceca257b8 100644 --- a/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/Solution.cpp +++ b/solution/0100-0199/0159.Longest Substring with At Most Two Distinct Characters/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int lengthOfLongestSubstringTwoDistinct(string s) { unordered_map mp; int i = 0, j = 0, ans = 0; - for (char& c : s) - { + for (char& c : s) { ++mp[c]; - while (mp.size() > 2) - { + while (mp.size() > 2) { --mp[s[i]]; if (mp[s[i]] == 0) mp.erase(s[i]); ++i; diff --git a/solution/0100-0199/0160.Intersection of Two Linked Lists/README.md b/solution/0100-0199/0160.Intersection of Two Linked Lists/README.md index 4ebdb0a8ee596..0681c4edf9511 100644 --- a/solution/0100-0199/0160.Intersection of Two Linked Lists/README.md +++ b/solution/0100-0199/0160.Intersection of Two Linked Lists/README.md @@ -164,10 +164,9 @@ public class Solution { */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode *a = headA, *b = headB; - while (a != b) - { + while (a != b) { a = a ? a->next : headB; b = b ? b->next : headA; } diff --git a/solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md b/solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md index 05c0658dae98c..eb22011f6f2ca 100644 --- a/solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md +++ b/solution/0100-0199/0160.Intersection of Two Linked Lists/README_EN.md @@ -132,10 +132,9 @@ public class Solution { */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode *a = headA, *b = headB; - while (a != b) - { + while (a != b) { a = a ? a->next : headB; b = b ? b->next : headA; } diff --git a/solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.cpp b/solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.cpp index 25c1f5ffe3994..6c4f6f2552d78 100644 --- a/solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.cpp +++ b/solution/0100-0199/0160.Intersection of Two Linked Lists/Solution.cpp @@ -8,10 +8,9 @@ */ class Solution { public: - ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { + ListNode* getIntersectionNode(ListNode* headA, ListNode* headB) { ListNode *a = headA, *b = headB; - while (a != b) - { + while (a != b) { a = a ? a->next : headB; b = b ? b->next : headA; } diff --git a/solution/0100-0199/0164.Maximum Gap/README.md b/solution/0100-0199/0164.Maximum Gap/README.md index c86a860476f65..770c5f3788448 100644 --- a/solution/0100-0199/0164.Maximum Gap/README.md +++ b/solution/0100-0199/0164.Maximum Gap/README.md @@ -147,25 +147,22 @@ public: int maximumGap(vector& nums) { int n = nums.size(); if (n < 2) return 0; - int mi = inf, mx = - inf; - for (int v : nums) - { + int mi = inf, mx = -inf; + for (int v : nums) { mi = min(mi, v); mx = max(mx, v); } int bucketSize = max(1, (mx - mi) / (n - 1)); int bucketCount = (mx - mi) / bucketSize + 1; vector buckets(bucketCount, {inf, -inf}); - for (int v : nums) - { + for (int v : nums) { int i = (v - mi) / bucketSize; buckets[i].first = min(buckets[i].first, v); buckets[i].second = max(buckets[i].second, v); } int ans = 0; int prev = inf; - for (auto [curmin, curmax] : buckets) - { + for (auto [curmin, curmax] : buckets) { if (curmin > curmax) continue; ans = max(ans, curmin - prev); prev = curmax; diff --git a/solution/0100-0199/0164.Maximum Gap/README_EN.md b/solution/0100-0199/0164.Maximum Gap/README_EN.md index 2d0db99dc3e4f..8dd2e23fc9a9f 100644 --- a/solution/0100-0199/0164.Maximum Gap/README_EN.md +++ b/solution/0100-0199/0164.Maximum Gap/README_EN.md @@ -115,25 +115,22 @@ public: int maximumGap(vector& nums) { int n = nums.size(); if (n < 2) return 0; - int mi = inf, mx = - inf; - for (int v : nums) - { + int mi = inf, mx = -inf; + for (int v : nums) { mi = min(mi, v); mx = max(mx, v); } int bucketSize = max(1, (mx - mi) / (n - 1)); int bucketCount = (mx - mi) / bucketSize + 1; vector buckets(bucketCount, {inf, -inf}); - for (int v : nums) - { + for (int v : nums) { int i = (v - mi) / bucketSize; buckets[i].first = min(buckets[i].first, v); buckets[i].second = max(buckets[i].second, v); } int ans = 0; int prev = inf; - for (auto [curmin, curmax] : buckets) - { + for (auto [curmin, curmax] : buckets) { if (curmin > curmax) continue; ans = max(ans, curmin - prev); prev = curmax; diff --git a/solution/0100-0199/0164.Maximum Gap/Solution.cpp b/solution/0100-0199/0164.Maximum Gap/Solution.cpp index 5df93868bcfda..fc6e101dec46f 100644 --- a/solution/0100-0199/0164.Maximum Gap/Solution.cpp +++ b/solution/0100-0199/0164.Maximum Gap/Solution.cpp @@ -6,25 +6,22 @@ class Solution { int maximumGap(vector& nums) { int n = nums.size(); if (n < 2) return 0; - int mi = inf, mx = - inf; - for (int v : nums) - { + int mi = inf, mx = -inf; + for (int v : nums) { mi = min(mi, v); mx = max(mx, v); } int bucketSize = max(1, (mx - mi) / (n - 1)); int bucketCount = (mx - mi) / bucketSize + 1; vector buckets(bucketCount, {inf, -inf}); - for (int v : nums) - { + for (int v : nums) { int i = (v - mi) / bucketSize; buckets[i].first = min(buckets[i].first, v); buckets[i].second = max(buckets[i].second, v); } int ans = 0; int prev = inf; - for (auto [curmin, curmax] : buckets) - { + for (auto [curmin, curmax] : buckets) { if (curmin > curmax) continue; ans = max(ans, curmin - prev); prev = curmax; diff --git a/solution/0100-0199/0165.Compare Version Numbers/README.md b/solution/0100-0199/0165.Compare Version Numbers/README.md index c93b4d5e2f293..02c5968f37ce7 100644 --- a/solution/0100-0199/0165.Compare Version Numbers/README.md +++ b/solution/0100-0199/0165.Compare Version Numbers/README.md @@ -138,8 +138,7 @@ function compareVersion(version1: string, version2: string): number { class Solution { public: int compareVersion(string version1, string version2) { - for (int i = 0, j = 0; i < version1.size() || j < version2.size(); ++i, ++j) - { + for (int i = 0, j = 0; i < version1.size() || j < version2.size(); ++i, ++j) { int a = 0, b = 0; while (i < version1.size() && version1[i] != '.') a = a * 10 + version1[i++] - '0'; diff --git a/solution/0100-0199/0165.Compare Version Numbers/README_EN.md b/solution/0100-0199/0165.Compare Version Numbers/README_EN.md index ea8c8ec36d3de..546ec4ce20c8d 100644 --- a/solution/0100-0199/0165.Compare Version Numbers/README_EN.md +++ b/solution/0100-0199/0165.Compare Version Numbers/README_EN.md @@ -125,8 +125,7 @@ function compareVersion(version1: string, version2: string): number { class Solution { public: int compareVersion(string version1, string version2) { - for (int i = 0, j = 0; i < version1.size() || j < version2.size(); ++i, ++j) - { + for (int i = 0, j = 0; i < version1.size() || j < version2.size(); ++i, ++j) { int a = 0, b = 0; while (i < version1.size() && version1[i] != '.') a = a * 10 + version1[i++] - '0'; diff --git a/solution/0100-0199/0165.Compare Version Numbers/Solution.cpp b/solution/0100-0199/0165.Compare Version Numbers/Solution.cpp index 5dc73ce912a0c..162730b1995ff 100644 --- a/solution/0100-0199/0165.Compare Version Numbers/Solution.cpp +++ b/solution/0100-0199/0165.Compare Version Numbers/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: int compareVersion(string version1, string version2) { - for (int i = 0, j = 0; i < version1.size() || j < version2.size(); ++i, ++j) - { + for (int i = 0, j = 0; i < version1.size() || j < version2.size(); ++i, ++j) { int a = 0, b = 0; while (i < version1.size() && version1[i] != '.') a = a * 10 + version1[i++] - '0'; diff --git a/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md b/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md index a272b13b179fb..11e393ee45103 100644 --- a/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md +++ b/solution/0100-0199/0166.Fraction to Recurring Decimal/README.md @@ -145,14 +145,12 @@ public: if (num == 0) return res; res += "."; unordered_map mp; - while (num) - { + while (num) { mp[num] = res.size(); num *= 10; res += to_string(num / d); num %= d; - if (mp.count(num)) - { + if (mp.count(num)) { int idx = mp[num]; res.insert(idx, "("); res += ")"; diff --git a/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md b/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md index bc0f9708870b4..505276e915bd1 100644 --- a/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md +++ b/solution/0100-0199/0166.Fraction to Recurring Decimal/README_EN.md @@ -133,14 +133,12 @@ public: if (num == 0) return res; res += "."; unordered_map mp; - while (num) - { + while (num) { mp[num] = res.size(); num *= 10; res += to_string(num / d); num %= d; - if (mp.count(num)) - { + if (mp.count(num)) { int idx = mp[num]; res.insert(idx, "("); res += ")"; diff --git a/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.cpp b/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.cpp index e22b66c98f844..69f6f01574981 100644 --- a/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.cpp +++ b/solution/0100-0199/0166.Fraction to Recurring Decimal/Solution.cpp @@ -14,14 +14,12 @@ class Solution { if (num == 0) return res; res += "."; unordered_map mp; - while (num) - { + while (num) { mp[num] = res.size(); num *= 10; res += to_string(num / d); num %= d; - if (mp.count(num)) - { + if (mp.count(num)) { int idx = mp[num]; res.insert(idx, "("); res += ")"; diff --git a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.cpp b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.cpp index 014e34f59d45e..87fad63e2be7e 100644 --- a/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.cpp +++ b/solution/0100-0199/0167.Two Sum II - Input Array Is Sorted/Solution.cpp @@ -2,12 +2,13 @@ public: vector twoSum(vector& numbers, int target) { int left = 1, right = numbers.size(); - while (left < right) - { + while (left < right) { int x = numbers[left - 1] + numbers[right - 1]; if (x == target) return {left, right}; - if (x < target) ++left; - else --right; + if (x < target) + ++left; + else + --right; } return {-1, -1}; } diff --git a/solution/0100-0199/0169.Majority Element/README.md b/solution/0100-0199/0169.Majority Element/README.md index 6bcc1ee1a9f61..2bc1d4cf158cf 100644 --- a/solution/0100-0199/0169.Majority Element/README.md +++ b/solution/0100-0199/0169.Majority Element/README.md @@ -124,14 +124,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } return m; } diff --git a/solution/0100-0199/0169.Majority Element/README_EN.md b/solution/0100-0199/0169.Majority Element/README_EN.md index 5095c85eb458d..28b66808ec3b8 100644 --- a/solution/0100-0199/0169.Majority Element/README_EN.md +++ b/solution/0100-0199/0169.Majority Element/README_EN.md @@ -94,14 +94,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } return m; } diff --git a/solution/0100-0199/0169.Majority Element/Solution.cpp b/solution/0100-0199/0169.Majority Element/Solution.cpp index 279972790d91b..f487480e340a0 100644 --- a/solution/0100-0199/0169.Majority Element/Solution.cpp +++ b/solution/0100-0199/0169.Majority Element/Solution.cpp @@ -2,14 +2,12 @@ class Solution { public: int majorityElement(vector& nums) { int cnt = 0, m = 0; - for (int& v : nums) - { - if (cnt == 0) - { + for (int& v : nums) { + if (cnt == 0) { m = v; cnt = 1; - } - else cnt += (m == v ? 1 : -1); + } else + cnt += (m == v ? 1 : -1); } return m; } diff --git a/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.cpp b/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.cpp index 7cffe141cff36..1e9939f2773f1 100644 --- a/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.cpp +++ b/solution/0100-0199/0173.Binary Search Tree Iterator/Solution.cpp @@ -17,7 +17,7 @@ class BSTIterator { stack.push(root); } } - + int next() { TreeNode* cur = stack.top(); stack.pop(); @@ -27,7 +27,7 @@ class BSTIterator { } return cur->val; } - + bool hasNext() { return !stack.empty(); } diff --git a/solution/0100-0199/0191.Number of 1 Bits/README.md b/solution/0100-0199/0191.Number of 1 Bits/README.md index 32dbc8bd82ab7..447eae0db6072 100644 --- a/solution/0100-0199/0191.Number of 1 Bits/README.md +++ b/solution/0100-0199/0191.Number of 1 Bits/README.md @@ -161,8 +161,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n &= n - 1; ++ans; } @@ -176,8 +175,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n -= (n & -n); ++ans; } diff --git a/solution/0100-0199/0191.Number of 1 Bits/README_EN.md b/solution/0100-0199/0191.Number of 1 Bits/README_EN.md index 74d3dbd013122..0a7e1e009356a 100644 --- a/solution/0100-0199/0191.Number of 1 Bits/README_EN.md +++ b/solution/0100-0199/0191.Number of 1 Bits/README_EN.md @@ -111,8 +111,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n &= n - 1; ++ans; } @@ -126,8 +125,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n -= (n & -n); ++ans; } diff --git a/solution/0100-0199/0191.Number of 1 Bits/Solution.cpp b/solution/0100-0199/0191.Number of 1 Bits/Solution.cpp index 0c33aaeecd64c..a14f6399639df 100644 --- a/solution/0100-0199/0191.Number of 1 Bits/Solution.cpp +++ b/solution/0100-0199/0191.Number of 1 Bits/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int hammingWeight(uint32_t n) { int ans = 0; - while (n) - { + while (n) { n &= n - 1; ++ans; } diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/README.md b/solution/0100-0199/0199.Binary Tree Right Side View/README.md index f7934cef1f0f3..2a06ec669fa4c 100644 --- a/solution/0100-0199/0199.Binary Tree Right Side View/README.md +++ b/solution/0100-0199/0199.Binary Tree Right Side View/README.md @@ -142,12 +142,10 @@ public: vector rightSideView(TreeNode* root) { vector ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { ans.push_back(q.front()->val); - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { TreeNode* node = q.front(); q.pop(); if (node->right) q.push(node->right); diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md b/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md index bbfb61fc1c571..8ee829791d6c2 100644 --- a/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md +++ b/solution/0100-0199/0199.Binary Tree Right Side View/README_EN.md @@ -128,12 +128,10 @@ public: vector rightSideView(TreeNode* root) { vector ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { ans.push_back(q.front()->val); - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { TreeNode* node = q.front(); q.pop(); if (node->right) q.push(node->right); diff --git a/solution/0100-0199/0199.Binary Tree Right Side View/Solution.cpp b/solution/0100-0199/0199.Binary Tree Right Side View/Solution.cpp index 064392a21d641..99b5e8d90328e 100644 --- a/solution/0100-0199/0199.Binary Tree Right Side View/Solution.cpp +++ b/solution/0100-0199/0199.Binary Tree Right Side View/Solution.cpp @@ -14,12 +14,10 @@ class Solution { vector rightSideView(TreeNode* root) { vector ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { ans.push_back(q.front()->val); - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { TreeNode* node = q.front(); q.pop(); if (node->right) q.push(node->right); diff --git a/solution/0200-0299/0200.Number of Islands/README.md b/solution/0200-0299/0200.Number of Islands/README.md index c05644b0b00a3..0fed71794405e 100644 --- a/solution/0200-0299/0200.Number of Islands/README.md +++ b/solution/0200-0299/0200.Number of Islands/README.md @@ -388,12 +388,9 @@ public: int m = grid.size(); int n = grid[0].size(); int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == '1') - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == '1') { dfs(i, j, grid); ++ans; } @@ -405,8 +402,7 @@ public: void dfs(int i, int j, vector>& grid) { grid[i][j] = '0'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == '1') dfs(x, y, grid); diff --git a/solution/0200-0299/0200.Number of Islands/README_EN.md b/solution/0200-0299/0200.Number of Islands/README_EN.md index f8151df885af0..14accb40c671e 100644 --- a/solution/0200-0299/0200.Number of Islands/README_EN.md +++ b/solution/0200-0299/0200.Number of Islands/README_EN.md @@ -374,12 +374,9 @@ public: int m = grid.size(); int n = grid[0].size(); int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == '1') - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == '1') { dfs(i, j, grid); ++ans; } @@ -391,8 +388,7 @@ public: void dfs(int i, int j, vector>& grid) { grid[i][j] = '0'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == '1') dfs(x, y, grid); diff --git a/solution/0200-0299/0200.Number of Islands/Solution.cpp b/solution/0200-0299/0200.Number of Islands/Solution.cpp index 89da9438cf1ba..a644dabaed523 100644 --- a/solution/0200-0299/0200.Number of Islands/Solution.cpp +++ b/solution/0200-0299/0200.Number of Islands/Solution.cpp @@ -4,12 +4,9 @@ class Solution { int m = grid.size(); int n = grid[0].size(); int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == '1') - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == '1') { dfs(i, j, grid); ++ans; } @@ -21,8 +18,7 @@ class Solution { void dfs(int i, int j, vector>& grid) { grid[i][j] = '0'; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == '1') dfs(x, y, grid); diff --git a/solution/0200-0299/0204.Count Primes/README.md b/solution/0200-0299/0204.Count Primes/README.md index 30db50374b8ef..b9dceb5902f89 100644 --- a/solution/0200-0299/0204.Count Primes/README.md +++ b/solution/0200-0299/0204.Count Primes/README.md @@ -104,12 +104,10 @@ public: int countPrimes(int n) { vector primes(n, true); int ans = 0; - for (int i = 2; i < n; ++i) - { - if (primes[i]) - { + for (int i = 2; i < n; ++i) { + if (primes[i]) { ++ans; - for (int j = i + i; j < n; j += i) primes[j] = false; + for (int j = i; j < n; j += i) primes[j] = false; } } return ans; diff --git a/solution/0200-0299/0204.Count Primes/README_EN.md b/solution/0200-0299/0204.Count Primes/README_EN.md index 0c54193ab0c12..e8f62c745306a 100644 --- a/solution/0200-0299/0204.Count Primes/README_EN.md +++ b/solution/0200-0299/0204.Count Primes/README_EN.md @@ -84,12 +84,10 @@ public: int countPrimes(int n) { vector primes(n, true); int ans = 0; - for (int i = 2; i < n; ++i) - { - if (primes[i]) - { + for (int i = 2; i < n; ++i) { + if (primes[i]) { ++ans; - for (int j = i + i; j < n; j += i) primes[j] = false; + for (int j = i; j < n; j += i) primes[j] = false; } } return ans; diff --git a/solution/0200-0299/0204.Count Primes/Solution.cpp b/solution/0200-0299/0204.Count Primes/Solution.cpp index 4039477039bc1..f903e1dbaeb3d 100644 --- a/solution/0200-0299/0204.Count Primes/Solution.cpp +++ b/solution/0200-0299/0204.Count Primes/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int countPrimes(int n) { vector primes(n, true); int ans = 0; - for (int i = 2; i < n; ++i) - { - if (primes[i]) - { + for (int i = 2; i < n; ++i) { + if (primes[i]) { ++ans; for (int j = i; j < n; j += i) primes[j] = false; } diff --git a/solution/0200-0299/0205.Isomorphic Strings/README.md b/solution/0200-0299/0205.Isomorphic Strings/README.md index 5054347e01b77..d699792318681 100644 --- a/solution/0200-0299/0205.Isomorphic Strings/README.md +++ b/solution/0200-0299/0205.Isomorphic Strings/README.md @@ -137,8 +137,7 @@ public: vector d1(256); vector d2(256); int n = s.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { char a = s[i], b = t[i]; if (d1[a] != d2[b]) return false; d1[a] = d2[b] = i + 1; diff --git a/solution/0200-0299/0205.Isomorphic Strings/README_EN.md b/solution/0200-0299/0205.Isomorphic Strings/README_EN.md index fef10e89d35db..67bf34d039034 100644 --- a/solution/0200-0299/0205.Isomorphic Strings/README_EN.md +++ b/solution/0200-0299/0205.Isomorphic Strings/README_EN.md @@ -114,8 +114,7 @@ public: vector d1(256); vector d2(256); int n = s.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { char a = s[i], b = t[i]; if (d1[a] != d2[b]) return false; d1[a] = d2[b] = i + 1; diff --git a/solution/0200-0299/0205.Isomorphic Strings/Solution.cpp b/solution/0200-0299/0205.Isomorphic Strings/Solution.cpp index 42675e861dd79..201d60e4a2a18 100644 --- a/solution/0200-0299/0205.Isomorphic Strings/Solution.cpp +++ b/solution/0200-0299/0205.Isomorphic Strings/Solution.cpp @@ -4,8 +4,7 @@ class Solution { vector d1(256); vector d2(256); int n = s.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { char a = s[i], b = t[i]; if (d1[a] != d2[b]) return false; d1[a] = d2[b] = i + 1; diff --git a/solution/0200-0299/0206.Reverse Linked List/README.md b/solution/0200-0299/0206.Reverse Linked List/README.md index e1a45e67cdc09..b5208df7c2247 100644 --- a/solution/0200-0299/0206.Reverse Linked List/README.md +++ b/solution/0200-0299/0206.Reverse Linked List/README.md @@ -239,8 +239,7 @@ public: ListNode* reverseList(ListNode* head) { ListNode* dummy = new ListNode(); ListNode* curr = head; - while (curr) - { + while (curr) { ListNode* next = curr->next; curr->next = dummy->next; dummy->next = curr; diff --git a/solution/0200-0299/0206.Reverse Linked List/README_EN.md b/solution/0200-0299/0206.Reverse Linked List/README_EN.md index 898412b39e5a6..973c8849a7f18 100644 --- a/solution/0200-0299/0206.Reverse Linked List/README_EN.md +++ b/solution/0200-0299/0206.Reverse Linked List/README_EN.md @@ -222,8 +222,7 @@ public: ListNode* reverseList(ListNode* head) { ListNode* dummy = new ListNode(); ListNode* curr = head; - while (curr) - { + while (curr) { ListNode* next = curr->next; curr->next = dummy->next; dummy->next = curr; diff --git a/solution/0200-0299/0206.Reverse Linked List/Solution.cpp b/solution/0200-0299/0206.Reverse Linked List/Solution.cpp index 38a0f8ffc9267..6e4899bdfe531 100644 --- a/solution/0200-0299/0206.Reverse Linked List/Solution.cpp +++ b/solution/0200-0299/0206.Reverse Linked List/Solution.cpp @@ -13,8 +13,7 @@ class Solution { ListNode* reverseList(ListNode* head) { ListNode* dummy = new ListNode(); ListNode* curr = head; - while (curr) - { + while (curr) { ListNode* next = curr->next; curr->next = dummy->next; dummy->next = curr; diff --git a/solution/0200-0299/0207.Course Schedule/Solution.cpp b/solution/0200-0299/0207.Course Schedule/Solution.cpp index a56d60e71afd5..b4d2e1e28f0d4 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.cpp +++ b/solution/0200-0299/0207.Course Schedule/Solution.cpp @@ -3,21 +3,21 @@ class Solution { bool canFinish(int numCourses, vector>& prerequisites) { vector> g(numCourses); vector indeg(numCourses); - for (auto& p : prerequisites) - { + for (auto& p : prerequisites) { int a = p[0], b = p[1]; g[b].push_back(a); ++indeg[a]; } queue q; - for (int i = 0; i < numCourses; ++i) if (indeg[i] == 0) q.push(i); + for (int i = 0; i < numCourses; ++i) + if (indeg[i] == 0) q.push(i); int cnt = 0; - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); ++cnt; - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return cnt == numCourses; } diff --git a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.cpp b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.cpp index e69db33d331b0..874449720f66e 100644 --- a/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.cpp +++ b/solution/0200-0299/0208.Implement Trie (Prefix Tree)/Solution.cpp @@ -5,8 +5,7 @@ class Trie { Trie* searchPrefix(string s) { Trie* node = this; - for (char c : s) - { + for (char c : s) { int idx = c - 'a'; if (!node->children[idx]) return nullptr; node = node->children[idx]; @@ -15,24 +14,25 @@ class Trie { } public: - Trie() : children(26), isEnd(false) {} - + Trie() + : children(26) + , isEnd(false) { } + void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { int idx = c - 'a'; if (!node->children[idx]) node->children[idx] = new Trie(); node = node->children[idx]; } node->isEnd = true; } - + bool search(string word) { Trie* node = searchPrefix(word); return node != nullptr && node->isEnd; } - + bool startsWith(string prefix) { Trie* node = searchPrefix(prefix); return node != nullptr; diff --git a/solution/0200-0299/0210.Course Schedule II/Solution.cpp b/solution/0200-0299/0210.Course Schedule II/Solution.cpp index e56514aec406e..ab3af7299602d 100644 --- a/solution/0200-0299/0210.Course Schedule II/Solution.cpp +++ b/solution/0200-0299/0210.Course Schedule II/Solution.cpp @@ -3,21 +3,21 @@ class Solution { vector findOrder(int numCourses, vector>& prerequisites) { vector> g(numCourses); vector indeg(numCourses); - for (auto& p : prerequisites) - { + for (auto& p : prerequisites) { int a = p[0], b = p[1]; g[b].push_back(a); ++indeg[a]; } queue q; - for (int i = 0; i < numCourses; ++i) if (indeg[i] == 0) q.push(i); + for (int i = 0; i < numCourses; ++i) + if (indeg[i] == 0) q.push(i); vector ans; - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); ans.push_back(i); - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return ans.size() == numCourses ? ans : vector(); } diff --git a/solution/0200-0299/0211.Design Add and Search Words Data Structure/Solution.cpp b/solution/0200-0299/0211.Design Add and Search Words Data Structure/Solution.cpp index 49e08ba25ab3e..6dd1c7fe6a537 100644 --- a/solution/0200-0299/0211.Design Add and Search Words Data Structure/Solution.cpp +++ b/solution/0200-0299/0211.Design Add and Search Words Data Structure/Solution.cpp @@ -26,7 +26,8 @@ class WordDictionary { trie* root; public: - WordDictionary() : root(new trie) {} + WordDictionary() + : root(new trie) { } void addWord(string word) { root->insert(word); diff --git a/solution/0200-0299/0212.Word Search II/Solution.cpp b/solution/0200-0299/0212.Word Search II/Solution.cpp index 0f01024e7d404..e42c808717ef1 100644 --- a/solution/0200-0299/0212.Word Search II/Solution.cpp +++ b/solution/0200-0299/0212.Word Search II/Solution.cpp @@ -2,12 +2,13 @@ class Trie { public: vector children; string w; - Trie() : children(26), w("") {} + Trie() + : children(26) + , w("") { } void insert(string& w) { Trie* node = this; - for (char c : w) - { + for (char c : w) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; @@ -36,12 +37,11 @@ class Solution { int idx = board[i][j] - 'a'; if (!node->children[idx]) return; node = node->children[idx]; - if(node->w != "") res.insert(node->w); + if (node->w != "") res.insert(node->w); char c = board[i][j]; board[i][j] = '0'; - - for (int k = 0; k < 4; ++k) - { + + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < board.size() && y >= 0 && y < board[0].size() && board[x][y] != '0') dfs(node, x, y, board, res); } diff --git a/solution/0200-0299/0214.Shortest Palindrome/Solution.cpp b/solution/0200-0299/0214.Shortest Palindrome/Solution.cpp index 135b9160ce1e0..46282965b1d57 100644 --- a/solution/0200-0299/0214.Shortest Palindrome/Solution.cpp +++ b/solution/0200-0299/0214.Shortest Palindrome/Solution.cpp @@ -8,8 +8,7 @@ class Solution { ull prefix = 0; ull suffix = 0; int idx = 0, n = s.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int t = s[i] - 'a' + 1; prefix = prefix * base + t; suffix = suffix + mul * t; diff --git a/solution/0200-0299/0215.Kth Largest Element in an Array/Solution.cpp b/solution/0200-0299/0215.Kth Largest Element in an Array/Solution.cpp index 5f514fead2fe4..9413e80e60e65 100644 --- a/solution/0200-0299/0215.Kth Largest Element in an Array/Solution.cpp +++ b/solution/0200-0299/0215.Kth Largest Element in an Array/Solution.cpp @@ -9,10 +9,11 @@ class Solution { if (left == right) return nums[left]; int i = left - 1, j = right + 1; int x = nums[left + right >> 1]; - while (i < j) - { - while (nums[++i] < x); - while (nums[--j] > x); + while (i < j) { + while (nums[++i] < x) + ; + while (nums[--j] > x) + ; if (i < j) swap(nums[i], nums[j]); } return j < k ? quickSort(nums, j + 1, right, k) : quickSort(nums, left, j, k); diff --git a/solution/0200-0299/0216.Combination Sum III/Solution.cpp b/solution/0200-0299/0216.Combination Sum III/Solution.cpp index b0f88efa6faba..2ee3b681c2315 100644 --- a/solution/0200-0299/0216.Combination Sum III/Solution.cpp +++ b/solution/0200-0299/0216.Combination Sum III/Solution.cpp @@ -10,8 +10,7 @@ class Solution { void dfs(int i, int n, int k, vector& t) { if (i > 9 || n < 0 || t.size() > k) return; - if (n == 0 && t.size() == k) - { + if (n == 0 && t.size() == k) { ans.push_back(t); return; } diff --git a/solution/0200-0299/0217.Contains Duplicate/Solution.cpp b/solution/0200-0299/0217.Contains Duplicate/Solution.cpp index 128717ec5bd60..4526e8ea3de42 100644 --- a/solution/0200-0299/0217.Contains Duplicate/Solution.cpp +++ b/solution/0200-0299/0217.Contains Duplicate/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool containsDuplicate(vector& nums) { unordered_set s; - for (int e : nums) - { + for (int e : nums) { if (s.count(e)) return true; s.insert(e); } diff --git a/solution/0200-0299/0218.The Skyline Problem/Solution.cpp b/solution/0200-0299/0218.The Skyline Problem/Solution.cpp index a22db24dabf02..41697fff4c8d3 100644 --- a/solution/0200-0299/0218.The Skyline Problem/Solution.cpp +++ b/solution/0200-0299/0218.The Skyline Problem/Solution.cpp @@ -2,40 +2,36 @@ class Solution { public: vector> getSkyline(vector>& buildings) { - set poss ; - map m ; - for (auto v: buildings) - { - poss.insert(v[0]) ; - poss.insert(v[1]) ; + set poss; + map m; + for (auto v : buildings) { + poss.insert(v[0]); + poss.insert(v[1]); } - - int i = 0 ; - for (int pos: poss) - m.insert(pair(pos, i++)) ; - - vector highs(m.size(), 0) ; - for (auto v: buildings) - { - const int b = m[v[0]], e = m[v[1]] ; + + int i = 0; + for (int pos : poss) + m.insert(pair(pos, i++)); + + vector highs(m.size(), 0); + for (auto v : buildings) { + const int b = m[v[0]], e = m[v[1]]; for (int i = b; i < e; ++i) - highs[i] = max(highs[i], v[2]) ; + highs[i] = max(highs[i], v[2]); } - - vector> res ; - vector mm(poss.begin(), poss.end()) ; - for (int i = 0; i < highs.size(); ++i) - { - if (highs[i] != highs[i+1]) - res.push_back(pair(mm[i], highs[i])) ; - else - { - const int start = i ; - res.push_back(pair(mm[start], highs[i])) ; - while (highs[i] == highs[i+1]) - ++i ; + + vector> res; + vector mm(poss.begin(), poss.end()); + for (int i = 0; i < highs.size(); ++i) { + if (highs[i] != highs[i + 1]) + res.push_back(pair(mm[i], highs[i])); + else { + const int start = i; + res.push_back(pair(mm[start], highs[i])); + while (highs[i] == highs[i + 1]) + ++i; } } - return res ; + return res; } }; diff --git a/solution/0200-0299/0219.Contains Duplicate II/Solution.cpp b/solution/0200-0299/0219.Contains Duplicate II/Solution.cpp index 50090d43206b2..8f3f4ab2bafdb 100644 --- a/solution/0200-0299/0219.Contains Duplicate II/Solution.cpp +++ b/solution/0200-0299/0219.Contains Duplicate II/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool containsNearbyDuplicate(vector& nums, int k) { unordered_map mp; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { if (mp.count(nums[i]) && i - mp[nums[i]] <= k) return true; mp[nums[i]] = i; } diff --git a/solution/0200-0299/0220.Contains Duplicate III/Solution.cpp b/solution/0200-0299/0220.Contains Duplicate III/Solution.cpp index 7eb786c64d7b0..cf409f482a57d 100644 --- a/solution/0200-0299/0220.Contains Duplicate III/Solution.cpp +++ b/solution/0200-0299/0220.Contains Duplicate III/Solution.cpp @@ -2,12 +2,11 @@ class Solution { public: bool containsNearbyAlmostDuplicate(vector& nums, int k, int t) { set s; - for (int i = 0; i < nums.size(); ++i) - { - auto it = s.lower_bound((long) nums[i] - t); - if (it != s.end() && *it <= (long) nums[i] + t) return true; - s.insert((long) nums[i]); - if (i >= k) s.erase((long) nums[i - k]); + for (int i = 0; i < nums.size(); ++i) { + auto it = s.lower_bound((long)nums[i] - t); + if (it != s.end() && *it <= (long)nums[i] + t) return true; + s.insert((long)nums[i]); + if (i >= k) s.erase((long)nums[i - k]); } return false; } diff --git a/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.cpp b/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.cpp index a2b6cb69171ac..0c4e1aaa23f51 100644 --- a/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.cpp +++ b/solution/0200-0299/0222.Count Complete Tree Nodes/Solution.cpp @@ -21,7 +21,8 @@ class Solution { int depth(TreeNode* root) { int res = 0; - for (; root != nullptr; ++res, root = root->left); + for (; root != nullptr; ++res, root = root->left) + ; return res; } }; \ No newline at end of file diff --git a/solution/0200-0299/0229.Majority Element II/Solution.cpp b/solution/0200-0299/0229.Majority Element II/Solution.cpp index 9074c8a488950..161aa0d4968db 100644 --- a/solution/0200-0299/0229.Majority Element II/Solution.cpp +++ b/solution/0200-0299/0229.Majority Element II/Solution.cpp @@ -3,22 +3,18 @@ class Solution { vector majorityElement(vector& nums) { int n1 = 0, n2 = 0; int m1 = 0, m2 = 1; - for (int m : nums) - { - if (m == m1) ++n1; - else if (m == m2) ++n2; - else if (n1 == 0) - { + for (int m : nums) { + if (m == m1) + ++n1; + else if (m == m2) + ++n2; + else if (n1 == 0) { m1 = m; ++n1; - } - else if (n2 == 0) - { + } else if (n2 == 0) { m2 = m; ++n2; - } - else - { + } else { --n1; --n2; } diff --git a/solution/0200-0299/0230.Kth Smallest Element in a BST/Solution.cpp b/solution/0200-0299/0230.Kth Smallest Element in a BST/Solution.cpp index 8694e297889d0..ea75faef3cc18 100644 --- a/solution/0200-0299/0230.Kth Smallest Element in a BST/Solution.cpp +++ b/solution/0200-0299/0230.Kth Smallest Element in a BST/Solution.cpp @@ -13,15 +13,11 @@ class Solution { public: int kthSmallest(TreeNode* root, int k) { stack stk; - while (root || !stk.empty()) - { - if (root) - { + while (root || !stk.empty()) { + if (root) { stk.push(root); root = root->left; - } - else - { + } else { root = stk.top(); stk.pop(); if (--k == 0) return root->val; diff --git a/solution/0200-0299/0234.Palindrome Linked List/Solution.cpp b/solution/0200-0299/0234.Palindrome Linked List/Solution.cpp index 2e59862d01ab4..04c0173e6011a 100644 --- a/solution/0200-0299/0234.Palindrome Linked List/Solution.cpp +++ b/solution/0200-0299/0234.Palindrome Linked List/Solution.cpp @@ -14,22 +14,19 @@ class Solution { if (!head || !head->next) return true; ListNode* slow = head; ListNode* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } ListNode* pre = nullptr; ListNode* cur = slow->next; - while (cur) - { + while (cur) { ListNode* t = cur->next; cur->next = pre; pre = cur; cur = t; } - while (pre) - { + while (pre) { if (pre->val != head->val) return false; pre = pre->next; head = head->next; diff --git a/solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.cpp b/solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.cpp index 1524e9634cff6..5a2ceb9160d36 100644 --- a/solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.cpp +++ b/solution/0200-0299/0235.Lowest Common Ancestor of a Binary Search Tree/Solution.cpp @@ -11,11 +11,13 @@ class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) { - while (root) - { - if (root->val < p->val && root->val < q->val) root = root->right; - else if (root->val > p->val && root->val > q->val) root = root->left; - else return root; + while (root) { + if (root->val < p->val && root->val < q->val) + root = root->right; + else if (root->val > p->val && root->val > q->val) + root = root->left; + else + return root; } return root; } diff --git a/solution/0200-0299/0238.Product of Array Except Self/Solution.cpp b/solution/0200-0299/0238.Product of Array Except Self/Solution.cpp index db65b5837ccea..b60ba51b7d814 100644 --- a/solution/0200-0299/0238.Product of Array Except Self/Solution.cpp +++ b/solution/0200-0299/0238.Product of Array Except Self/Solution.cpp @@ -3,13 +3,11 @@ class Solution { vector productExceptSelf(vector& nums) { int n = nums.size(); vector ans(n); - for (int i = 0, left = 1; i < n; ++i) - { + for (int i = 0, left = 1; i < n; ++i) { ans[i] = left; left *= nums[i]; } - for (int i = n - 1, right = 1; i >= 0; --i) - { + for (int i = n - 1, right = 1; i >= 0; --i) { ans[i] *= right; right *= nums[i]; } diff --git a/solution/0200-0299/0239.Sliding Window Maximum/Solution.cpp b/solution/0200-0299/0239.Sliding Window Maximum/Solution.cpp index 96c214b8cbd78..96745a7bccce6 100644 --- a/solution/0200-0299/0239.Sliding Window Maximum/Solution.cpp +++ b/solution/0200-0299/0239.Sliding Window Maximum/Solution.cpp @@ -3,8 +3,7 @@ class Solution { vector maxSlidingWindow(vector& nums, int k) { deque q; vector ans; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { if (!q.empty() && i - k + 1 > q.front()) q.pop_front(); while (!q.empty() && nums[q.back()] <= nums[i]) q.pop_back(); q.push_back(i); diff --git a/solution/0200-0299/0240.Search a 2D Matrix II/Solution.cpp b/solution/0200-0299/0240.Search a 2D Matrix II/Solution.cpp index 87f00ba963a8d..e33f992add84e 100644 --- a/solution/0200-0299/0240.Search a 2D Matrix II/Solution.cpp +++ b/solution/0200-0299/0240.Search a 2D Matrix II/Solution.cpp @@ -3,11 +3,12 @@ class Solution { bool searchMatrix(vector>& matrix, int target) { int m = matrix.size(), n = matrix[0].size(); int i = m - 1, j = 0; - while (i >= 0 && j < n) - { + while (i >= 0 && j < n) { if (matrix[i][j] == target) return true; - if (matrix[i][j] > target) --i; - else ++j; + if (matrix[i][j] > target) + --i; + else + ++j; } return false; } diff --git a/solution/0200-0299/0241.Different Ways to Add Parentheses/Solution.cpp b/solution/0200-0299/0241.Different Ways to Add Parentheses/Solution.cpp index db5d7878e9e72..1f0e76f8470d6 100644 --- a/solution/0200-0299/0241.Different Ways to Add Parentheses/Solution.cpp +++ b/solution/0200-0299/0241.Different Ways to Add Parentheses/Solution.cpp @@ -9,20 +9,19 @@ class Solution { if (exp.size() < 3) return {stoi(exp)}; vector ans; int n = exp.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { char c = exp[i]; - if (c == '-' || c == '+' || c == '*') - { + if (c == '-' || c == '+' || c == '*') { vector left = dfs(exp.substr(0, i)); vector right = dfs(exp.substr(i + 1, n - i - 1)); - for (int& a : left) - { - for (int& b : right) - { - if (c == '-') ans.push_back(a - b); - else if (c == '+') ans.push_back(a + b); - else ans.push_back(a * b); + for (int& a : left) { + for (int& b : right) { + if (c == '-') + ans.push_back(a - b); + else if (c == '+') + ans.push_back(a + b); + else + ans.push_back(a * b); } } } diff --git a/solution/0200-0299/0242.Valid Anagram/Solution.cpp b/solution/0200-0299/0242.Valid Anagram/Solution.cpp index a101f722d1d8b..1de3066f3166d 100644 --- a/solution/0200-0299/0242.Valid Anagram/Solution.cpp +++ b/solution/0200-0299/0242.Valid Anagram/Solution.cpp @@ -4,13 +4,11 @@ class Solution { if (s.size() != t.size()) return false; vector chars(26, 0); - for (int i = 0, n = s.size(); i < n; ++i) - { + for (int i = 0, n = s.size(); i < n; ++i) { ++chars[s[i] - 'a']; --chars[t[i] - 'a']; } - for (int c : chars) - { + for (int c : chars) { if (c != 0) return false; } diff --git a/solution/0200-0299/0247.Strobogrammatic Number II/Solution.cpp b/solution/0200-0299/0247.Strobogrammatic Number II/Solution.cpp index f5889b211a8a6..649668794b9ad 100644 --- a/solution/0200-0299/0247.Strobogrammatic Number II/Solution.cpp +++ b/solution/0200-0299/0247.Strobogrammatic Number II/Solution.cpp @@ -11,8 +11,7 @@ class Solution { if (u == 1) return {"0", "1", "8"}; vector ans; vector> pairs = {{'1', '1'}, {'8', '8'}, {'6', '9'}, {'9', '6'}}; - for (string v : dfs(u - 2)) - { + for (string v : dfs(u - 2)) { for (auto& p : pairs) ans.push_back({p[0] + v + p[1]}); if (u != n) ans.push_back('0' + v + '0'); } diff --git a/solution/0200-0299/0249.Group Shifted Strings/Solution.cpp b/solution/0200-0299/0249.Group Shifted Strings/Solution.cpp index 4e96c7e8e4bfd..78526a5bb3127 100644 --- a/solution/0200-0299/0249.Group Shifted Strings/Solution.cpp +++ b/solution/0200-0299/0249.Group Shifted Strings/Solution.cpp @@ -2,12 +2,10 @@ class Solution { public: vector> groupStrings(vector& strings) { unordered_map> mp; - for (auto& s : strings) - { + for (auto& s : strings) { int diff = s[0] - 'a'; string t = s; - for (int i = 0; i < t.size(); ++i) - { + for (int i = 0; i < t.size(); ++i) { char d = t[i] - diff; if (d < 'a') d += 26; t[i] = d; diff --git a/solution/0200-0299/0255.Verify Preorder Sequence in Binary Search Tree/Solution.cpp b/solution/0200-0299/0255.Verify Preorder Sequence in Binary Search Tree/Solution.cpp index d4a19fbea7aa0..bf05ca2e45dc1 100644 --- a/solution/0200-0299/0255.Verify Preorder Sequence in Binary Search Tree/Solution.cpp +++ b/solution/0200-0299/0255.Verify Preorder Sequence in Binary Search Tree/Solution.cpp @@ -3,11 +3,9 @@ class Solution { bool verifyPreorder(vector& preorder) { stack stk; int last = INT_MIN; - for (int x : preorder) - { + for (int x : preorder) { if (x < last) return false; - while (!stk.empty() && stk.top() < x) - { + while (!stk.empty() && stk.top() < x) { last = stk.top(); stk.pop(); } diff --git a/solution/0200-0299/0257.Binary Tree Paths/Solution.cpp b/solution/0200-0299/0257.Binary Tree Paths/Solution.cpp index 76e57d14c96d5..0837e2332eb53 100644 --- a/solution/0200-0299/0257.Binary Tree Paths/Solution.cpp +++ b/solution/0200-0299/0257.Binary Tree Paths/Solution.cpp @@ -20,8 +20,7 @@ class Solution { void dfs(TreeNode* root, string t) { t += to_string(root->val); - if (!root->left && !root->right) - { + if (!root->left && !root->right) { ans.push_back(t); return; } diff --git a/solution/0200-0299/0259.3Sum Smaller/Solution.cpp b/solution/0200-0299/0259.3Sum Smaller/Solution.cpp index 9e6489ee0b753..329b31aa296f2 100644 --- a/solution/0200-0299/0259.3Sum Smaller/Solution.cpp +++ b/solution/0200-0299/0259.3Sum Smaller/Solution.cpp @@ -3,15 +3,13 @@ class Solution { int threeSumSmaller(vector& nums, int target) { sort(nums.begin(), nums.end()); int ans = 0; - for (int i = 0, n = nums.size(); i < n; ++i) - { + for (int i = 0, n = nums.size(); i < n; ++i) { int j = i + 1, k = n - 1; - while (j < k) - { + while (j < k) { int s = nums[i] + nums[j] + nums[k]; - if (s >= target) --k; - else - { + if (s >= target) + --k; + else { ans += k - j; ++j; } diff --git a/solution/0200-0299/0261.Graph Valid Tree/Solution.cpp b/solution/0200-0299/0261.Graph Valid Tree/Solution.cpp index b9ac9f7af7a01..a04f5d7e0eef4 100644 --- a/solution/0200-0299/0261.Graph Valid Tree/Solution.cpp +++ b/solution/0200-0299/0261.Graph Valid Tree/Solution.cpp @@ -5,8 +5,7 @@ class Solution { bool validTree(int n, vector>& edges) { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; if (find(a) == find(b)) return 0; p[find(a)] = find(b); diff --git a/solution/0200-0299/0269.Alien Dictionary/Solution.cpp b/solution/0200-0299/0269.Alien Dictionary/Solution.cpp index 16fd0e7386603..b0089714f20e8 100644 --- a/solution/0200-0299/0269.Alien Dictionary/Solution.cpp +++ b/solution/0200-0299/0269.Alien Dictionary/Solution.cpp @@ -5,21 +5,17 @@ class Solution { vector s(26); int cnt = 0; int n = words.size(); - for (int i = 0; i < n - 1; ++i) - { - for (char c : words[i]) - { + for (int i = 0; i < n - 1; ++i) { + for (char c : words[i]) { if (cnt == 26) break; c -= 'a'; - if (!s[c]) - { + if (!s[c]) { ++cnt; s[c] = true; } } int m = words[i].size(); - for (int j = 0; j < m; ++j) - { + for (int j = 0; j < m; ++j) { if (j >= words[i + 1].size()) return ""; char c1 = words[i][j], c2 = words[i + 1][j]; if (c1 == c2) continue; @@ -28,12 +24,10 @@ class Solution { break; } } - for (char c : words[n - 1]) - { + for (char c : words[n - 1]) { if (cnt == 26) break; c -= 'a'; - if (!s[c]) - { + if (!s[c]) { ++cnt; s[c] = true; } @@ -48,8 +42,7 @@ class Solution { if (s[i] && indegree[i] == 0) q.push(i); string ans = ""; - while (!q.empty()) - { + while (!q.empty()) { int t = q.front(); ans += (t + 'a'); q.pop(); diff --git a/solution/0200-0299/0270.Closest Binary Search Tree Value/Solution.cpp b/solution/0200-0299/0270.Closest Binary Search Tree Value/Solution.cpp index e8204c56e293e..a6098344e028e 100644 --- a/solution/0200-0299/0270.Closest Binary Search Tree Value/Solution.cpp +++ b/solution/0200-0299/0270.Closest Binary Search Tree Value/Solution.cpp @@ -14,16 +14,16 @@ class Solution { int closestValue(TreeNode* root, double target) { int ans = root->val; double mi = INT_MAX; - while (root) - { + while (root) { double t = abs(root->val - target); - if (t < mi) - { + if (t < mi) { mi = t; ans = root->val; } - if (root->val > target) root = root->left; - else root = root->right; + if (root->val > target) + root = root->left; + else + root = root->right; } return ans; } diff --git a/solution/0200-0299/0272.Closest Binary Search Tree Value II/Solution.cpp b/solution/0200-0299/0272.Closest Binary Search Tree Value II/Solution.cpp index c6da117aab687..00ab2d0f5750c 100644 --- a/solution/0200-0299/0272.Closest Binary Search Tree Value II/Solution.cpp +++ b/solution/0200-0299/0272.Closest Binary Search Tree Value II/Solution.cpp @@ -20,8 +20,7 @@ class Solution { this->k = k; dfs(root); vector ans; - while (!q.empty()) - { + while (!q.empty()) { ans.push_back(q.front()); q.pop(); } @@ -31,9 +30,9 @@ class Solution { void dfs(TreeNode* root) { if (!root) return; dfs(root->left); - if (q.size() < k) q.push(root->val); - else - { + if (q.size() < k) + q.push(root->val); + else { if (abs(root->val - target) >= abs(q.front() - target)) return; q.pop(); q.push(root->val); diff --git a/solution/0200-0299/0275.H-Index II/Solution.cpp b/solution/0200-0299/0275.H-Index II/Solution.cpp index 37d23965089c3..9a789bd45fa04 100644 --- a/solution/0200-0299/0275.H-Index II/Solution.cpp +++ b/solution/0200-0299/0275.H-Index II/Solution.cpp @@ -3,11 +3,12 @@ class Solution { int hIndex(vector& citations) { int n = citations.size(); int left = 0, right = n; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; - if (citations[n - mid] >= mid) left = mid; - else right = mid - 1; + if (citations[n - mid] >= mid) + left = mid; + else + right = mid - 1; } return left; } diff --git a/solution/0200-0299/0283.Move Zeroes/Solution.cpp b/solution/0200-0299/0283.Move Zeroes/Solution.cpp index f3bcb4c422168..f4a1cb31019f9 100644 --- a/solution/0200-0299/0283.Move Zeroes/Solution.cpp +++ b/solution/0200-0299/0283.Move Zeroes/Solution.cpp @@ -3,8 +3,7 @@ class Solution { void moveZeroes(vector& nums) { int left = 0, n = nums.size(); for (int right = 0; right < n; ++right) { - if (nums[right] != 0) - { + if (nums[right] != 0) { swap(nums[left], nums[right]); ++left; } diff --git a/solution/0200-0299/0284.Peeking Iterator/Solution.cpp b/solution/0200-0299/0284.Peeking Iterator/Solution.cpp index a2143efe90da3..61dba4534cafb 100644 --- a/solution/0200-0299/0284.Peeking Iterator/Solution.cpp +++ b/solution/0200-0299/0284.Peeking Iterator/Solution.cpp @@ -19,34 +19,35 @@ class PeekingIterator : public Iterator { public: - PeekingIterator(const vector& nums) : Iterator(nums) { - // Initialize any member here. - // **DO NOT** save a copy of nums and manipulate it directly. - // You should only use the Iterator interface methods. - hasPeeked = false; - } - + PeekingIterator(const vector& nums) + : Iterator(nums) { + // Initialize any member here. + // **DO NOT** save a copy of nums and manipulate it directly. + // You should only use the Iterator interface methods. + hasPeeked = false; + } + // Returns the next element in the iteration without advancing the iterator. - int peek() { - if (!hasPeeked) - { + int peek() { + if (!hasPeeked) { peekedElement = Iterator::next(); hasPeeked = true; } return peekedElement; - } - - // hasNext() and next() should behave the same as in the Iterator interface. - // Override them if needed. - int next() { - if (!hasPeeked) return Iterator::next(); + } + + // hasNext() and next() should behave the same as in the Iterator interface. + // Override them if needed. + int next() { + if (!hasPeeked) return Iterator::next(); hasPeeked = false; return peekedElement; - } - - bool hasNext() const { - return hasPeeked || Iterator::hasNext(); - } + } + + bool hasNext() const { + return hasPeeked || Iterator::hasNext(); + } + private: bool hasPeeked; int peekedElement; diff --git a/solution/0200-0299/0286.Walls and Gates/Solution.cpp b/solution/0200-0299/0286.Walls and Gates/Solution.cpp index 0633c26230f35..8ff0b33e30304 100644 --- a/solution/0200-0299/0286.Walls and Gates/Solution.cpp +++ b/solution/0200-0299/0286.Walls and Gates/Solution.cpp @@ -10,19 +10,15 @@ class Solution { q.emplace(i, j); int d = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { ++d; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { auto p = q.front(); q.pop(); - for (int j = 0; j < 4; ++j) - { + for (int j = 0; j < 4; ++j) { int x = p.first + dirs[j]; int y = p.second + dirs[j + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && rooms[x][y] == INT_MAX) - { + if (x >= 0 && x < m && y >= 0 && y < n && rooms[x][y] == INT_MAX) { rooms[x][y] = d; q.emplace(x, y); } diff --git a/solution/0200-0299/0287.Find the Duplicate Number/Solution.cpp b/solution/0200-0299/0287.Find the Duplicate Number/Solution.cpp index 1bc12c3ffd288..b677ec7ae1ab8 100644 --- a/solution/0200-0299/0287.Find the Duplicate Number/Solution.cpp +++ b/solution/0200-0299/0287.Find the Duplicate Number/Solution.cpp @@ -2,15 +2,16 @@ class Solution { public: int findDuplicate(vector& nums) { int left = 1, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int cnt = 0; for (int& v : nums) if (v <= mid) ++cnt; - if (cnt > mid) right = mid; - else left = mid + 1; + if (cnt > mid) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0200-0299/0288.Unique Word Abbreviation/Solution.cpp b/solution/0200-0299/0288.Unique Word Abbreviation/Solution.cpp index daf7d1670253d..fe386370afe2d 100644 --- a/solution/0200-0299/0288.Unique Word Abbreviation/Solution.cpp +++ b/solution/0200-0299/0288.Unique Word Abbreviation/Solution.cpp @@ -3,13 +3,12 @@ class ValidWordAbbr { unordered_map> words; ValidWordAbbr(vector& dictionary) { - for (auto word : dictionary) - { + for (auto word : dictionary) { auto abbr = wordAbbr(word); words[abbr].insert(word); } } - + bool isUnique(string word) { auto abbr = wordAbbr(word); if (!words.count(abbr)) return true; diff --git a/solution/0200-0299/0289.Game of Life/Solution.cpp b/solution/0200-0299/0289.Game of Life/Solution.cpp index 8f423e039f464..f39dfab942b6c 100644 --- a/solution/0200-0299/0289.Game of Life/Solution.cpp +++ b/solution/0200-0299/0289.Game of Life/Solution.cpp @@ -2,19 +2,18 @@ class Solution { public: void gameOfLife(vector>& board) { int m = board.size(), n = board[0].size(); - vector> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + vector> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int cnt = 0; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int x = i + dir[0], y = j + dir[1]; if (x >= 0 && x < m && y >= 0 && y < n && (board[x][y] == 1 || board[x][y] == 2)) ++cnt; } - if (board[i][j] == 1 && (cnt < 2 || cnt > 3)) board[i][j] = 2; - else if (board[i][j] == 0 && cnt == 3) board[i][j] = 3; + if (board[i][j] == 1 && (cnt < 2 || cnt > 3)) + board[i][j] = 2; + else if (board[i][j] == 0 && cnt == 3) + board[i][j] = 3; } } for (int i = 0; i < m; ++i) diff --git a/solution/0200-0299/0290.Word Pattern/Solution.cpp b/solution/0200-0299/0290.Word Pattern/Solution.cpp index 13a8ed23af128..30d90570b81e7 100644 --- a/solution/0200-0299/0290.Word Pattern/Solution.cpp +++ b/solution/0200-0299/0290.Word Pattern/Solution.cpp @@ -9,8 +9,7 @@ class Solution { unordered_map c2str; unordered_map str2c; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { char k = pattern[i]; string v = ss[i]; if (c2str.count(k) && c2str[k] != v) return false; diff --git a/solution/0200-0299/0291.Word Pattern II/Solution.cpp b/solution/0200-0299/0291.Word Pattern II/Solution.cpp index ed4a2f3f005a0..19573e106797f 100644 --- a/solution/0200-0299/0291.Word Pattern II/Solution.cpp +++ b/solution/0200-0299/0291.Word Pattern II/Solution.cpp @@ -11,15 +11,12 @@ class Solution { if (i == m && j == n) return true; if (i == m || j == n || m - i > n - j) return false; char c = p[i]; - for (int k = j + 1; k <= n; ++k) - { + for (int k = j + 1; k <= n; ++k) { string t = s.substr(j, k - j); - if (d.count(c) && d[c] == t) - { + if (d.count(c) && d[c] == t) { if (dfs(i + 1, k, p, s, vis, d)) return true; } - if (!d.count(c) && !vis.count(t)) - { + if (!d.count(c) && !vis.count(t)) { d[c] = t; vis.insert(t); if (dfs(i + 1, k, p, s, vis, d)) return true; diff --git a/solution/0200-0299/0293.Flip Game/Solution.cpp b/solution/0200-0299/0293.Flip Game/Solution.cpp index c3315f4da81e8..3f2a37dc6e91e 100644 --- a/solution/0200-0299/0293.Flip Game/Solution.cpp +++ b/solution/0200-0299/0293.Flip Game/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: vector generatePossibleNextMoves(string currentState) { vector ans; - for (int i = 0; i < currentState.size() - 1; ++i) - { - if (currentState[i] == '+' && currentState[i + 1] == '+') - { + for (int i = 0; i < currentState.size() - 1; ++i) { + if (currentState[i] == '+' && currentState[i + 1] == '+') { currentState[i] = '-'; currentState[i + 1] = '-'; ans.push_back(currentState); diff --git a/solution/0200-0299/0294.Flip Game II/Solution.cpp b/solution/0200-0299/0294.Flip Game II/Solution.cpp index 3b79118634171..09ed439864c22 100644 --- a/solution/0200-0299/0294.Flip Game II/Solution.cpp +++ b/solution/0200-0299/0294.Flip Game II/Solution.cpp @@ -8,14 +8,14 @@ class Solution { bool canWin(string currentState) { n = currentState.size(); ll mask = 0; - for (int i = 0; i < n; ++i) if (currentState[i] == '+') mask |= 1ll << i; + for (int i = 0; i < n; ++i) + if (currentState[i] == '+') mask |= 1ll << i; return dfs(mask); } bool dfs(ll mask) { if (memo.count(mask)) return memo[mask]; - for (int i = 0; i < n - 1; ++i) - { + for (int i = 0; i < n - 1; ++i) { if ((mask & (1ll << i)) == 0 || (mask & (1ll << (i + 1))) == 0) continue; if (dfs(mask ^ (1ll << i) ^ (1ll << (i + 1)))) continue; memo[mask] = true; diff --git a/solution/0200-0299/0295.Find Median from Data Stream/Solution.cpp b/solution/0200-0299/0295.Find Median from Data Stream/Solution.cpp index d288c0294113a..55ecc9460b997 100644 --- a/solution/0200-0299/0295.Find Median from Data Stream/Solution.cpp +++ b/solution/0200-0299/0295.Find Median from Data Stream/Solution.cpp @@ -1,7 +1,7 @@ class MedianFinder { public: /** initialize your data structure here. */ - MedianFinder() {} + MedianFinder() { } void addNum(int num) { max_heap.push(num); diff --git a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.cpp b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.cpp index 3eb98b9b42343..a55345bc92edf 100644 --- a/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.cpp +++ b/solution/0200-0299/0297.Serialize and Deserialize Binary Tree/Solution.cpp @@ -9,7 +9,6 @@ */ class Codec { public: - // Encodes a tree to a single string. string serialize(TreeNode* root) { if (!root) return ""; @@ -19,9 +18,9 @@ class Codec { } void preorder(TreeNode* root, string& s) { - if (!root) s += "# "; - else - { + if (!root) + s += "# "; + else { s += to_string(root->val) + " "; preorder(root->left, s); preorder(root->right, s); @@ -49,4 +48,3 @@ class Codec { // Your Codec object will be instantiated and called as such: // Codec ser, deser; // TreeNode* ans = deser.deserialize(ser.serialize(root)); - diff --git a/solution/0200-0299/0298.Binary Tree Longest Consecutive Sequence/Solution.cpp b/solution/0200-0299/0298.Binary Tree Longest Consecutive Sequence/Solution.cpp index 310454ab2e56e..bbc940278b9b5 100644 --- a/solution/0200-0299/0298.Binary Tree Longest Consecutive Sequence/Solution.cpp +++ b/solution/0200-0299/0298.Binary Tree Longest Consecutive Sequence/Solution.cpp @@ -21,7 +21,7 @@ class Solution { void dfs(TreeNode* root, TreeNode* p, int t) { if (!root) return; - t = p != nullptr && p->val + 1 == root-> val ? t + 1 : 1; + t = p != nullptr && p->val + 1 == root->val ? t + 1 : 1; ans = max(ans, t); dfs(root->left, root, t); dfs(root->right, root, t); diff --git a/solution/0200-0299/0299.Bulls and Cows/Solution.cpp b/solution/0200-0299/0299.Bulls and Cows/Solution.cpp index 89292e4035820..cf9f2c9692e07 100644 --- a/solution/0200-0299/0299.Bulls and Cows/Solution.cpp +++ b/solution/0200-0299/0299.Bulls and Cows/Solution.cpp @@ -4,12 +4,11 @@ class Solution { int x = 0, y = 0; vector cnt1(10); vector cnt2(10); - for (int i = 0; i < secret.size(); ++i) - { + for (int i = 0; i < secret.size(); ++i) { int a = secret[i] - '0', b = guess[i] - '0'; - if (a == b) ++x; - else - { + if (a == b) + ++x; + else { ++cnt1[a]; ++cnt2[b]; } diff --git a/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.cpp b/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.cpp index f77e0cd34fec6..6ceaa76de3555 100644 --- a/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.cpp +++ b/solution/0300-0399/0300.Longest Increasing Subsequence/Solution.cpp @@ -2,12 +2,11 @@ class Solution { public: int lengthOfLIS(vector& nums) { int n = nums.size(); - vector d{nums[0]}; - for (int i = 1; i < n; ++i) - { - if (nums[i] > d[d.size() - 1]) d.push_back(nums[i]); - else - { + vector d {nums[0]}; + for (int i = 1; i < n; ++i) { + if (nums[i] > d[d.size() - 1]) + d.push_back(nums[i]); + else { int idx = lower_bound(d.begin(), d.end(), nums[i]) - d.begin(); if (idx == d.size()) idx = 0; d[idx] = nums[i]; diff --git a/solution/0300-0399/0301.Remove Invalid Parentheses/Solution.cpp b/solution/0300-0399/0301.Remove Invalid Parentheses/Solution.cpp index 2e70a59bb4b13..43aec343313a2 100644 --- a/solution/0300-0399/0301.Remove Invalid Parentheses/Solution.cpp +++ b/solution/0300-0399/0301.Remove Invalid Parentheses/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: vector removeInvalidParentheses(string s) { int ldel = 0, rdel = 0; - for (char c : s) - { - if (c == '(') ++ldel; - else if (c == ')') - { - if (ldel == 0) ++rdel; - else --ldel; + for (char c : s) { + if (c == '(') + ++ldel; + else if (c == ')') { + if (ldel == 0) + ++rdel; + else + --ldel; } } int tdel = ldel + rdel; @@ -21,23 +22,17 @@ class Solution { void dfs(int i, string t, string s, int lcnt, int rcnt, int ldel, int rdel, int tdel, unordered_set& ans) { if (ldel * rdel < 0 || lcnt < rcnt || ldel + rdel > s.size() - i) return; - if (ldel == 0 && rdel == 0) - { + if (ldel == 0 && rdel == 0) { if (s.size() - t.size() == tdel) ans.insert(t); } if (i == s.size()) return; - if (s[i] == '(') - { + if (s[i] == '(') { dfs(i + 1, t, s, lcnt, rcnt, ldel - 1, rdel, tdel, ans); dfs(i + 1, t + s[i], s, lcnt + 1, rcnt, ldel, rdel, tdel, ans); - } - else if (s[i] == ')') - { + } else if (s[i] == ')') { dfs(i + 1, t, s, lcnt, rcnt, ldel, rdel - 1, tdel, ans); dfs(i + 1, t + s[i], s, lcnt, rcnt + 1, ldel, rdel, tdel, ans); - } - else - { + } else { dfs(i + 1, t + s[i], s, lcnt, rcnt, ldel, rdel, tdel, ans); } } diff --git a/solution/0300-0399/0302.Smallest Rectangle Enclosing Black Pixels/Solution.cpp b/solution/0300-0399/0302.Smallest Rectangle Enclosing Black Pixels/Solution.cpp index ca594c30d282a..7ec06d241bb38 100644 --- a/solution/0300-0399/0302.Smallest Rectangle Enclosing Black Pixels/Solution.cpp +++ b/solution/0300-0399/0302.Smallest Rectangle Enclosing Black Pixels/Solution.cpp @@ -3,46 +3,50 @@ class Solution { int minArea(vector>& image, int x, int y) { int m = image.size(), n = image[0].size(); int left = 0, right = x; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int c = 0; while (c < n && image[mid][c] == '0') ++c; - if (c < n) right = mid; - else left = mid + 1; + if (c < n) + right = mid; + else + left = mid + 1; } int u = left; left = x; right = m - 1; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; int c = 0; while (c < n && image[mid][c] == '0') ++c; - if (c < n) left = mid; - else right = mid - 1; + if (c < n) + left = mid; + else + right = mid - 1; } int d = left; left = 0; right = y; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int r = 0; while (r < m && image[r][mid] == '0') ++r; - if (r < m) right = mid; - else left = mid + 1; + if (r < m) + right = mid; + else + left = mid + 1; } int l = left; left = y; right = n - 1; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; int r = 0; while (r < m && image[r][mid] == '0') ++r; - if (r < m) left = mid; - else right = mid - 1; + if (r < m) + left = mid; + else + right = mid - 1; } int r = left; return (d - u + 1) * (r - l + 1); diff --git a/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.cpp b/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.cpp index 99cabbd89e639..3c8e4ec54a3cb 100644 --- a/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.cpp +++ b/solution/0300-0399/0303.Range Sum Query - Immutable/Solution.cpp @@ -7,7 +7,7 @@ class NumArray { s.resize(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; } - + int sumRange(int left, int right) { return s[right + 1] - s[left]; } diff --git a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.cpp b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.cpp index b63bb64efed23..b767f5cce2b86 100644 --- a/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.cpp +++ b/solution/0300-0399/0304.Range Sum Query 2D - Immutable/Solution.cpp @@ -5,17 +5,15 @@ class NumMatrix { NumMatrix(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); s.resize(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + matrix[i][j]; } } } - + int sumRegion(int row1, int col1, int row2, int col2) { - return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1]; + return s[row2 + 1][col2 + 1] - s[row2 + 1][col1] - s[row1][col2 + 1] + s[row1][col1]; } }; diff --git a/solution/0300-0399/0305.Number of Islands II/Solution.cpp b/solution/0300-0399/0305.Number of Islands II/Solution.cpp index daac610aaf75f..dffdd0297c6d0 100644 --- a/solution/0300-0399/0305.Number of Islands II/Solution.cpp +++ b/solution/0300-0399/0305.Number of Islands II/Solution.cpp @@ -9,21 +9,17 @@ class Solution { vector ans; int cnt = 0; vector dirs = {-1, 0, 1, 0, -1}; - for (auto& pos : positions) - { + for (auto& pos : positions) { int i = pos[0], j = pos[1]; - if (grid[i][j] == 1) - { + if (grid[i][j] == 1) { ans.push_back(cnt); continue; } grid[i][j] = 1; ++cnt; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && find(x * n + y) != find(i * n + j)) - { + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1 && find(x * n + y) != find(i * n + j)) { p[find(x * n + y)] = find(i * n + j); --cnt; } diff --git a/solution/0300-0399/0306.Additive Number/Solution.cpp b/solution/0300-0399/0306.Additive Number/Solution.cpp index 2c9c71e00ea85..7dbc682c5cd21 100644 --- a/solution/0300-0399/0306.Additive Number/Solution.cpp +++ b/solution/0300-0399/0306.Additive Number/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: bool isAdditiveNumber(string num) { int n = num.size(); - for (int i = 1; i < min(n - 1, 19); ++i) - { - for (int j = i + 1; j < min(n, i + 19); ++j) - { + for (int i = 1; i < min(n - 1, 19); ++i) { + for (int j = i + 1; j < min(n, i + 19); ++j) { if (i > 1 && num[0] == '0') break; if (j - i > 1 && num[i] == '0') continue; auto a = stoll(num.substr(0, i)); @@ -19,7 +17,7 @@ class Solution { bool dfs(long long a, long long b, string num) { if (num == "") return true; if (a + b > 0 && num[0] == '0') return false; - for (int i = 1; i < min((int) num.size() + 1, 19); ++i) + for (int i = 1; i < min((int)num.size() + 1, 19); ++i) if (a + b == stoll(num.substr(0, i))) if (dfs(b, a + b, num.substr(i, num.size() - i))) return true; diff --git a/solution/0300-0399/0307.Range Sum Query - Mutable/Solution.cpp b/solution/0300-0399/0307.Range Sum Query - Mutable/Solution.cpp index b25745584b66d..01792a0df0ac8 100644 --- a/solution/0300-0399/0307.Range Sum Query - Mutable/Solution.cpp +++ b/solution/0300-0399/0307.Range Sum Query - Mutable/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -28,7 +28,6 @@ class BinaryIndexedTree { } }; - class NumArray { public: BinaryIndexedTree* tree; @@ -38,12 +37,12 @@ class NumArray { tree = new BinaryIndexedTree(n); for (int i = 0; i < n; ++i) tree->update(i + 1, nums[i]); } - + void update(int index, int val) { int prev = sumRange(index, index); tree->update(index + 1, val - prev); } - + int sumRange(int left, int right) { return tree->query(right + 1) - tree->query(left); } diff --git a/solution/0300-0399/0308.Range Sum Query 2D - Mutable/Solution.cpp b/solution/0300-0399/0308.Range Sum Query 2D - Mutable/Solution.cpp index 080d2f7ccd0b8..2d1b6678f2267 100644 --- a/solution/0300-0399/0308.Range Sum Query 2D - Mutable/Solution.cpp +++ b/solution/0300-0399/0308.Range Sum Query 2D - Mutable/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -42,17 +42,16 @@ class NumMatrix { trees[i] = tree; } } - + void update(int row, int col, int val) { BinaryIndexedTree* tree = trees[row]; int prev = tree->query(col + 1) - tree->query(col); tree->update(col + 1, val - prev); } - + int sumRegion(int row1, int col1, int row2, int col2) { int s = 0; - for (int i = row1; i <= row2; ++i) - { + for (int i = row1; i <= row2; ++i) { BinaryIndexedTree* tree = trees[i]; s += tree->query(col2 + 1) - tree->query(col1); } diff --git a/solution/0300-0399/0310.Minimum Height Trees/Solution.cpp b/solution/0300-0399/0310.Minimum Height Trees/Solution.cpp index be30a112a8abf..c71bcdbf58183 100644 --- a/solution/0300-0399/0310.Minimum Height Trees/Solution.cpp +++ b/solution/0300-0399/0310.Minimum Height Trees/Solution.cpp @@ -4,8 +4,7 @@ class Solution { if (n == 1) return {0}; vector> g(n); vector degree(n); - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; g[a].push_back(b); g[b].push_back(a); @@ -17,11 +16,9 @@ class Solution { if (degree[i] == 1) q.push(i); vector ans; - while (!q.empty()) - { + while (!q.empty()) { ans.clear(); - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { int a = q.front(); q.pop(); ans.push_back(a); diff --git a/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.cpp b/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.cpp index 4476fb22bf639..d8e2b6416932f 100644 --- a/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.cpp +++ b/solution/0300-0399/0311.Sparse Matrix Multiplication/Solution.cpp @@ -4,17 +4,13 @@ class Solution { int r1 = mat1.size(), c1 = mat1[0].size(), c2 = mat2[0].size(); vector> res(r1, vector(c2)); unordered_map> mp; - for (int i = 0; i < r1; ++i) - { - for (int j = 0; j < c1; ++j) - { + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c1; ++j) { if (mat1[i][j] != 0) mp[i].push_back(j); } } - for (int i = 0; i < r1; ++i) - { - for (int j = 0; j < c2; ++j) - { + for (int i = 0; i < r1; ++i) { + for (int j = 0; j < c2; ++j) { for (int k : mp[i]) res[i][j] += mat1[i][k] * mat2[k][j]; } } diff --git a/solution/0300-0399/0312.Burst Balloons/Solution.cpp b/solution/0300-0399/0312.Burst Balloons/Solution.cpp index 81849fb43dc86..9fcb2e96ea59d 100644 --- a/solution/0300-0399/0312.Burst Balloons/Solution.cpp +++ b/solution/0300-0399/0312.Burst Balloons/Solution.cpp @@ -5,13 +5,10 @@ class Solution { nums.push_back(1); int n = nums.size(); vector> dp(n, vector(n)); - for (int l = 2; l < n; ++l) - { - for (int i = 0; i + l < n; ++i) - { + for (int l = 2; l < n; ++l) { + for (int i = 0; i + l < n; ++i) { int j = i + l; - for (int k = i + 1; k < j; ++k) - { + for (int k = i + 1; k < j; ++k) { dp[i][j] = max(dp[i][j], dp[i][k] + dp[k][j] + nums[i] * nums[k] * nums[j]); } } diff --git a/solution/0300-0399/0315.Count of Smaller Numbers After Self/Solution.cpp b/solution/0300-0399/0315.Count of Smaller Numbers After Self/Solution.cpp index 4284c66b6202c..4afaac806ab02 100644 --- a/solution/0300-0399/0315.Count of Smaller Numbers After Self/Solution.cpp +++ b/solution/0300-0399/0315.Count of Smaller Numbers After Self/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -39,8 +39,7 @@ class Solution { for (int i = 0; i < n; ++i) m[alls[i]] = i + 1; BinaryIndexedTree* tree = new BinaryIndexedTree(n); vector ans(nums.size()); - for (int i = nums.size() - 1; i >= 0; --i) - { + for (int i = nums.size() - 1; i >= 0; --i) { int x = m[nums[i]]; tree->update(x, 1); ans[i] = tree->query(x - 1); diff --git a/solution/0300-0399/0317.Shortest Distance from All Buildings/Solution.cpp b/solution/0300-0399/0317.Shortest Distance from All Buildings/Solution.cpp index c52faf693ee04..15f63ed318192 100644 --- a/solution/0300-0399/0317.Shortest Distance from All Buildings/Solution.cpp +++ b/solution/0300-0399/0317.Shortest Distance from All Buildings/Solution.cpp @@ -9,29 +9,22 @@ class Solution { vector> cnt(m, vector(n)); vector> dist(m, vector(n)); vector dirs = {-1, 0, 1, 0, -1}; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 1) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 1) { ++total; q.push({i, j}); vector> vis(m, vector(n)); int d = 0; - while (!q.empty()) - { + while (!q.empty()) { ++d; - for (int k = q.size(); k > 0; --k) - { + for (int k = q.size(); k > 0; --k) { auto p = q.front(); q.pop(); - for (int l = 0; l < 4; ++l) - { + for (int l = 0; l < 4; ++l) { int x = p.first + dirs[l]; int y = p.second + dirs[l + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 && !vis[x][y]) - { + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 0 && !vis[x][y]) { ++cnt[x][y]; dist[x][y] += d; q.push({x, y}); diff --git a/solution/0300-0399/0318.Maximum Product of Word Lengths/Solution.cpp b/solution/0300-0399/0318.Maximum Product of Word Lengths/Solution.cpp index 88c555cbb8275..3e9a4a34fcf30 100644 --- a/solution/0300-0399/0318.Maximum Product of Word Lengths/Solution.cpp +++ b/solution/0300-0399/0318.Maximum Product of Word Lengths/Solution.cpp @@ -10,7 +10,7 @@ for (int i = 0; i < n - 1; ++i) for (int j = i + 1; j < n; ++j) if (!(mask[i] & mask[j])) - ans = max(ans, (int) (words[i].size() * words[j].size())); + ans = max(ans, (int)(words[i].size() * words[j].size())); return ans; } }; \ No newline at end of file diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.cpp b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.cpp index 26958bc79cb78..d0c8e6389d5c5 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.cpp +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/Solution.cpp @@ -5,8 +5,7 @@ class Solution { int countComponents(int n, vector>& edges) { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; p[find(a)] = find(b); } diff --git a/solution/0300-0399/0324.Wiggle Sort II/Solution.cpp b/solution/0300-0399/0324.Wiggle Sort II/Solution.cpp index 237619adcfcfb..9bd111b41749f 100644 --- a/solution/0300-0399/0324.Wiggle Sort II/Solution.cpp +++ b/solution/0300-0399/0324.Wiggle Sort II/Solution.cpp @@ -5,10 +5,11 @@ class Solution { sort(arr.begin(), arr.end()); int n = nums.size(); int i = (n - 1) >> 1, j = n - 1; - for (int k = 0; k < n; ++k) - { - if (k % 2 == 0) nums[k] = arr[i--]; - else nums[k] = arr[j--]; + for (int k = 0; k < n; ++k) { + if (k % 2 == 0) + nums[k] = arr[i--]; + else + nums[k] = arr[j--]; } } }; \ No newline at end of file diff --git a/solution/0300-0399/0325.Maximum Size Subarray Sum Equals k/Solution.cpp b/solution/0300-0399/0325.Maximum Size Subarray Sum Equals k/Solution.cpp index 929f42d69e42b..0318a4fd47a67 100644 --- a/solution/0300-0399/0325.Maximum Size Subarray Sum Equals k/Solution.cpp +++ b/solution/0300-0399/0325.Maximum Size Subarray Sum Equals k/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map mp; mp[0] = -1; int s = 0, ans = 0; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { s += nums[i]; if (mp.count(s - k)) ans = max(ans, i - mp[s - k]); if (!mp.count(s)) mp[s] = i; diff --git a/solution/0300-0399/0327.Count of Range Sum/Solution.cpp b/solution/0300-0399/0327.Count of Range Sum/Solution.cpp index 1302b0db31562..d77a994b6f343 100644 --- a/solution/0300-0399/0327.Count of Range Sum/Solution.cpp +++ b/solution/0300-0399/0327.Count of Range Sum/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -35,8 +35,7 @@ class Solution { vector preSum(n + 1); for (int i = 0; i < n; ++i) preSum[i + 1] = preSum[i] + nums[i]; set alls; - for (auto& s : preSum) - { + for (auto& s : preSum) { alls.insert(s); alls.insert(s - upper); alls.insert(s - lower); @@ -46,8 +45,7 @@ class Solution { for (auto& v : alls) m[v] = idx++; BinaryIndexedTree* tree = new BinaryIndexedTree(m.size()); int ans = 0; - for (auto& s : preSum) - { + for (auto& s : preSum) { int i = m[s - upper], j = m[s - lower]; ans += tree->query(j) - tree->query(i - 1); tree->update(m[s], 1); diff --git a/solution/0300-0399/0328.Odd Even Linked List/Solution.cpp b/solution/0300-0399/0328.Odd Even Linked List/Solution.cpp index e2170e499f4d6..173d230c0bf11 100644 --- a/solution/0300-0399/0328.Odd Even Linked List/Solution.cpp +++ b/solution/0300-0399/0328.Odd Even Linked List/Solution.cpp @@ -15,7 +15,7 @@ class Solution { return head; } ListNode *odd = head, *even = head->next; - ListNode *evenHead = even; + ListNode* evenHead = even; while (even && even->next) { odd->next = even->next; odd = odd->next; diff --git a/solution/0300-0399/0329.Longest Increasing Path in a Matrix/Solution.cpp b/solution/0300-0399/0329.Longest Increasing Path in a Matrix/Solution.cpp index d373c482e751d..66a354cf6501a 100644 --- a/solution/0300-0399/0329.Longest Increasing Path in a Matrix/Solution.cpp +++ b/solution/0300-0399/0329.Longest Increasing Path in a Matrix/Solution.cpp @@ -21,8 +21,7 @@ class Solution { if (memo[i][j] != -1) return memo[i][j]; int ans = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && matrix[x][y] > matrix[i][j]) ans = max(ans, dfs(x, y) + 1); diff --git a/solution/0300-0399/0333.Largest BST Subtree/Solution.cpp b/solution/0300-0399/0333.Largest BST Subtree/Solution.cpp index 14f69fe895ceb..70eb32ae78860 100644 --- a/solution/0300-0399/0333.Largest BST Subtree/Solution.cpp +++ b/solution/0300-0399/0333.Largest BST Subtree/Solution.cpp @@ -23,8 +23,7 @@ class Solution { if (!root) return {INT_MAX, INT_MIN, 0}; auto left = dfs(root->left); auto right = dfs(root->right); - if (left[1] < root->val && root->val < right[0]) - { + if (left[1] < root->val && root->val < right[0]) { ans = max(ans, left[2] + right[2] + 1); return {min(root->val, left[0]), max(root->val, right[1]), left[2] + right[2] + 1}; } diff --git a/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.cpp b/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.cpp index db0ddc0c9262f..cf799055f8b48 100644 --- a/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.cpp +++ b/solution/0300-0399/0334.Increasing Triplet Subsequence/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: bool increasingTriplet(vector& nums) { int mi = INT_MAX, mid = INT_MAX; - for (int num : nums) - { + for (int num : nums) { if (num > mid) return true; - if (num <= mi) mi = num; - else mid = num; + if (num <= mi) + mi = num; + else + mid = num; } return false; } diff --git a/solution/0300-0399/0335.Self Crossing/Solution.cpp b/solution/0300-0399/0335.Self Crossing/Solution.cpp index 500ea8465ba8b..463e02eac70bd 100644 --- a/solution/0300-0399/0335.Self Crossing/Solution.cpp +++ b/solution/0300-0399/0335.Self Crossing/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool isSelfCrossing(vector& distance) { vector d = distance; - for (int i = 3; i < d.size(); ++i) - { + for (int i = 3; i < d.size(); ++i) { if (d[i] >= d[i - 2] && d[i - 1] <= d[i - 3]) return true; if (i >= 4 && d[i - 1] == d[i - 3] && d[i] + d[i - 4] >= d[i - 2]) return true; if (i >= 5 && d[i - 2] >= d[i - 4] && d[i - 1] <= d[i - 3] && d[i] >= d[i - 2] - d[i - 4] && d[i - 1] + d[i - 5] >= d[i - 3]) return true; diff --git a/solution/0300-0399/0337.House Robber III/Solution.cpp b/solution/0300-0399/0337.House Robber III/Solution.cpp index f5e3942c6dea3..7f6612f22d745 100644 --- a/solution/0300-0399/0337.House Robber III/Solution.cpp +++ b/solution/0300-0399/0337.House Robber III/Solution.cpp @@ -21,7 +21,7 @@ class Solution { if (!root) return 0; if (memo.count(root)) return memo[root]; int a = dfs(root->left) + dfs(root->right); - int b = root-> val; + int b = root->val; if (root->left) b += dfs(root->left->left) + dfs(root->left->right); if (root->right) b += dfs(root->right->left) + dfs(root->right->right); int res = max(a, b); diff --git a/solution/0300-0399/0346.Moving Average from Data Stream/Solution.cpp b/solution/0300-0399/0346.Moving Average from Data Stream/Solution.cpp index 83001cb699e51..a5886735c121b 100644 --- a/solution/0300-0399/0346.Moving Average from Data Stream/Solution.cpp +++ b/solution/0300-0399/0346.Moving Average from Data Stream/Solution.cpp @@ -3,13 +3,13 @@ class MovingAverage { MovingAverage(int size) { arr.resize(size); } - + double next(int val) { int idx = cnt % arr.size(); s += val - arr[idx]; arr[idx] = val; ++cnt; - return (double) s / min(cnt, (int) arr.size()); + return (double)s / min(cnt, (int)arr.size()); } private: diff --git a/solution/0300-0399/0347.Top K Frequent Elements/Solution.cpp b/solution/0300-0399/0347.Top K Frequent Elements/Solution.cpp index 02e35a19c4be3..d5a73d9038cd6 100644 --- a/solution/0300-0399/0347.Top K Frequent Elements/Solution.cpp +++ b/solution/0300-0399/0347.Top K Frequent Elements/Solution.cpp @@ -7,18 +7,15 @@ class Solution { unordered_map counter; for (auto& e : nums) ++counter[e]; priority_queue, vector>, decltype(&cmp)> pq(cmp); - for (auto& [num, freq] : counter) - { - if (pq.size() == k) - { + for (auto& [num, freq] : counter) { + if (pq.size() == k) { pq.emplace(num, freq); pq.pop(); - } - else pq.emplace(num, freq); + } else + pq.emplace(num, freq); } vector ans; - while (!pq.empty()) - { + while (!pq.empty()) { ans.push_back(pq.top().first); pq.pop(); } diff --git a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cpp b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cpp index d542d2b002e6d..6aaf67c9c6d4c 100644 --- a/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cpp +++ b/solution/0300-0399/0349.Intersection of Two Arrays/Solution.cpp @@ -5,10 +5,8 @@ class Solution { for (int num : nums1) s.insert(num); unordered_set t; vector res; - for (int num : nums2) - { - if (s.count(num) && !t.count(num)) - { + for (int num : nums2) { + if (s.count(num) && !t.count(num)) { t.insert(num); res.push_back(num); } diff --git a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp index a2f3d4ee0079f..d52d47c599fa9 100644 --- a/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp +++ b/solution/0300-0399/0350.Intersection of Two Arrays II/Solution.cpp @@ -4,10 +4,8 @@ class Solution { unordered_map counter; for (int num : nums1) ++counter[num]; vector res; - for (int num : nums2) - { - if (counter[num] > 0) - { + for (int num : nums2) { + if (counter[num] > 0) { --counter[num]; res.push_back(num); } diff --git a/solution/0300-0399/0352.Data Stream as Disjoint Intervals/Solution.cpp b/solution/0300-0399/0352.Data Stream as Disjoint Intervals/Solution.cpp index be19030e3fdce..a3f1cfdfcdc87 100644 --- a/solution/0300-0399/0352.Data Stream as Disjoint Intervals/Solution.cpp +++ b/solution/0300-0399/0352.Data Stream as Disjoint Intervals/Solution.cpp @@ -1,24 +1,25 @@ class SummaryRanges { private: map> mp; + public: SummaryRanges() { - } - + void addNum(int val) { auto r = mp.upper_bound(val); auto l = r == mp.begin() ? mp.end() : prev(r); - if (l != mp.end() && r != mp.end() && l->second[1] + 1 == val && r->second[0] - 1 == val) - { + if (l != mp.end() && r != mp.end() && l->second[1] + 1 == val && r->second[0] - 1 == val) { l->second[1] = r->second[1]; mp.erase(r); - } - else if (l != mp.end() && val <= l->second[1] + 1) l->second[1] = max(val, l->second[1]); - else if (r != mp.end() && val >= r->second[0] - 1) r->second[0] = min(val, r->second[0]); - else mp[val] = {val, val}; + } else if (l != mp.end() && val <= l->second[1] + 1) + l->second[1] = max(val, l->second[1]); + else if (r != mp.end() && val >= r->second[0] - 1) + r->second[0] = min(val, r->second[0]); + else + mp[val] = {val, val}; } - + vector> getIntervals() { vector> res; for (auto& range : mp) res.push_back(range.second); diff --git a/solution/0300-0399/0354.Russian Doll Envelopes/Solution.cpp b/solution/0300-0399/0354.Russian Doll Envelopes/Solution.cpp index 653ee63702dd4..9f5d239645e90 100644 --- a/solution/0300-0399/0354.Russian Doll Envelopes/Solution.cpp +++ b/solution/0300-0399/0354.Russian Doll Envelopes/Solution.cpp @@ -5,13 +5,12 @@ class Solution { return e1[0] < e2[0] || (e1[0] == e2[0] && e1[1] > e2[1]); }); int n = envelopes.size(); - vector d{envelopes[0][1]}; - for (int i = 1; i < n; ++i) - { + vector d {envelopes[0][1]}; + for (int i = 1; i < n; ++i) { int x = envelopes[i][1]; - if (x > d[d.size() - 1]) d.push_back(x); - else - { + if (x > d[d.size() - 1]) + d.push_back(x); + else { int idx = lower_bound(d.begin(), d.end(), x) - d.begin(); if (idx == d.size()) idx = 0; d[idx] = x; diff --git a/solution/0300-0399/0357.Count Numbers with Unique Digits/Solution.cpp b/solution/0300-0399/0357.Count Numbers with Unique Digits/Solution.cpp index 95e4ee8fbca52..a3bd0498af51e 100644 --- a/solution/0300-0399/0357.Count Numbers with Unique Digits/Solution.cpp +++ b/solution/0300-0399/0357.Count Numbers with Unique Digits/Solution.cpp @@ -4,8 +4,7 @@ class Solution { if (n == 0) return 1; if (n == 1) return 10; int ans = 10; - for (int i = 0, cur = 9; i < n - 1; ++i) - { + for (int i = 0, cur = 9; i < n - 1; ++i) { cur *= (9 - i); ans += cur; } diff --git a/solution/0300-0399/0360.Sort Transformed Array/Solution.cpp b/solution/0300-0399/0360.Sort Transformed Array/Solution.cpp index 5b83dd0aed897..1c407f215929c 100644 --- a/solution/0300-0399/0360.Sort Transformed Array/Solution.cpp +++ b/solution/0300-0399/0360.Sort Transformed Array/Solution.cpp @@ -1,45 +1,35 @@ class Solution { public: - vector sortTransformedArray(vector &nums, int a, int b, int c) { - int n = nums.size(); - int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1; - vector res(n); - while (i <= j) - { - int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]); - if (a < 0) - { - if (v1 <= v2) - { - res[k] = v1; - ++i; - } - else - { - res[k] = v2; - --j; - } - ++k; - } - else - { - if (v1 >= v2) - { - res[k] = v1; - ++i; - } - else - { - res[k] = v2; - --j; - } - --k; - } - } - return res; - } + vector sortTransformedArray(vector& nums, int a, int b, int c) { + int n = nums.size(); + int i = 0, j = n - 1, k = a < 0 ? 0 : n - 1; + vector res(n); + while (i <= j) { + int v1 = f(a, b, c, nums[i]), v2 = f(a, b, c, nums[j]); + if (a < 0) { + if (v1 <= v2) { + res[k] = v1; + ++i; + } else { + res[k] = v2; + --j; + } + ++k; + } else { + if (v1 >= v2) { + res[k] = v1; + ++i; + } else { + res[k] = v2; + --j; + } + --k; + } + } + return res; + } - int f(int a, int b, int c, int x) { - return a * x * x + b * x + c; - } + int f(int a, int b, int c, int x) { + return a * x * x + b * x + c; + } }; \ No newline at end of file diff --git a/solution/0300-0399/0361.Bomb Enemy/Solution.cpp b/solution/0300-0399/0361.Bomb Enemy/Solution.cpp index 76478aef0e94f..d8cabe22c0b63 100644 --- a/solution/0300-0399/0361.Bomb Enemy/Solution.cpp +++ b/solution/0300-0399/0361.Bomb Enemy/Solution.cpp @@ -3,45 +3,45 @@ class Solution { int maxKilledEnemies(vector>& grid) { int m = grid.size(), n = grid[0].size(); vector> g(m, vector(n)); - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { int t = 0; - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 'W') t = 0; - else if (grid[i][j] == 'E') ++t; + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 'W') + t = 0; + else if (grid[i][j] == 'E') + ++t; g[i][j] += t; } t = 0; - for (int j = n - 1; j >= 0; --j) - { - if (grid[i][j] == 'W') t = 0; - else if (grid[i][j] == 'E') ++t; + for (int j = n - 1; j >= 0; --j) { + if (grid[i][j] == 'W') + t = 0; + else if (grid[i][j] == 'E') + ++t; g[i][j] += t; } } - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { int t = 0; - for (int i = 0; i < m; ++i) - { - if (grid[i][j] == 'W') t = 0; - else if (grid[i][j] == 'E') ++t; + for (int i = 0; i < m; ++i) { + if (grid[i][j] == 'W') + t = 0; + else if (grid[i][j] == 'E') + ++t; g[i][j] += t; } t = 0; - for (int i = m - 1; i >= 0; --i) - { - if (grid[i][j] == 'W') t = 0; - else if (grid[i][j] == 'E') ++t; + for (int i = m - 1; i >= 0; --i) { + if (grid[i][j] == 'W') + t = 0; + else if (grid[i][j] == 'E') + ++t; g[i][j] += t; } } int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (grid[i][j] == '0') ans = max(ans, g[i][j]); } } diff --git a/solution/0300-0399/0366.Find Leaves of Binary Tree/Solution.cpp b/solution/0300-0399/0366.Find Leaves of Binary Tree/Solution.cpp index f17643cf628b3..51d10a3e5a19b 100644 --- a/solution/0300-0399/0366.Find Leaves of Binary Tree/Solution.cpp +++ b/solution/0300-0399/0366.Find Leaves of Binary Tree/Solution.cpp @@ -14,8 +14,7 @@ class Solution { vector> findLeaves(TreeNode* root) { vector> res; TreeNode* prev = new TreeNode(0, root, nullptr); - while (prev->left) - { + while (prev->left) { vector t; dfs(prev->left, prev, t); res.push_back(t); @@ -25,11 +24,12 @@ class Solution { void dfs(TreeNode* root, TreeNode* prev, vector& t) { if (!root) return; - if (!root->left && !root->right) - { + if (!root->left && !root->right) { t.push_back(root->val); - if (prev->left == root) prev->left = nullptr; - else prev->right = nullptr; + if (prev->left == root) + prev->left = nullptr; + else + prev->right = nullptr; } dfs(root->left, root, t); dfs(root->right, root, t); diff --git a/solution/0300-0399/0367.Valid Perfect Square/Solution.cpp b/solution/0300-0399/0367.Valid Perfect Square/Solution.cpp index a50985e594949..07f694c111408 100644 --- a/solution/0300-0399/0367.Valid Perfect Square/Solution.cpp +++ b/solution/0300-0399/0367.Valid Perfect Square/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: bool isPerfectSquare(int num) { long left = 1, right = num; - while (left < right) - { + while (left < right) { long mid = left + right >> 1; - if (mid * mid >= num) right = mid; - else left = mid + 1; + if (mid * mid >= num) + right = mid; + else + left = mid + 1; } return left * left == num; } diff --git a/solution/0300-0399/0371.Sum of Two Integers/Solution.cpp b/solution/0300-0399/0371.Sum of Two Integers/Solution.cpp index 11a4b83584e5f..8467935eda75f 100644 --- a/solution/0300-0399/0371.Sum of Two Integers/Solution.cpp +++ b/solution/0300-0399/0371.Sum of Two Integers/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: int getSum(int a, int b) { - while (b) - { + while (b) { unsigned int carry = (unsigned int)(a & b) << 1; a = a ^ b; b = carry; diff --git a/solution/0300-0399/0372.Super Pow/Solution.cpp b/solution/0300-0399/0372.Super Pow/Solution.cpp index 71342000be116..02a41ec5d53a9 100644 --- a/solution/0300-0399/0372.Super Pow/Solution.cpp +++ b/solution/0300-0399/0372.Super Pow/Solution.cpp @@ -1,11 +1,11 @@ class Solution { const int MOD = 1337; + public: int superPow(int a, vector& b) { int ans = 1; - for (int i = b.size() - 1; i >= 0; --i) - { - ans = (long) ans * quickPowAndMod(a, b[i]) % MOD; + for (int i = b.size() - 1; i >= 0; --i) { + ans = (long)ans * quickPowAndMod(a, b[i]) % MOD; a = quickPowAndMod(a, 10); } return ans; @@ -13,10 +13,8 @@ class Solution { int quickPowAndMod(int a, int b) { int ans = 1; - while (b) - { - if (b & 1) - { + while (b) { + if (b & 1) { ans = (ans * (a % MOD)) % MOD; } b >>= 1; diff --git a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.cpp b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.cpp index 7c8cae774e649..243d7eebd4931 100644 --- a/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.cpp +++ b/solution/0300-0399/0373.Find K Pairs with Smallest Sums/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: - vector> kSmallestPairs(vector &nums1, vector &nums2, int k) { - auto cmp = [&nums1, &nums2](const pair &a, const pair &b) - { + vector> kSmallestPairs(vector& nums1, vector& nums2, int k) { + auto cmp = [&nums1, &nums2](const pair& a, const pair& b) { return nums1[a.first] + nums2[a.second] > nums1[b.first] + nums2[b.second]; }; @@ -12,11 +11,10 @@ class Solution { priority_queue, vector>, decltype(cmp)> pq(cmp); for (int i = 0; i < min(k, m); i++) pq.emplace(i, 0); - while (k-- && !pq.empty()) - { + while (k-- && !pq.empty()) { auto [x, y] = pq.top(); pq.pop(); - ans.emplace_back(initializer_list{nums1[x], nums2[y]}); + ans.emplace_back(initializer_list {nums1[x], nums2[y]}); if (y + 1 < n) pq.emplace(x, y + 1); } diff --git a/solution/0300-0399/0375.Guess Number Higher or Lower II/Solution.cpp b/solution/0300-0399/0375.Guess Number Higher or Lower II/Solution.cpp index fb0483b1ed053..d9ba619ccc4a9 100644 --- a/solution/0300-0399/0375.Guess Number Higher or Lower II/Solution.cpp +++ b/solution/0300-0399/0375.Guess Number Higher or Lower II/Solution.cpp @@ -2,14 +2,11 @@ class Solution { public: int getMoneyAmount(int n) { vector> dp(n + 10, vector(n + 10)); - for (int l = 2; l <= n; ++l) - { - for (int i = 1; i + l - 1 <= n; ++i) - { + for (int l = 2; l <= n; ++l) { + for (int i = 1; i + l - 1 <= n; ++i) { int j = i + l - 1; dp[i][j] = INT_MAX; - for (int k = i; k <= j; ++k) - { + for (int k = i; k <= j; ++k) { int t = max(dp[i][k - 1], dp[k + 1][j]) + k; dp[i][j] = min(dp[i][j], t); } diff --git a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.cpp b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.cpp index 0b6a2fdd085ca..10a663e79dbe1 100644 --- a/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.cpp +++ b/solution/0300-0399/0380.Insert Delete GetRandom O(1)/Solution.cpp @@ -2,18 +2,18 @@ class RandomizedSet { private: unordered_map mp; vector nums; + public: RandomizedSet() { - } - + bool insert(int val) { if (mp.count(val)) return false; mp[val] = nums.size(); nums.push_back(val); return true; } - + bool remove(int val) { if (!mp.count(val)) return false; int idx = mp[val]; @@ -23,7 +23,7 @@ class RandomizedSet { nums.pop_back(); return true; } - + int getRandom() { return nums[rand() % nums.size()]; } diff --git a/solution/0300-0399/0382.Linked List Random Node/Solution.cpp b/solution/0300-0399/0382.Linked List Random Node/Solution.cpp index aa09459a7ac4c..b4bbf5807b545 100644 --- a/solution/0300-0399/0382.Linked List Random Node/Solution.cpp +++ b/solution/0300-0399/0382.Linked List Random Node/Solution.cpp @@ -15,11 +15,10 @@ class Solution { Solution(ListNode* head) { this->head = head; } - + int getRandom() { int n = 0, ans = 0; - for (ListNode* node = head; node != nullptr; node = node->next) - { + for (ListNode* node = head; node != nullptr; node = node->next) { n += 1; int x = 1 + rand() % n; if (n == x) ans = node->val; diff --git a/solution/0300-0399/0383.Ransom Note/Solution.cpp b/solution/0300-0399/0383.Ransom Note/Solution.cpp index a0b61e84ae9b6..fb75586030ee1 100644 --- a/solution/0300-0399/0383.Ransom Note/Solution.cpp +++ b/solution/0300-0399/0383.Ransom Note/Solution.cpp @@ -3,8 +3,7 @@ class Solution { bool canConstruct(string ransomNote, string magazine) { vector counter(26); for (char c : magazine) ++counter[c - 'a']; - for (char c : ransomNote) - { + for (char c : ransomNote) { if (counter[c - 'a'] <= 0) return false; --counter[c - 'a']; } diff --git a/solution/0300-0399/0384.Shuffle an Array/Solution.cpp b/solution/0300-0399/0384.Shuffle an Array/Solution.cpp index daf963934649b..61178cdb299bc 100644 --- a/solution/0300-0399/0384.Shuffle an Array/Solution.cpp +++ b/solution/0300-0399/0384.Shuffle an Array/Solution.cpp @@ -8,15 +8,14 @@ class Solution { this->original.resize(nums.size()); copy(nums.begin(), nums.end(), original.begin()); } - + vector reset() { copy(original.begin(), original.end(), nums.begin()); return nums; } - + vector shuffle() { - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { int j = i + rand() % (nums.size() - i); swap(nums[i], nums[j]); } diff --git a/solution/0300-0399/0385.Mini Parser/Solution.cpp b/solution/0300-0399/0385.Mini Parser/Solution.cpp index d84cc99650f98..e29b7ec3e1844 100644 --- a/solution/0300-0399/0385.Mini Parser/Solution.cpp +++ b/solution/0300-0399/0385.Mini Parser/Solution.cpp @@ -35,15 +35,14 @@ class Solution { if (s.size() <= 2) return NestedInteger(); NestedInteger ans; int depth = 0; - for (int i = 1, j = 1; i < s.size(); ++i) - { - if (depth == 0 && (s[i] == ',' || i == s.size() - 1)) - { + for (int i = 1, j = 1; i < s.size(); ++i) { + if (depth == 0 && (s[i] == ',' || i == s.size() - 1)) { ans.add(deserialize(s.substr(j, i - j))); j = i + 1; - } - else if (s[i] == '[') ++depth; - else if (s[i] == ']') --depth; + } else if (s[i] == '[') + ++depth; + else if (s[i] == ']') + --depth; } return ans; } diff --git a/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp b/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp index f2d3ded837713..f7f95eba0f4b8 100644 --- a/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp +++ b/solution/0300-0399/0386.Lexicographical Numbers/Solution.cpp @@ -3,12 +3,11 @@ class Solution { vector lexicalOrder(int n) { vector ans; int v = 1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { ans.push_back(v); - if (v * 10 <= n) v *= 10; - else - { + if (v * 10 <= n) + v *= 10; + else { while (v % 10 == 9 || v + 1 > n) v /= 10; ++v; } diff --git a/solution/0300-0399/0390.Elimination Game/Solution.cpp b/solution/0300-0399/0390.Elimination Game/Solution.cpp index a0b9b460816ea..560499d1b7ee0 100644 --- a/solution/0300-0399/0390.Elimination Game/Solution.cpp +++ b/solution/0300-0399/0390.Elimination Game/Solution.cpp @@ -2,15 +2,11 @@ class Solution { public: int lastRemaining(int n) { int a1 = 1, an = n, step = 1; - for (int i = 0, cnt = n; cnt > 1; cnt >>= 1, step <<= 1, ++i) - { - if (i % 2) - { + for (int i = 0, cnt = n; cnt > 1; cnt >>= 1, step <<= 1, ++i) { + if (i % 2) { an -= step; if (cnt % 2) a1 += step; - } - else - { + } else { a1 += step; if (cnt % 2) an -= step; } diff --git a/solution/0300-0399/0391.Perfect Rectangle/Solution.cpp b/solution/0300-0399/0391.Perfect Rectangle/Solution.cpp index 9d18d86ef2570..fda0f486748b6 100644 --- a/solution/0300-0399/0391.Perfect Rectangle/Solution.cpp +++ b/solution/0300-0399/0391.Perfect Rectangle/Solution.cpp @@ -25,9 +25,7 @@ class Solution { ++cnt[{r[2], r[1]}]; } - if (area != (long long)(maxX - minX) * (maxY - minY) || - cnt[{minX, minY}] != 1 || cnt[{minX, maxY}] != 1 || - cnt[{maxX, maxY}] != 1 || cnt[{maxX, minY}] != 1) { + if (area != (long long)(maxX - minX) * (maxY - minY) || cnt[{minX, minY}] != 1 || cnt[{minX, maxY}] != 1 || cnt[{maxX, maxY}] != 1 || cnt[{maxX, minY}] != 1) { return false; } diff --git a/solution/0300-0399/0392.Is Subsequence/Solution.cpp b/solution/0300-0399/0392.Is Subsequence/Solution.cpp index 1c2c3f121d4b1..8d1fad17a1ea4 100644 --- a/solution/0300-0399/0392.Is Subsequence/Solution.cpp +++ b/solution/0300-0399/0392.Is Subsequence/Solution.cpp @@ -3,8 +3,7 @@ class Solution { bool isSubsequence(string s, string t) { int m = s.size(), n = t.size(); int i = 0, j = 0; - while (i < m && j < n) - { + while (i < m && j < n) { if (s[i] == t[j]) ++i; ++j; } diff --git a/solution/0300-0399/0393.UTF-8 Validation/Solution.cpp b/solution/0300-0399/0393.UTF-8 Validation/Solution.cpp index da0a496f21a80..44113d11a652d 100644 --- a/solution/0300-0399/0393.UTF-8 Validation/Solution.cpp +++ b/solution/0300-0399/0393.UTF-8 Validation/Solution.cpp @@ -2,18 +2,20 @@ public: bool validUtf8(vector& data) { int n = 0; - for (int& v : data) - { - if (n > 0) - { + for (int& v : data) { + if (n > 0) { if (v >> 6 != 0b10) return false; --n; - } - else if (v >> 7 == 0) n = 0; - else if (v >> 5 == 0b110) n = 1; - else if (v >> 4 == 0b1110) n = 2; - else if (v >> 3 == 0b11110) n = 3; - else return false; + } else if (v >> 7 == 0) + n = 0; + else if (v >> 5 == 0b110) + n = 1; + else if (v >> 4 == 0b1110) + n = 2; + else if (v >> 3 == 0b11110) + n = 3; + else + return false; } return n == 0; } diff --git a/solution/0300-0399/0396.Rotate Function/Solution.cpp b/solution/0300-0399/0396.Rotate Function/Solution.cpp index 0dda34655c987..7b921b9dbc37f 100644 --- a/solution/0300-0399/0396.Rotate Function/Solution.cpp +++ b/solution/0300-0399/0396.Rotate Function/Solution.cpp @@ -2,14 +2,12 @@ class Solution { public: int maxRotateFunction(vector& nums) { int f = 0, s = 0, n = nums.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { f += i * nums[i]; s += nums[i]; } int ans = f; - for (int i = 1; i < n; ++i) - { + for (int i = 1; i < n; ++i) { f = f + s - n * nums[n - i]; ans = max(ans, f); } diff --git a/solution/0300-0399/0397.Integer Replacement/Solution.cpp b/solution/0300-0399/0397.Integer Replacement/Solution.cpp index ef14ce61cdd2f..17f20535c0ddf 100644 --- a/solution/0300-0399/0397.Integer Replacement/Solution.cpp +++ b/solution/0300-0399/0397.Integer Replacement/Solution.cpp @@ -3,11 +3,13 @@ class Solution { int integerReplacement(int N) { int ans = 0; long n = N; - while (n != 1) - { - if ((n & 1) == 0) n >>= 1; - else if (n != 3 && (n & 3) == 3) ++n; - else --n; + while (n != 1) { + if ((n & 1) == 0) + n >>= 1; + else if (n != 3 && (n & 3) == 3) + ++n; + else + --n; ++ans; } return ans; diff --git a/solution/0300-0399/0398.Random Pick Index/Solution.cpp b/solution/0300-0399/0398.Random Pick Index/Solution.cpp index ce9efc9ecaed0..4206480813b52 100644 --- a/solution/0300-0399/0398.Random Pick Index/Solution.cpp +++ b/solution/0300-0399/0398.Random Pick Index/Solution.cpp @@ -5,13 +5,11 @@ class Solution { Solution(vector& nums) { this->nums = nums; } - + int pick(int target) { int n = 0, ans = 0; - for (int i = 0; i < nums.size(); ++i) - { - if (nums[i] == target) - { + for (int i = 0; i < nums.size(); ++i) { + if (nums[i] == target) { ++n; int x = 1 + rand() % n; if (n == x) ans = i; diff --git a/solution/0300-0399/0399.Evaluate Division/Solution.cpp b/solution/0300-0399/0399.Evaluate Division/Solution.cpp index 1db067bbf3231..67de4c4987459 100644 --- a/solution/0300-0399/0399.Evaluate Division/Solution.cpp +++ b/solution/0300-0399/0399.Evaluate Division/Solution.cpp @@ -5,15 +5,13 @@ class Solution { vector calcEquation(vector>& equations, vector& values, vector>& queries) { int n = equations.size(); - for (auto e : equations) - { + for (auto e : equations) { p[e[0]] = e[0]; p[e[1]] = e[1]; w[e[0]] = 1.0; w[e[1]] = 1.0; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { vector e = equations[i]; string a = e[0], b = e[1]; string pa = find(a), pb = find(b); @@ -23,8 +21,7 @@ class Solution { } int m = queries.size(); vector ans(m); - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { string c = queries[i][0], d = queries[i][1]; ans[i] = p.find(c) == p.end() || p.find(d) == p.end() || find(c) != find(d) ? -1.0 : w[c] / w[d]; } @@ -32,8 +29,7 @@ class Solution { } string find(string x) { - if (p[x] != x) - { + if (p[x] != x) { string origin = p[x]; p[x] = find(p[x]); w[x] *= w[origin]; diff --git a/solution/0400-0499/0401.Binary Watch/Solution.cpp b/solution/0400-0499/0401.Binary Watch/Solution.cpp index 32595791865b6..385c20ee7053d 100644 --- a/solution/0400-0499/0401.Binary Watch/Solution.cpp +++ b/solution/0400-0499/0401.Binary Watch/Solution.cpp @@ -2,12 +2,9 @@ class Solution { public: vector readBinaryWatch(int turnedOn) { vector ans; - for (int i = 0; i < 12; ++i) - { - for (int j = 0; j < 60; ++j) - { - if (__builtin_popcount(i) + __builtin_popcount(j) == turnedOn) - { + for (int i = 0; i < 12; ++i) { + for (int j = 0; j < 60; ++j) { + if (__builtin_popcount(i) + __builtin_popcount(j) == turnedOn) { ans.push_back(to_string(i) + ":" + (j < 10 ? "0" : "") + to_string(j)); } } diff --git a/solution/0400-0499/0405.Convert a Number to Hexadecimal/Solution.cpp b/solution/0400-0499/0405.Convert a Number to Hexadecimal/Solution.cpp index b584d52960f40..98c0e21807072 100644 --- a/solution/0400-0499/0405.Convert a Number to Hexadecimal/Solution.cpp +++ b/solution/0400-0499/0405.Convert a Number to Hexadecimal/Solution.cpp @@ -3,12 +3,10 @@ class Solution { string toHex(int num) { if (num == 0) return "0"; string s = ""; - for (int i = 7; i >= 0; --i) - { + for (int i = 7; i >= 0; --i) { int x = (num >> (4 * i)) & 0xf; - if (s.size() > 0 || x != 0) - { - char c = x < 10 ? (char) (x + '0') : (char) (x - 10 + 'a'); + if (s.size() > 0 || x != 0) { + char c = x < 10 ? (char)(x + '0') : (char)(x - 10 + 'a'); s += c; } } diff --git a/solution/0400-0499/0406.Queue Reconstruction by Height/Solution.cpp b/solution/0400-0499/0406.Queue Reconstruction by Height/Solution.cpp index 26946c4bd71ba..27da853374ce9 100644 --- a/solution/0400-0499/0406.Queue Reconstruction by Height/Solution.cpp +++ b/solution/0400-0499/0406.Queue Reconstruction by Height/Solution.cpp @@ -5,7 +5,7 @@ class Solution { return a[0] > b[0] || (a[0] == b[0] && a[1] < b[1]); }); vector> ans; - for (const vector& p: people) + for (const vector& p : people) ans.insert(ans.begin() + p[1], p); return ans; } diff --git a/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp b/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp index 81535a0fde7e2..65d950d1a4298 100644 --- a/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp +++ b/solution/0400-0499/0410.Split Array Largest Sum/Solution.cpp @@ -1,11 +1,13 @@ class Solution { public: int splitArray(vector& nums, int m) { - int left = *max_element(nums.begin(), nums.end()), right = (int) 1e9; + int left = *max_element(nums.begin(), nums.end()), right = (int)1e9; while (left < right) { int mid = left + right >> 1; - if (check(nums, m, mid)) right = mid; - else left = mid + 1; + if (check(nums, m, mid)) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0400-0499/0412.Fizz Buzz/Solution.cpp b/solution/0400-0499/0412.Fizz Buzz/Solution.cpp index 6568657078932..63d3912d5c1f1 100644 --- a/solution/0400-0499/0412.Fizz Buzz/Solution.cpp +++ b/solution/0400-0499/0412.Fizz Buzz/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: vector fizzBuzz(int n) { vector ans; - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { string s = ""; if (i % 3 == 0) s += "Fizz"; if (i % 5 == 0) s += "Buzz"; diff --git a/solution/0400-0499/0414.Third Maximum Number/Solution.cpp b/solution/0400-0499/0414.Third Maximum Number/Solution.cpp index 9f8a84d2a43f1..ac11b728a474c 100644 --- a/solution/0400-0499/0414.Third Maximum Number/Solution.cpp +++ b/solution/0400-0499/0414.Third Maximum Number/Solution.cpp @@ -2,25 +2,19 @@ class Solution { public: int thirdMax(vector& nums) { long m1 = LONG_MIN, m2 = LONG_MIN, m3 = LONG_MIN; - for (int num : nums) - { + for (int num : nums) { if (num == m1 || num == m2 || num == m3) continue; - if (num > m1) - { + if (num > m1) { m3 = m2; m2 = m1; m1 = num; - } - else if (num > m2) - { + } else if (num > m2) { m3 = m2; m2 = num; - } - else if (num > m3) - { + } else if (num > m3) { m3 = num; } } - return (int) (m3 != LONG_MIN ? m3 : m1); + return (int)(m3 != LONG_MIN ? m3 : m1); } }; \ No newline at end of file diff --git a/solution/0400-0499/0415.Add Strings/Solution.cpp b/solution/0400-0499/0415.Add Strings/Solution.cpp index 5d4f4cd1808ed..141acd3171502 100644 --- a/solution/0400-0499/0415.Add Strings/Solution.cpp +++ b/solution/0400-0499/0415.Add Strings/Solution.cpp @@ -3,8 +3,7 @@ class Solution { string addStrings(string num1, string num2) { string ans; int i = num1.size() - 1, j = num2.size() - 1, carry = 0; - for (; i >= 0 || j >= 0 || carry; --i, --j) - { + for (; i >= 0 || j >= 0 || carry; --i, --j) { carry += (i < 0 ? 0 : num1[i] - '0') + (j < 0 ? 0 : num2[j] - '0'); ans += to_string(carry % 10); carry /= 10; diff --git a/solution/0400-0499/0417.Pacific Atlantic Water Flow/Solution.cpp b/solution/0400-0499/0417.Pacific Atlantic Water Flow/Solution.cpp index 9ea84bfe5bdc7..4e5e2ab0e74f7 100644 --- a/solution/0400-0499/0417.Pacific Atlantic Water Flow/Solution.cpp +++ b/solution/0400-0499/0417.Pacific Atlantic Water Flow/Solution.cpp @@ -14,17 +14,13 @@ class Solution { queue q2; unordered_set vis1; unordered_set vis2; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (i == 0 || j == 0) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (i == 0 || j == 0) { vis1.insert(i * n + j); q1.emplace(i, j); } - if (i == m - 1 || j == n - 1) - { + if (i == m - 1 || j == n - 1) { vis2.insert(i * n + j); q2.emplace(i, j); } @@ -33,13 +29,10 @@ class Solution { bfs(q1, vis1); bfs(q2, vis2); vector> ans; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int x = i * n + j; - if (vis1.count(x) && vis2.count(x)) - { + if (vis1.count(x) && vis2.count(x)) { ans.push_back({i, j}); } } @@ -49,18 +42,14 @@ class Solution { void bfs(queue& q, unordered_set& vis) { vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { - for (int k = q.size(); k > 0; --k) - { + while (!q.empty()) { + for (int k = q.size(); k > 0; --k) { auto p = q.front(); q.pop(); - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { int x = p.first + dirs[i]; int y = p.second + dirs[i + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && !vis.count(x * n + y) && heights[x][y] >= heights[p.first][p.second]) - { + if (x >= 0 && x < m && y >= 0 && y < n && !vis.count(x * n + y) && heights[x][y] >= heights[p.first][p.second]) { vis.insert(x * n + y); q.emplace(x, y); } diff --git a/solution/0400-0499/0419.Battleships in a Board/Solution.cpp b/solution/0400-0499/0419.Battleships in a Board/Solution.cpp index 32999aa6eb6e9..00cd5aa2ff54a 100644 --- a/solution/0400-0499/0419.Battleships in a Board/Solution.cpp +++ b/solution/0400-0499/0419.Battleships in a Board/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int countBattleships(vector>& board) { int m = board.size(), n = board[0].size(); int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (board[i][j] == '.') continue; if (i > 0 && board[i - 1][j] == 'X') continue; if (j > 0 && board[i][j - 1] == 'X') continue; diff --git a/solution/0400-0499/0420.Strong Password Checker/Solution.cpp b/solution/0400-0499/0420.Strong Password Checker/Solution.cpp index e4bb7a8735b77..836c0b58fc39e 100644 --- a/solution/0400-0499/0420.Strong Password Checker/Solution.cpp +++ b/solution/0400-0499/0420.Strong Password Checker/Solution.cpp @@ -4,15 +4,13 @@ class Solution { int types = countTypes(password); int n = password.size(); if (n < 6) return max(6 - n, 3 - types); - if (n <= 20) - { + if (n <= 20) { int replace = 0, cnt = 0; char prev = '~'; - for (char& curr : password) - { - if (curr == prev) ++cnt; - else - { + for (char& curr : password) { + if (curr == prev) + ++cnt; + else { replace += cnt / 3; cnt = 1; prev = curr; @@ -25,33 +23,28 @@ class Solution { int remove2 = 0; int cnt = 0; char prev = '~'; - for (char& curr : password) - { - if (curr == prev) ++cnt; - else - { - if (remove > 0 && cnt >= 3) - { - if (cnt % 3 == 0) - { + for (char& curr : password) { + if (curr == prev) + ++cnt; + else { + if (remove > 0 && cnt >= 3) { + if (cnt % 3 == 0) { --remove; --replace; - } - else if (cnt % 3 == 1) ++remove2; + } else if (cnt % 3 == 1) + ++remove2; } replace += cnt / 3; cnt = 1; prev = curr; } } - if (remove > 0 && cnt >= 3) - { - if (cnt % 3 == 0) - { + if (remove > 0 && cnt >= 3) { + if (cnt % 3 == 0) { --remove; --replace; - } - else if (cnt % 3 == 1) ++remove2; + } else if (cnt % 3 == 1) + ++remove2; } replace += cnt / 3; @@ -67,11 +60,13 @@ class Solution { int countTypes(string& s) { int a = 0, b = 0, c = 0; - for (char& ch : s) - { - if (islower(ch)) a = 1; - else if (isupper(ch)) b = 1; - else if (isdigit(ch)) c = 1; + for (char& ch : s) { + if (islower(ch)) + a = 1; + else if (isupper(ch)) + b = 1; + else if (isdigit(ch)) + c = 1; } return a + b + c; } diff --git a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp index a6f29f8656832..6795f17f7385d 100644 --- a/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp +++ b/solution/0400-0499/0421.Maximum XOR of Two Numbers in an Array/Solution.cpp @@ -2,12 +2,12 @@ class Trie { public: vector children; string v; - Trie() : children(2) {} + Trie() + : children(2) { } void insert(int x) { Trie* node = this; - for (int i = 30; ~i; --i) - { + for (int i = 30; ~i; --i) { int v = (x >> i) & 1; if (!node->children[v]) node->children[v] = new Trie(); node = node->children[v]; @@ -17,16 +17,12 @@ class Trie { int search(int x) { Trie* node = this; int res = 0; - for (int i = 30; ~i; --i) - { + for (int i = 30; ~i; --i) { int v = (x >> i) & 1; - if (node->children[v ^ 1]) - { + if (node->children[v ^ 1]) { res = res << 1 | 1; node = node->children[v ^ 1]; - } - else - { + } else { res <<= 1; node = node->children[v]; } @@ -40,8 +36,7 @@ class Solution { int findMaximumXOR(vector& nums) { Trie* trie = new Trie(); int ans = 0; - for (int v : nums) - { + for (int v : nums) { trie->insert(v); ans = max(ans, trie->search(v)); } diff --git a/solution/0400-0499/0424.Longest Repeating Character Replacement/Solution.cpp b/solution/0400-0499/0424.Longest Repeating Character Replacement/Solution.cpp index ce1c564a1edc8..0908d13a2a6c6 100644 --- a/solution/0400-0499/0424.Longest Repeating Character Replacement/Solution.cpp +++ b/solution/0400-0499/0424.Longest Repeating Character Replacement/Solution.cpp @@ -3,12 +3,10 @@ class Solution { int characterReplacement(string s, int k) { vector counter(26); int i = 0, j = 0, maxCnt = 0; - for (char& c : s) - { + for (char& c : s) { ++counter[c - 'A']; maxCnt = max(maxCnt, counter[c - 'A']); - if (i - j + 1 > maxCnt + k) - { + if (i - j + 1 > maxCnt + k) { --counter[s[j] - 'A']; ++j; } diff --git a/solution/0400-0499/0426.Convert Binary Search Tree to Sorted Doubly Linked List/Solution.cpp b/solution/0400-0499/0426.Convert Binary Search Tree to Sorted Doubly Linked List/Solution.cpp index efdb0de9bc2c1..52633603b7c99 100644 --- a/solution/0400-0499/0426.Convert Binary Search Tree to Sorted Doubly Linked List/Solution.cpp +++ b/solution/0400-0499/0426.Convert Binary Search Tree to Sorted Doubly Linked List/Solution.cpp @@ -34,18 +34,17 @@ class Solution { dfs(root); prev->right = head; head->left = prev; - return head; + return head; } void dfs(Node* root) { if (!root) return; dfs(root->left); - if (prev) - { + if (prev) { prev->right = root; root->left = prev; - } - else head = root; + } else + head = root; prev = root; dfs(root->right); } diff --git a/solution/0400-0499/0427.Construct Quad Tree/Solution.cpp b/solution/0400-0499/0427.Construct Quad Tree/Solution.cpp index e65405f41eb49..7d85b47239108 100644 --- a/solution/0400-0499/0427.Construct Quad Tree/Solution.cpp +++ b/solution/0400-0499/0427.Construct Quad Tree/Solution.cpp @@ -46,12 +46,12 @@ class Solution { Node* dfs(int a, int b, int c, int d, vector>& grid) { int zero = 0, one = 0; - for (int i = a; i <= c; ++i) - { - for (int j = b; j <= d; ++j) - { - if (grid[i][j]) one = 1; - else zero = 1; + for (int i = a; i <= c; ++i) { + for (int j = b; j <= d; ++j) { + if (grid[i][j]) + one = 1; + else + zero = 1; } } bool isLeaf = zero + one == 1; diff --git a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.cpp b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.cpp index e09ac5c0a1a24..9d6c476ac37fd 100644 --- a/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.cpp +++ b/solution/0400-0499/0429.N-ary Tree Level Order Traversal/Solution.cpp @@ -23,12 +23,10 @@ class Solution { vector> levelOrder(Node* root) { vector> ans; if (!root) return ans; - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { vector t; - for (int n = q.size(); n > 0; --n) - { + for (int n = q.size(); n > 0; --n) { root = q.front(); q.pop(); t.push_back(root->val); diff --git a/solution/0400-0499/0433.Minimum Genetic Mutation/Solution.cpp b/solution/0400-0499/0433.Minimum Genetic Mutation/Solution.cpp index 02521c0a1621a..49dcbee47352e 100644 --- a/solution/0400-0499/0433.Minimum Genetic Mutation/Solution.cpp +++ b/solution/0400-0499/0433.Minimum Genetic Mutation/Solution.cpp @@ -10,20 +10,16 @@ class Solution { mp['G'] = "ATC"; queue> q; q.push({start, 0}); - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); string t = p.first; int step = p.second; if (t == end) return step; - for (int i = 0; i < t.size(); ++i) - { - for (char c : mp[t[i]]) - { + for (int i = 0; i < t.size(); ++i) { + for (char c : mp[t[i]]) { string next = t.substr(0, i) + c + t.substr(i + 1, t.size() - i - 1); - if (s.count(next)) - { + if (s.count(next)) { q.push({next, step + 1}); s.erase(next); } diff --git a/solution/0400-0499/0434.Number of Segments in a String/Solution.cpp b/solution/0400-0499/0434.Number of Segments in a String/Solution.cpp index e014d2b2fe61c..1ac033bcb0e24 100644 --- a/solution/0400-0499/0434.Number of Segments in a String/Solution.cpp +++ b/solution/0400-0499/0434.Number of Segments in a String/Solution.cpp @@ -2,9 +2,8 @@ public: int countSegments(string s) { int res = 0; - for (int i = 0; i < s.size(); ++i) - { - if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) + for (int i = 0; i < s.size(); ++i) { + if (s[i] != ' ' && (i == 0 || s[i - 1] == ' ')) ++res; } return res; diff --git a/solution/0400-0499/0435.Non-overlapping Intervals/Solution.cpp b/solution/0400-0499/0435.Non-overlapping Intervals/Solution.cpp index 1f0140a5a4548..4bab38c48b5cd 100644 --- a/solution/0400-0499/0435.Non-overlapping Intervals/Solution.cpp +++ b/solution/0400-0499/0435.Non-overlapping Intervals/Solution.cpp @@ -1,12 +1,13 @@ class Solution { public: int eraseOverlapIntervals(vector>& intervals) { - sort(intervals.begin(), intervals.end(), [](const auto &a, const auto &b) { return a[1] < b[1]; }); + sort(intervals.begin(), intervals.end(), [](const auto& a, const auto& b) { return a[1] < b[1]; }); int ans = 0, t = intervals[0][1]; - for (int i = 1; i < intervals.size(); ++i) - { - if (t <= intervals[i][0]) t = intervals[i][1]; - else ++ans; + for (int i = 1; i < intervals.size(); ++i) { + if (t <= intervals[i][0]) + t = intervals[i][1]; + else + ++ans; } return ans; } diff --git a/solution/0400-0499/0436.Find Right Interval/Solution.cpp b/solution/0400-0499/0436.Find Right Interval/Solution.cpp index ed8a78deaa97e..28acbe2ec7db9 100644 --- a/solution/0400-0499/0436.Find Right Interval/Solution.cpp +++ b/solution/0400-0499/0436.Find Right Interval/Solution.cpp @@ -13,8 +13,10 @@ class Solution { int end = interval[1]; while (left < right) { int mid = left + right >> 1; - if (starts[mid].first >= end) right = mid; - else left = mid + 1; + if (starts[mid].first >= end) + right = mid; + else + left = mid + 1; } res.push_back(starts[left].first < end ? -1 : starts[left].second); } diff --git a/solution/0400-0499/0438.Find All Anagrams in a String/Solution.cpp b/solution/0400-0499/0438.Find All Anagrams in a String/Solution.cpp index 53435842e5d76..602c9e3bf49ff 100644 --- a/solution/0400-0499/0438.Find All Anagrams in a String/Solution.cpp +++ b/solution/0400-0499/0438.Find All Anagrams in a String/Solution.cpp @@ -6,12 +6,10 @@ vector ans; int left = 0, right = 0; vector t(26); - while (right < s.size()) - { + while (right < s.size()) { int i = s[right] - 'a'; ++t[i]; - while (t[i] > counter[i]) - { + while (t[i] > counter[i]) { --t[s[left] - 'a']; ++left; } diff --git a/solution/0400-0499/0440.K-th Smallest in Lexicographical Order/Solution.cpp b/solution/0400-0499/0440.K-th Smallest in Lexicographical Order/Solution.cpp index fed732ba57964..e31d086b3b354 100644 --- a/solution/0400-0499/0440.K-th Smallest in Lexicographical Order/Solution.cpp +++ b/solution/0400-0499/0440.K-th Smallest in Lexicographical Order/Solution.cpp @@ -1,33 +1,28 @@ class Solution { public: int n; - + int findKthNumber(int n, int k) { this->n = n; --k; long long curr = 1; - while (k) - { + while (k) { int cnt = count(curr); - if (k >= cnt) - { + if (k >= cnt) { k -= cnt; ++curr; - } - else - { + } else { --k; curr *= 10; } } - return (int) curr; + return (int)curr; } int count(long long curr) { long long next = curr + 1; int cnt = 0; - while (curr <= n) - { + while (curr <= n) { cnt += min(n - curr + 1, next - curr); next *= 10; curr *= 10; diff --git a/solution/0400-0499/0441.Arranging Coins/Solution.cpp b/solution/0400-0499/0441.Arranging Coins/Solution.cpp index 2a363bb9a982c..02f662fee2c6a 100644 --- a/solution/0400-0499/0441.Arranging Coins/Solution.cpp +++ b/solution/0400-0499/0441.Arranging Coins/Solution.cpp @@ -4,12 +4,13 @@ class Solution { public: int arrangeCoins(int n) { LL left = 1, right = n; - while (left < right) - { + while (left < right) { LL mid = left + right + 1 >> 1; LL s = (1 + mid) * mid >> 1; - if (n < s) right = mid - 1; - else left = mid; + if (n < s) + right = mid - 1; + else + left = mid; } return left; } diff --git a/solution/0400-0499/0442.Find All Duplicates in an Array/Solution.cpp b/solution/0400-0499/0442.Find All Duplicates in an Array/Solution.cpp index a45e08cef86df..5dd5c9bbe26cf 100644 --- a/solution/0400-0499/0442.Find All Duplicates in an Array/Solution.cpp +++ b/solution/0400-0499/0442.Find All Duplicates in an Array/Solution.cpp @@ -2,18 +2,14 @@ class Solution { public: vector findDuplicates(vector& nums) { int n = nums.size(); - for (int i = 0; i < n; ++i) - { - while (nums[i] != nums[nums[i] - 1]) - { + for (int i = 0; i < n; ++i) { + while (nums[i] != nums[nums[i] - 1]) { swap(nums[i], nums[nums[i] - 1]); } } vector ans; - for (int i = 0; i < n; ++i) - { - if (nums[i] != i + 1) - { + for (int i = 0; i < n; ++i) { + if (nums[i] != i + 1) { ans.push_back(nums[i]); } } diff --git a/solution/0400-0499/0443.String Compression/Solution.cpp b/solution/0400-0499/0443.String Compression/Solution.cpp index 61fde70fbb6e3..9213ee71a1f4b 100644 --- a/solution/0400-0499/0443.String Compression/Solution.cpp +++ b/solution/0400-0499/0443.String Compression/Solution.cpp @@ -1,16 +1,13 @@ class Solution { public: - int compress(vector &chars) { + int compress(vector& chars) { int k = 0, n = chars.size(); - for (int i = 0, j = i + 1; i < n;) - { + for (int i = 0, j = i + 1; i < n;) { while (j < n && chars[j] == chars[i]) ++j; chars[k++] = chars[i]; - if (j - i > 1) - { - for (char c : to_string(j - i)) - { + if (j - i > 1) { + for (char c : to_string(j - i)) { chars[k++] = c; } } diff --git a/solution/0400-0499/0444.Sequence Reconstruction/Solution.cpp b/solution/0400-0499/0444.Sequence Reconstruction/Solution.cpp index 82c93d32225f7..93f7e64eb5f51 100644 --- a/solution/0400-0499/0444.Sequence Reconstruction/Solution.cpp +++ b/solution/0400-0499/0444.Sequence Reconstruction/Solution.cpp @@ -4,23 +4,22 @@ class Solution { int n = nums.size(); vector> g(n); vector indeg(n); - for (auto& seq : sequences) - { - for (int i = 1; i < seq.size(); ++i) - { + for (auto& seq : sequences) { + for (int i = 1; i < seq.size(); ++i) { int a = seq[i - 1] - 1, b = seq[i] - 1; g[a].push_back(b); ++indeg[b]; } } queue q; - for (int i = 0; i < n; ++i) if (indeg[i] == 0) q.push(i); - while (!q.empty()) - { + for (int i = 0; i < n; ++i) + if (indeg[i] == 0) q.push(i); + while (!q.empty()) { if (q.size() > 1) return false; int i = q.front(); q.pop(); - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } return true; } diff --git a/solution/0400-0499/0445.Add Two Numbers II/Solution.cpp b/solution/0400-0499/0445.Add Two Numbers II/Solution.cpp index d14b2e87bcafe..5a2cb2da5bb2f 100644 --- a/solution/0400-0499/0445.Add Two Numbers II/Solution.cpp +++ b/solution/0400-0499/0445.Add Two Numbers II/Solution.cpp @@ -17,15 +17,12 @@ class Solution { for (; l2; l2 = l2->next) s2.push(l2->val); int carry = 0; ListNode* dummy = new ListNode(); - while (!s1.empty() || !s2.empty() || carry) - { - if (!s1.empty()) - { + while (!s1.empty() || !s2.empty() || carry) { + if (!s1.empty()) { carry += s1.top(); s1.pop(); } - if (!s2.empty()) - { + if (!s2.empty()) { carry += s2.top(); s2.pop(); } diff --git a/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp b/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp index bdad1b6af013b..a995317d75c56 100644 --- a/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp +++ b/solution/0400-0499/0447.Number of Boomerangs/Solution.cpp @@ -3,7 +3,7 @@ class Solution { int numberOfBoomerangs(vector>& points) { int ans = 0; for (const auto& p : points) { - unordered_map cnt; + unordered_map cnt; for (const auto& q : points) { ++cnt[(p[0] - q[0]) * (p[0] - q[0]) + (p[1] - q[1]) * (p[1] - q[1])]; } diff --git a/solution/0400-0499/0448.Find All Numbers Disappeared in an Array/Solution.cpp b/solution/0400-0499/0448.Find All Numbers Disappeared in an Array/Solution.cpp index ebfd90288ad55..faf881bc6b544 100644 --- a/solution/0400-0499/0448.Find All Numbers Disappeared in an Array/Solution.cpp +++ b/solution/0400-0499/0448.Find All Numbers Disappeared in an Array/Solution.cpp @@ -1,16 +1,14 @@ class Solution { public: - vector findDisappearedNumbers(vector &nums) { + vector findDisappearedNumbers(vector& nums) { int n = nums.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int idx = abs(nums[i]) - 1; if (nums[idx] > 0) nums[idx] *= -1; } vector res; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (nums[i] > 0) res.push_back(i + 1); } diff --git a/solution/0400-0499/0450.Delete Node in a BST/Solution.cpp b/solution/0400-0499/0450.Delete Node in a BST/Solution.cpp index be755649917fe..e188f931d247a 100644 --- a/solution/0400-0499/0450.Delete Node in a BST/Solution.cpp +++ b/solution/0400-0499/0450.Delete Node in a BST/Solution.cpp @@ -13,13 +13,11 @@ class Solution { public: TreeNode* deleteNode(TreeNode* root, int key) { if (!root) return root; - if (root->val > key) - { + if (root->val > key) { root->left = deleteNode(root->left, key); return root; } - if (root->val < key) - { + if (root->val < key) { root->right = deleteNode(root->right, key); return root; } diff --git a/solution/0400-0499/0452.Minimum Number of Arrows to Burst Balloons/Solution.cpp b/solution/0400-0499/0452.Minimum Number of Arrows to Burst Balloons/Solution.cpp index 6b34e71f051cd..4bb3f244f890b 100644 --- a/solution/0400-0499/0452.Minimum Number of Arrows to Burst Balloons/Solution.cpp +++ b/solution/0400-0499/0452.Minimum Number of Arrows to Burst Balloons/Solution.cpp @@ -6,11 +6,9 @@ class Solution { }); int ans = 1; int x = points[0][1]; - for (auto& v : points) - { + for (auto& v : points) { int a = v[0], b = v[1]; - if (a > x) - { + if (a > x) { ++ans; x = b; } diff --git a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution.cpp b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution.cpp index bc853276e4e80..eecc3dbcdc335 100644 --- a/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution.cpp +++ b/solution/0400-0499/0453.Minimum Moves to Equal Array Elements/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minMoves(vector& nums) { int s = 0; int mi = INT_MAX; - for (int num : nums) - { + for (int num : nums) { s += num; mi = min(mi, num); } diff --git a/solution/0400-0499/0456.132 Pattern/Solution.cpp b/solution/0400-0499/0456.132 Pattern/Solution.cpp index 0e1907f05d3c1..0174833adaf09 100644 --- a/solution/0400-0499/0456.132 Pattern/Solution.cpp +++ b/solution/0400-0499/0456.132 Pattern/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -40,8 +40,7 @@ class Solution { BinaryIndexedTree* tree = new BinaryIndexedTree(n); for (int v : nums) tree->update(m[v], 1); int mi = nums[0]; - for (int v : nums) - { + for (int v : nums) { tree->update(m[v], -1); if (tree->query(m[v] - 1) - tree->query(m[mi]) > 0) return true; mi = min(mi, v); diff --git a/solution/0400-0499/0463.Island Perimeter/Solution.cpp b/solution/0400-0499/0463.Island Perimeter/Solution.cpp index ce190795d5828..69cd1acea881b 100644 --- a/solution/0400-0499/0463.Island Perimeter/Solution.cpp +++ b/solution/0400-0499/0463.Island Perimeter/Solution.cpp @@ -3,12 +3,9 @@ class Solution { int islandPerimeter(vector>& grid) { int m = grid.size(), n = grid[0].size(); int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 1) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 1) { ans += 4; if (i < m - 1 && grid[i + 1][j] == 1) ans -= 2; if (j < n - 1 && grid[i][j + 1] == 1) ans -= 2; diff --git a/solution/0400-0499/0464.Can I Win/Solution.cpp b/solution/0400-0499/0464.Can I Win/Solution.cpp index 9b2bea2f86673..c550824bebf9c 100644 --- a/solution/0400-0499/0464.Can I Win/Solution.cpp +++ b/solution/0400-0499/0464.Can I Win/Solution.cpp @@ -10,11 +10,9 @@ class Solution { bool dfs(int state, int t, int maxChoosableInteger, int desiredTotal, unordered_map& memo) { if (memo.count(state)) return memo[state]; bool res = false; - for (int i = 1; i <= maxChoosableInteger; ++i) - { + for (int i = 1; i <= maxChoosableInteger; ++i) { if ((state >> i) & 1) continue; - if (t + i >= desiredTotal || !dfs(state | 1 << i, t + i, maxChoosableInteger, desiredTotal, memo)) - { + if (t + i >= desiredTotal || !dfs(state | 1 << i, t + i, maxChoosableInteger, desiredTotal, memo)) { res = true; break; } diff --git a/solution/0400-0499/0467.Unique Substrings in Wraparound String/Solution.cpp b/solution/0400-0499/0467.Unique Substrings in Wraparound String/Solution.cpp index 70d07af8efb12..785d20cda9659 100644 --- a/solution/0400-0499/0467.Unique Substrings in Wraparound String/Solution.cpp +++ b/solution/0400-0499/0467.Unique Substrings in Wraparound String/Solution.cpp @@ -3,11 +3,12 @@ class Solution { int findSubstringInWraproundString(string p) { vector dp(26); int k = 0; - for (int i = 0; i < p.size(); ++i) - { + for (int i = 0; i < p.size(); ++i) { char c = p[i]; - if (i && (c - p[i - 1] + 26) % 26 == 1) ++k; - else k = 1; + if (i && (c - p[i - 1] + 26) % 26 == 1) + ++k; + else + k = 1; dp[c - 'a'] = max(dp[c - 'a'], k); } int ans = 0; diff --git a/solution/0400-0499/0472.Concatenated Words/Solution.cpp b/solution/0400-0499/0472.Concatenated Words/Solution.cpp index b0188f6026482..47fbbfa678011 100644 --- a/solution/0400-0499/0472.Concatenated Words/Solution.cpp +++ b/solution/0400-0499/0472.Concatenated Words/Solution.cpp @@ -2,12 +2,13 @@ class Trie { public: vector children; bool isEnd; - Trie(): children(26), isEnd(false) {} + Trie() + : children(26) + , isEnd(false) { } void insert(string w) { Trie* node = this; - for (char c : w) - { + for (char c : w) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; @@ -21,14 +22,15 @@ class Solution { Trie* trie = new Trie(); vector findAllConcatenatedWordsInADict(vector& words) { - sort(words.begin(), words.end(), [&](const string & a, const string & b){ - return a.size() < b.size(); + sort(words.begin(), words.end(), [&](const string& a, const string& b) { + return a.size() < b.size(); }); vector ans; - for (auto& w : words) - { - if (dfs(w)) ans.push_back(w); - else trie->insert(w); + for (auto& w : words) { + if (dfs(w)) + ans.push_back(w); + else + trie->insert(w); } return ans; } @@ -36,8 +38,7 @@ class Solution { bool dfs(string w) { if (w == "") return true; Trie* node = trie; - for (int i = 0; i < w.size(); ++i) - { + for (int i = 0; i < w.size(); ++i) { int idx = w[i] - 'a'; if (!node->children[idx]) return false; node = node->children[idx]; diff --git a/solution/0400-0499/0473.Matchsticks to Square/Solution.cpp b/solution/0400-0499/0473.Matchsticks to Square/Solution.cpp index b198590088c1a..eb4bbe81a935c 100644 --- a/solution/0400-0499/0473.Matchsticks to Square/Solution.cpp +++ b/solution/0400-0499/0473.Matchsticks to Square/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool makesquare(vector& matchsticks) { int s = 0, mx = 0; - for (int& v : matchsticks) - { + for (int& v : matchsticks) { s += v; mx = max(mx, v); } @@ -16,8 +15,7 @@ class Solution { bool dfs(int u, int x, vector& matchsticks, vector& edges) { if (u == matchsticks.size()) return true; - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { if (i > 0 && edges[i - 1] == edges[i]) continue; edges[i] += matchsticks[u]; if (edges[i] <= x && dfs(u + 1, x, matchsticks, edges)) return true; diff --git a/solution/0400-0499/0474.Ones and Zeroes/Solution.cpp b/solution/0400-0499/0474.Ones and Zeroes/Solution.cpp index ed05cab54fde2..32b8d9d036fc5 100644 --- a/solution/0400-0499/0474.Ones and Zeroes/Solution.cpp +++ b/solution/0400-0499/0474.Ones and Zeroes/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int findMaxForm(vector& strs, int m, int n) { vector> dp(m + 1, vector(n + 1)); - for (auto s : strs) - { + for (auto s : strs) { vector t = count(s); for (int i = m; i >= t[0]; --i) for (int j = n; j >= t[1]; --j) @@ -16,6 +15,6 @@ class Solution { int n0 = 0; for (char c : s) if (c == '0') ++n0; - return {n0, (int) s.size() - n0}; + return {n0, (int)s.size() - n0}; } }; \ No newline at end of file diff --git a/solution/0400-0499/0475.Heaters/Solution.cpp b/solution/0400-0499/0475.Heaters/Solution.cpp index e48dab5c06d8e..2312bd7375036 100644 --- a/solution/0400-0499/0475.Heaters/Solution.cpp +++ b/solution/0400-0499/0475.Heaters/Solution.cpp @@ -6,8 +6,10 @@ class Solution { int left = 0, right = 1e9; while (left < right) { int mid = left + right >> 1; - if (check(houses, heaters, mid)) right = mid; - else left = mid + 1; + if (check(houses, heaters, mid)) + right = mid; + else + left = mid + 1; } return left; } @@ -15,14 +17,15 @@ class Solution { bool check(vector& houses, vector& heaters, int r) { int m = houses.size(), n = heaters.size(); int i = 0, j = 0; - while (i < m) - { + while (i < m) { if (j >= n) return false; int mi = heaters[j] - r; int mx = heaters[j] + r; if (houses[i] < mi) return false; - if (houses[i] > mx) ++j; - else ++i; + if (houses[i] > mx) + ++j; + else + ++i; } return true; } diff --git a/solution/0400-0499/0476.Number Complement/Solution.cpp b/solution/0400-0499/0476.Number Complement/Solution.cpp index d50d78350481e..6de34695822b2 100644 --- a/solution/0400-0499/0476.Number Complement/Solution.cpp +++ b/solution/0400-0499/0476.Number Complement/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int findComplement(int num) { int ans = 0; bool find = false; - for (int i = 30; i >= 0; --i) - { + for (int i = 30; i >= 0; --i) { int b = num & (1 << i); if (!find && b == 0) continue; find = true; diff --git a/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp b/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp index 8fbfb5131ed29..5a0c07ffec55e 100644 --- a/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp +++ b/solution/0400-0499/0477.Total Hamming Distance/Solution.cpp @@ -2,11 +2,9 @@ class Solution { public: int totalHammingDistance(vector& nums) { int ans = 0; - for (int i = 0; i < 31; ++i) - { + for (int i = 0; i < 31; ++i) { int a = 0, b = 0; - for (int& v : nums) - { + for (int& v : nums) { int t = (v >> i) & 1; a += t; b += t ^ 1; diff --git a/solution/0400-0499/0479.Largest Palindrome Product/Solution.cpp b/solution/0400-0499/0479.Largest Palindrome Product/Solution.cpp index 06e570b22e40d..fb09efbe4d301 100644 --- a/solution/0400-0499/0479.Largest Palindrome Product/Solution.cpp +++ b/solution/0400-0499/0479.Largest Palindrome Product/Solution.cpp @@ -2,12 +2,10 @@ class Solution { public: int largestPalindrome(int n) { int mx = pow(10, n) - 1; - for (int a = mx; a > mx / 10; --a) - { + for (int a = mx; a > mx / 10; --a) { int b = a; long x = a; - while (b) - { + while (b) { x = x * 10 + b % 10; b /= 10; } diff --git a/solution/0400-0499/0482.License Key Formatting/Solution.cpp b/solution/0400-0499/0482.License Key Formatting/Solution.cpp index c21b95ececdda..cd84af731406e 100644 --- a/solution/0400-0499/0482.License Key Formatting/Solution.cpp +++ b/solution/0400-0499/0482.License Key Formatting/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: string licenseKeyFormatting(string s, int k) { string ss = ""; - for (char c : s) - { + for (char c : s) { if (c == '-') continue; if ('a' <= c && c <= 'z') c += 'A' - 'a'; ss += c; @@ -12,12 +11,10 @@ class Solution { if (cnt == 0) cnt = k; int t = 0; string res = ""; - for (int i = 0; i < ss.size(); ++i) - { + for (int i = 0; i < ss.size(); ++i) { res += ss[i]; ++t; - if (t == cnt) - { + if (t == cnt) { t = 0; cnt = k; if (i != ss.size() - 1) res += '-'; diff --git a/solution/0400-0499/0489.Robot Room Cleaner/Solution.cpp b/solution/0400-0499/0489.Robot Room Cleaner/Solution.cpp index 0db8e13ecef10..af68060131f8a 100644 --- a/solution/0400-0499/0489.Robot Room Cleaner/Solution.cpp +++ b/solution/0400-0499/0489.Robot Room Cleaner/Solution.cpp @@ -29,13 +29,11 @@ class Solution { void dfs(int i, int j, int d, unordered_set& vis, Robot& robot) { vis.insert(to_string(i) + "," + to_string(j)); robot.clean(); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int nd = (d + k) % 4; int x = i + dirs[nd][0]; int y = j + dirs[nd][1]; - if (!vis.count(to_string(x) + "," + to_string(y)) && robot.move()) - { + if (!vis.count(to_string(x) + "," + to_string(y)) && robot.move()) { dfs(x, y, nd, vis, robot); back(robot); } diff --git a/solution/0400-0499/0490.The Maze/Solution.cpp b/solution/0400-0499/0490.The Maze/Solution.cpp index 62028765b6e33..2d27d49d79d8d 100644 --- a/solution/0400-0499/0490.The Maze/Solution.cpp +++ b/solution/0400-0499/0490.The Maze/Solution.cpp @@ -3,27 +3,23 @@ class Solution { bool hasPath(vector>& maze, vector& start, vector& destination) { int m = maze.size(); int n = maze[0].size(); - queue> q{{start}}; + queue> q {{start}}; vector> vis(m, vector(n)); vis[start[0]][start[1]] = true; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); int i = p[0], j = p[1]; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i, y = j; int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0) - { + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0) { x += a; y += b; } if (x == destination[0] && y == destination[1]) return 1; - if (!vis[x][y]) - { + if (!vis[x][y]) { vis[x][y] = true; q.push({x, y}); } diff --git a/solution/0400-0499/0491.Increasing Subsequences/Solution.cpp b/solution/0400-0499/0491.Increasing Subsequences/Solution.cpp index 48c91d4bf8114..5108dee0d3ba2 100644 --- a/solution/0400-0499/0491.Increasing Subsequences/Solution.cpp +++ b/solution/0400-0499/0491.Increasing Subsequences/Solution.cpp @@ -8,13 +8,11 @@ class Solution { } void dfs(int u, int last, vector& t, vector& nums, vector>& ans) { - if (u == nums.size()) - { + if (u == nums.size()) { if (t.size() > 1) ans.push_back(t); return; } - if (nums[u] >= last) - { + if (nums[u] >= last) { t.push_back(nums[u]); dfs(u + 1, nums[u], t, nums, ans); t.pop_back(); diff --git a/solution/0400-0499/0493.Reverse Pairs/Solution.cpp b/solution/0400-0499/0493.Reverse Pairs/Solution.cpp index 31be7b36217e6..92d4ad319bbb9 100644 --- a/solution/0400-0499/0493.Reverse Pairs/Solution.cpp +++ b/solution/0400-0499/0493.Reverse Pairs/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -32,8 +32,7 @@ class Solution { public: int reversePairs(vector& nums) { set s; - for (int num : nums) - { + for (int num : nums) { s.insert(num); s.insert(num * 2ll); } @@ -42,8 +41,7 @@ class Solution { for (long long num : s) m[num] = ++idx; BinaryIndexedTree* tree = new BinaryIndexedTree(m.size()); int ans = 0; - for (int i = nums.size() - 1; i >= 0; --i) - { + for (int i = nums.size() - 1; i >= 0; --i) { ans += tree->query(m[nums[i]] - 1); tree->update(m[nums[i] * 2ll], 1); } diff --git a/solution/0400-0499/0496.Next Greater Element I/Solution.cpp b/solution/0400-0499/0496.Next Greater Element I/Solution.cpp index 5005af450dfef..5f7136337d3d2 100644 --- a/solution/0400-0499/0496.Next Greater Element I/Solution.cpp +++ b/solution/0400-0499/0496.Next Greater Element I/Solution.cpp @@ -3,10 +3,8 @@ class Solution { vector nextGreaterElement(vector& nums1, vector& nums2) { stack stk; unordered_map m; - for (int& v : nums2) - { - while (!stk.empty() && stk.top() < v) - { + for (int& v : nums2) { + while (!stk.empty() && stk.top() < v) { m[stk.top()] = v; stk.pop(); } diff --git a/solution/0400-0499/0497.Random Point in Non-overlapping Rectangles/Solution.cpp b/solution/0400-0499/0497.Random Point in Non-overlapping Rectangles/Solution.cpp index d6f20860cca95..4411292163e7a 100644 --- a/solution/0400-0499/0497.Random Point in Non-overlapping Rectangles/Solution.cpp +++ b/solution/0400-0499/0497.Random Point in Non-overlapping Rectangles/Solution.cpp @@ -10,7 +10,7 @@ class Solution { this->rects = rects; srand(time(nullptr)); } - + vector pick() { int n = rects.size(); int v = 1 + rand() % s[n]; diff --git a/solution/0400-0499/0498.Diagonal Traverse/Solution.cpp b/solution/0400-0499/0498.Diagonal Traverse/Solution.cpp index a3cba04ad3f80..7925fea3b122a 100644 --- a/solution/0400-0499/0498.Diagonal Traverse/Solution.cpp +++ b/solution/0400-0499/0498.Diagonal Traverse/Solution.cpp @@ -4,8 +4,7 @@ class Solution { int m = mat.size(), n = mat[0].size(); vector ans; vector t; - for (int k = 0; k < m + n - 1; ++k) - { + for (int k = 0; k < m + n - 1; ++k) { int i = k < n ? 0 : k - n + 1; int j = k < n ? k : n - 1; while (i < m && j >= 0) t.push_back(mat[i++][j--]); diff --git a/solution/0400-0499/0499.The Maze III/Solution.cpp b/solution/0400-0499/0499.The Maze III/Solution.cpp index 37526316972d2..182b02c5c61f3 100644 --- a/solution/0400-0499/0499.The Maze III/Solution.cpp +++ b/solution/0400-0499/0499.The Maze III/Solution.cpp @@ -11,25 +11,21 @@ class Solution { dist[r][c] = 0; vector> path(m, vector(n, "")); vector> dirs = {{-1, 0, 'u'}, {1, 0, 'd'}, {0, -1, 'l'}, {0, 1, 'r'}}; - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); int i = p.first, j = p.second; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int a = dir[0], b = dir[1]; - char d = (char) dir[2]; + char d = (char)dir[2]; int x = i, y = j; int step = dist[i][j]; - while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0 && (x != rh || y != ch)) - { + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0 && (x != rh || y != ch)) { x += a; y += b; ++step; } - if (dist[x][y] > step || (dist[x][y] == step && (path[i][j] + d < path[x][y]))) - { + if (dist[x][y] > step || (dist[x][y] == step && (path[i][j] + d < path[x][y]))) { dist[x][y] = step; path[x][y] = path[i][j] + d; if (x != rh || y != ch) q.push({x, y}); diff --git a/solution/0500-0599/0500.Keyboard Row/Solution.cpp b/solution/0500-0599/0500.Keyboard Row/Solution.cpp index f43cd818010be..b7c40158e9e9c 100644 --- a/solution/0500-0599/0500.Keyboard Row/Solution.cpp +++ b/solution/0500-0599/0500.Keyboard Row/Solution.cpp @@ -3,11 +3,10 @@ class Solution { vector findWords(vector& words) { string s = "12210111011122000010020202"; vector ans; - for (auto& word : words) - { + for (auto& word : words) { unordered_set t; for (char c : word) t.insert(s[tolower(c) - 'a']); - if (t.size() == 1) ans.push_back(word); + if (t.size() == 1) ans.push_back(word); } return ans; } diff --git a/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.cpp b/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.cpp index 9d7a6e6eae223..78ab0e68e1a9c 100644 --- a/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.cpp +++ b/solution/0500-0599/0501.Find Mode in Binary Search Tree/Solution.cpp @@ -24,13 +24,12 @@ class Solution { if (!root) return; dfs(root->left); cnt = prev != nullptr && prev->val == root->val ? cnt + 1 : 1; - if (cnt > mx) - { + if (cnt > mx) { ans.clear(); ans.push_back(root->val); - mx = cnt; - } - else if (cnt == mx) ans.push_back(root->val); + mx = cnt; + } else if (cnt == mx) + ans.push_back(root->val); prev = root; dfs(root->right); } diff --git a/solution/0500-0599/0503.Next Greater Element II/Solution.cpp b/solution/0500-0599/0503.Next Greater Element II/Solution.cpp index c5a3dd15c27b8..6fa805bf03ee1 100644 --- a/solution/0500-0599/0503.Next Greater Element II/Solution.cpp +++ b/solution/0500-0599/0503.Next Greater Element II/Solution.cpp @@ -1,13 +1,11 @@ class Solution { public: - vector nextGreaterElements(vector &nums) { + vector nextGreaterElements(vector& nums) { int n = nums.size(); vector ans(n, -1); stack stk; - for (int i = 0; i < (n << 1); ++i) - { - while (!stk.empty() && nums[stk.top()] < nums[i % n]) - { + for (int i = 0; i < (n << 1); ++i) { + while (!stk.empty() && nums[stk.top()] < nums[i % n]) { ans[stk.top()] = nums[i % n]; stk.pop(); } diff --git a/solution/0500-0599/0504.Base 7/Solution.cpp b/solution/0500-0599/0504.Base 7/Solution.cpp index 7fc652a2128aa..82ee8050e5749 100644 --- a/solution/0500-0599/0504.Base 7/Solution.cpp +++ b/solution/0500-0599/0504.Base 7/Solution.cpp @@ -4,8 +4,7 @@ class Solution { if (num == 0) return "0"; if (num < 0) return "-" + convertToBase7(-num); string ans = ""; - while (num) - { + while (num) { ans = to_string(num % 7) + ans; num /= 7; } diff --git a/solution/0500-0599/0505.The Maze II/Solution.cpp b/solution/0500-0599/0505.The Maze II/Solution.cpp index 85d04eab5a93b..62c611600253e 100644 --- a/solution/0500-0599/0505.The Maze II/Solution.cpp +++ b/solution/0500-0599/0505.The Maze II/Solution.cpp @@ -5,25 +5,21 @@ class Solution { int n = maze[0].size(); vector> dist(m, vector(n, INT_MAX)); dist[start[0]][start[1]] = 0; - queue> q{{start}}; + queue> q {{start}}; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); int i = p[0], j = p[1]; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i, y = j, step = dist[i][j]; int a = dirs[k], b = dirs[k + 1]; - while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0) - { + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && maze[x + a][y + b] == 0) { x += a; y += b; ++step; } - if (step < dist[x][y]) - { + if (step < dist[x][y]) { dist[x][y] = step; q.push({x, y}); } diff --git a/solution/0500-0599/0506.Relative Ranks/Solution.cpp b/solution/0500-0599/0506.Relative Ranks/Solution.cpp index c5d8c13c9c525..6c1b3cd320c0b 100644 --- a/solution/0500-0599/0506.Relative Ranks/Solution.cpp +++ b/solution/0500-0599/0506.Relative Ranks/Solution.cpp @@ -1,13 +1,12 @@ class Solution { public: - vector findRelativeRanks(vector &score) { + vector findRelativeRanks(vector& score) { int n = score.size(); vector> idx; for (int i = 0; i < n; ++i) idx.push_back(make_pair(score[i], i)); sort(idx.begin(), idx.end(), - [&](const pair &x, const pair &y) - { return x.first > y.first; }); + [&](const pair& x, const pair& y) { return x.first > y.first; }); vector ans(n); vector top3 = {"Gold Medal", "Silver Medal", "Bronze Medal"}; for (int i = 0; i < n; ++i) diff --git a/solution/0500-0599/0507.Perfect Number/Solution.cpp b/solution/0500-0599/0507.Perfect Number/Solution.cpp index 856f7a8938398..ed7c5d58c6625 100644 --- a/solution/0500-0599/0507.Perfect Number/Solution.cpp +++ b/solution/0500-0599/0507.Perfect Number/Solution.cpp @@ -3,10 +3,8 @@ class Solution { bool checkPerfectNumber(int num) { if (num == 1) return false; int s = 1; - for (int i = 2; i * i <= num; ++i) - { - if (num % i == 0) - { + for (int i = 2; i * i <= num; ++i) { + if (num % i == 0) { s += i; if (i != num / i) s += num / i; } diff --git a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp index 4a1ddd156f70e..9c0a7f8119529 100644 --- a/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp +++ b/solution/0500-0599/0510.Inorder Successor in BST II/Solution.cpp @@ -12,8 +12,7 @@ class Node { class Solution { public: Node* inorderSuccessor(Node* node) { - if (node->right) - { + if (node->right) { node = node->right; while (node->left) node = node->left; return node; diff --git a/solution/0500-0599/0513.Find Bottom Left Tree Value/Solution.cpp b/solution/0500-0599/0513.Find Bottom Left Tree Value/Solution.cpp index 030bb4b831295..36616d48d9d83 100644 --- a/solution/0500-0599/0513.Find Bottom Left Tree Value/Solution.cpp +++ b/solution/0500-0599/0513.Find Bottom Left Tree Value/Solution.cpp @@ -12,13 +12,11 @@ class Solution { public: int findBottomLeftValue(TreeNode* root) { - queue q{{root}}; + queue q {{root}}; int ans = 0; - while (!q.empty()) - { + while (!q.empty()) { ans = q.front()->val; - for (int i = q.size(); i; --i) - { + for (int i = q.size(); i; --i) { TreeNode* node = q.front(); q.pop(); if (node->left) q.push(node->left); diff --git a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp index f75ba646a515a..6e9bee6e682df 100644 --- a/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp +++ b/solution/0500-0599/0515.Find Largest Value in Each Tree Row/Solution.cpp @@ -13,13 +13,11 @@ class Solution { public: vector largestValues(TreeNode* root) { if (!root) return {}; - queue q{{root}}; + queue q {{root}}; vector ans; - while (!q.empty()) - { + while (!q.empty()) { int t = q.front()->val; - for (int i = q.size(); i; --i) - { + for (int i = q.size(); i; --i) { TreeNode* node = q.front(); t = max(t, node->val); q.pop(); diff --git a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp index 7db51a22382a8..fe277abe3c7c3 100644 --- a/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp +++ b/solution/0500-0599/0522.Longest Uncommon Subsequence II/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: int findLUSlength(vector& strs) { int ans = -1; - for (int i = 0, j = 0, n = strs.size(); i < n; ++i) - { - for (j = 0; j < n; ++j) - { + for (int i = 0, j = 0, n = strs.size(); i < n; ++i) { + for (j = 0; j < n; ++j) { if (i == j) continue; if (check(strs[j], strs[i])) break; } diff --git a/solution/0500-0599/0523.Continuous Subarray Sum/Solution.cpp b/solution/0500-0599/0523.Continuous Subarray Sum/Solution.cpp index c653194c9f616..cd6aef247f7a4 100644 --- a/solution/0500-0599/0523.Continuous Subarray Sum/Solution.cpp +++ b/solution/0500-0599/0523.Continuous Subarray Sum/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map mp; mp[0] = -1; int s = 0; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { s += nums[i]; int r = s % k; if (mp.count(r) && i - mp[r] >= 2) return true; diff --git a/solution/0500-0599/0524.Longest Word in Dictionary through Deleting/Solution.cpp b/solution/0500-0599/0524.Longest Word in Dictionary through Deleting/Solution.cpp index af5475ae16028..ceb48033eb694 100644 --- a/solution/0500-0599/0524.Longest Word in Dictionary through Deleting/Solution.cpp +++ b/solution/0500-0599/0524.Longest Word in Dictionary through Deleting/Solution.cpp @@ -11,8 +11,7 @@ class Solution { bool check(string& a, string& b) { int m = a.size(), n = b.size(); int i = 0, j = 0; - while (i < m && j < n) - { + while (i < m && j < n) { if (a[i] == b[j]) ++j; ++i; } diff --git a/solution/0500-0599/0525.Contiguous Array/Solution.cpp b/solution/0500-0599/0525.Contiguous Array/Solution.cpp index bf4a907bf60b8..8344d29f987e5 100644 --- a/solution/0500-0599/0525.Contiguous Array/Solution.cpp +++ b/solution/0500-0599/0525.Contiguous Array/Solution.cpp @@ -4,11 +4,12 @@ class Solution { unordered_map mp; int s = 0, ans = 0; mp[0] = -1; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { s += nums[i] == 1 ? 1 : -1; - if (mp.count(s)) ans = max(ans, i - mp[s]); - else mp[s] = i; + if (mp.count(s)) + ans = max(ans, i - mp[s]); + else + mp[s] = i; } return ans; } diff --git a/solution/0500-0599/0526.Beautiful Arrangement/Solution.cpp b/solution/0500-0599/0526.Beautiful Arrangement/Solution.cpp index a6bfff0edee38..2fa376f348275 100644 --- a/solution/0500-0599/0526.Beautiful Arrangement/Solution.cpp +++ b/solution/0500-0599/0526.Beautiful Arrangement/Solution.cpp @@ -18,15 +18,12 @@ class Solution { } void dfs(int i) { - if (i == n + 1) - { + if (i == n + 1) { ++ans; return; } - for (int j : match[i]) - { - if (!vis[j]) - { + for (int j : match[i]) { + if (!vis[j]) { vis[j] = true; dfs(i + 1); vis[j] = false; diff --git a/solution/0500-0599/0528.Random Pick with Weight/Solution.cpp b/solution/0500-0599/0528.Random Pick with Weight/Solution.cpp index 705b760101919..c19d55934b8bb 100644 --- a/solution/0500-0599/0528.Random Pick with Weight/Solution.cpp +++ b/solution/0500-0599/0528.Random Pick with Weight/Solution.cpp @@ -7,16 +7,17 @@ class Solution { s.resize(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] + w[i]; } - + int pickIndex() { int n = s.size(); int x = 1 + rand() % s[n - 1]; int left = 1, right = n - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (s[mid] >= x) right = mid; - else left = mid + 1; + if (s[mid] >= x) + right = mid; + else + left = mid + 1; } return left - 1; } diff --git a/solution/0500-0599/0531.Lonely Pixel I/Solution.cpp b/solution/0500-0599/0531.Lonely Pixel I/Solution.cpp index 6cc3fb4d9dd5f..a46012d519c2c 100644 --- a/solution/0500-0599/0531.Lonely Pixel I/Solution.cpp +++ b/solution/0500-0599/0531.Lonely Pixel I/Solution.cpp @@ -4,26 +4,19 @@ class Solution { int m = picture.size(), n = picture[0].size(); vector rows(m); vector cols(n); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (picture[i][j] == 'B') - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (picture[i][j] == 'B') { ++rows[i]; ++cols[j]; } } } int res = 0; - for (int i = 0; i < m; ++i) - { - if (rows[i] == 1) - { - for (int j = 0; j < n; ++j) - { - if (picture[i][j] == 'B' && cols[j] == 1) - { + for (int i = 0; i < m; ++i) { + if (rows[i] == 1) { + for (int j = 0; j < n; ++j) { + if (picture[i][j] == 'B' && cols[j] == 1) { ++res; break; } diff --git a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp index da22498695d09..63a75d171b756 100644 --- a/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp +++ b/solution/0500-0599/0532.K-diff Pairs in an Array/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int findPairs(vector& nums, int k) { unordered_set vis; unordered_set ans; - for (int& v : nums) - { + for (int& v : nums) { if (vis.count(v - k)) ans.insert(v - k); if (vis.count(v + k)) ans.insert(v); vis.insert(v); diff --git a/solution/0500-0599/0533.Lonely Pixel II/Solution.cpp b/solution/0500-0599/0533.Lonely Pixel II/Solution.cpp index 756468ef7d0a8..7f298b339c063 100644 --- a/solution/0500-0599/0533.Lonely Pixel II/Solution.cpp +++ b/solution/0500-0599/0533.Lonely Pixel II/Solution.cpp @@ -4,35 +4,26 @@ class Solution { int m = picture.size(), n = picture[0].size(); vector rows(m); unordered_map> cols; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (picture[i][j] == 'B') - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (picture[i][j] == 'B') { ++rows[i]; cols[j].push_back(i); } } } vector> t(m, vector(m, false)); - for (int i = 0; i < m; ++i) - { - for (int k = i; k < m; ++k) - { + for (int i = 0; i < m; ++i) { + for (int k = i; k < m; ++k) { t[i][k] = i == k || all(picture[i], picture[k]); t[k][i] = t[i][k]; } } int res = 0; - for (int i = 0; i < m; ++i) - { - if (rows[i] == target) - { - for (int j = 0; j < n; ++j) - { - if (cols[j].size() == target) - { + for (int i = 0; i < m; ++i) { + if (rows[i] == target) { + for (int j = 0; j < n; ++j) { + if (cols[j].size() == target) { bool check = true; for (int k : cols[j]) check = check && t[i][k]; if (check) ++res; diff --git a/solution/0500-0599/0535.Encode and Decode TinyURL/Solution.cpp b/solution/0500-0599/0535.Encode and Decode TinyURL/Solution.cpp index 36c83c60918cf..4b336d33d49bd 100644 --- a/solution/0500-0599/0535.Encode and Decode TinyURL/Solution.cpp +++ b/solution/0500-0599/0535.Encode and Decode TinyURL/Solution.cpp @@ -1,6 +1,5 @@ class Solution { public: - // Encodes a URL to a shortened URL. string encode(string longUrl) { string v = to_string(++idx); diff --git a/solution/0500-0599/0536.Construct Binary Tree from String/Solution.cpp b/solution/0500-0599/0536.Construct Binary Tree from String/Solution.cpp index 1b92a09aa2cbc..93ee46871d2c5 100644 --- a/solution/0500-0599/0536.Construct Binary Tree from String/Solution.cpp +++ b/solution/0500-0599/0536.Construct Binary Tree from String/Solution.cpp @@ -22,18 +22,17 @@ class Solution { TreeNode* root = new TreeNode(stoi(s.substr(0, p))); int start = p; int cnt = 0; - for (int i = p; i < s.size(); ++i) - { - if (s[i] == '(') ++cnt; - else if (s[i] == ')') --cnt; - if (cnt == 0) - { - if (start == p) - { + for (int i = p; i < s.size(); ++i) { + if (s[i] == '(') + ++cnt; + else if (s[i] == ')') + --cnt; + if (cnt == 0) { + if (start == p) { root->left = dfs(s.substr(start + 1, i - start - 1)); start = i + 1; - } - else root->right = dfs(s.substr(start + 1, i - start - 1)); + } else + root->right = dfs(s.substr(start + 1, i - start - 1)); } } return root; diff --git a/solution/0500-0599/0538.Convert BST to Greater Tree/Solution.cpp b/solution/0500-0599/0538.Convert BST to Greater Tree/Solution.cpp index dfb0ec14551c0..4f15c632cf1ad 100644 --- a/solution/0500-0599/0538.Convert BST to Greater Tree/Solution.cpp +++ b/solution/0500-0599/0538.Convert BST to Greater Tree/Solution.cpp @@ -11,31 +11,23 @@ */ class Solution { public: - TreeNode *convertBST(TreeNode *root) { + TreeNode* convertBST(TreeNode* root) { int s = 0; - TreeNode *node = root; - while (root) - { - if (root->right == nullptr) - { + TreeNode* node = root; + while (root) { + if (root->right == nullptr) { s += root->val; root->val = s; root = root->left; - } - else - { - TreeNode *next = root->right; - while (next->left && next->left != root) - { + } else { + TreeNode* next = root->right; + while (next->left && next->left != root) { next = next->left; } - if (next->left == nullptr) - { + if (next->left == nullptr) { next->left = root; root = root->right; - } - else - { + } else { s += root->val; root->val = s; next->left = nullptr; diff --git a/solution/0500-0599/0539.Minimum Time Difference/Solution.cpp b/solution/0500-0599/0539.Minimum Time Difference/Solution.cpp index 18c4fd094f2f1..16bf8b7636d04 100644 --- a/solution/0500-0599/0539.Minimum Time Difference/Solution.cpp +++ b/solution/0500-0599/0539.Minimum Time Difference/Solution.cpp @@ -1,6 +1,6 @@ class Solution { public: - int findMinDifference(vector &timePoints) { + int findMinDifference(vector& timePoints) { if (timePoints.size() > 24 * 60) return 0; vector mins; diff --git a/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.cpp b/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.cpp index f27306f6d7b4a..5668de91bd506 100644 --- a/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.cpp +++ b/solution/0500-0599/0540.Single Element in a Sorted Array/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: int singleNonDuplicate(vector& nums) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] != nums[mid ^ 1]) right = mid; - else left = mid + 1; + if (nums[mid] != nums[mid ^ 1]) + right = mid; + else + left = mid + 1; } return nums[left]; } diff --git a/solution/0500-0599/0541.Reverse String II/Solution.cpp b/solution/0500-0599/0541.Reverse String II/Solution.cpp index c44f4a31c935f..528788dee6aad 100644 --- a/solution/0500-0599/0541.Reverse String II/Solution.cpp +++ b/solution/0500-0599/0541.Reverse String II/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: string reverseStr(string s, int k) { - for (int i = 0, n = s.size(); i < n; i += (k << 1)) - { + for (int i = 0, n = s.size(); i < n; i += (k << 1)) { reverse(s.begin() + i, s.begin() + min(i + k, n)); } return s; diff --git a/solution/0500-0599/0542.01 Matrix/Solution.cpp b/solution/0500-0599/0542.01 Matrix/Solution.cpp index 9ce4fb2904568..f36023e9bad31 100644 --- a/solution/0500-0599/0542.01 Matrix/Solution.cpp +++ b/solution/0500-0599/0542.01 Matrix/Solution.cpp @@ -4,28 +4,22 @@ class Solution { int m = mat.size(), n = mat[0].size(); vector> ans(m, vector(n, -1)); queue> q; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (mat[i][j] == 0) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (mat[i][j] == 0) { ans[i][j] = 0; q.emplace(i, j); } } } vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { int x = p.first + dirs[i]; int y = p.second + dirs[i + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) - { + if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) { ans[x][y] = ans[p.first][p.second] + 1; q.emplace(x, y); } diff --git a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp index 59fdf90243417..e0a83d57f1736 100644 --- a/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp +++ b/solution/0500-0599/0543.Diameter of Binary Tree/Solution.cpp @@ -12,7 +12,7 @@ class Solution { public: int ans; - + int diameterOfBinaryTree(TreeNode* root) { ans = 0; dfs(root); diff --git a/solution/0500-0599/0544.Output Contest Matches/Solution.cpp b/solution/0500-0599/0544.Output Contest Matches/Solution.cpp index 1f83f051b40bf..b9a69628d986e 100644 --- a/solution/0500-0599/0544.Output Contest Matches/Solution.cpp +++ b/solution/0500-0599/0544.Output Contest Matches/Solution.cpp @@ -3,10 +3,8 @@ class Solution { string findContestMatch(int n) { vector team(n); for (int i = 0; i < n; ++i) team[i] = to_string(i + 1); - for (; n > 1; n >>= 1) - { - for (int i = 0; i < n >> 1; ++i) - { + for (; n > 1; n >>= 1) { + for (int i = 0; i > 1; ++i) { team[i] = "(" + team[i] + "," + team[n - 1 - i] + ")"; } } diff --git a/solution/0500-0599/0547.Number of Provinces/Solution.cpp b/solution/0500-0599/0547.Number of Provinces/Solution.cpp index 5e92b241a1dac..940a5267c4489 100644 --- a/solution/0500-0599/0547.Number of Provinces/Solution.cpp +++ b/solution/0500-0599/0547.Number of Provinces/Solution.cpp @@ -9,10 +9,8 @@ class Solution { vis.resize(n); this->isConnected = isConnected; int ans = 0; - for (int i = 0; i < n; ++i) - { - if (!vis[i]) - { + for (int i = 0; i < n; ++i) { + if (!vis[i]) { dfs(i); ++ans; } diff --git a/solution/0500-0599/0548.Split Array with Equal Sum/Solution.cpp b/solution/0500-0599/0548.Split Array with Equal Sum/Solution.cpp index e8eed8989fcbd..93f978a44b70c 100644 --- a/solution/0500-0599/0548.Split Array with Equal Sum/Solution.cpp +++ b/solution/0500-0599/0548.Split Array with Equal Sum/Solution.cpp @@ -4,15 +4,14 @@ class Solution { int n = nums.size(); vector s(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; - for (int j = 3; j < n - 3; ++j) - { + for (int j = 3; j < n - 3; ++j) { unordered_set seen; for (int i = 1; i < j - 1; ++i) if (s[i] == s[j] - s[i + 1]) seen.insert(s[i]); for (int k = j + 2; k < n - 1; ++k) if (s[n] - s[k + 1] == s[k] - s[j + 1] && seen.count(s[n] - s[k + 1])) - return true; + return true; } return false; } diff --git a/solution/0500-0599/0549.Binary Tree Longest Consecutive Sequence II/Solution.cpp b/solution/0500-0599/0549.Binary Tree Longest Consecutive Sequence II/Solution.cpp index b7ab47876d436..4929a0a2d970d 100644 --- a/solution/0500-0599/0549.Binary Tree Longest Consecutive Sequence II/Solution.cpp +++ b/solution/0500-0599/0549.Binary Tree Longest Consecutive Sequence II/Solution.cpp @@ -24,13 +24,11 @@ class Solution { int incr = 1, decr = 1; auto left = dfs(root->left); auto right = dfs(root->right); - if (root->left) - { + if (root->left) { if (root->left->val + 1 == root->val) incr = left[0] + 1; if (root->left->val - 1 == root->val) decr = left[1] + 1; } - if (root->right) - { + if (root->right) { if (root->right->val + 1 == root->val) incr = max(incr, right[0] + 1); if (root->right->val - 1 == root->val) decr = max(decr, right[1] + 1); } diff --git a/solution/0500-0599/0551.Student Attendance Record I/Solution.cpp b/solution/0500-0599/0551.Student Attendance Record I/Solution.cpp index 153a92e4175d3..d5359de1228d7 100644 --- a/solution/0500-0599/0551.Student Attendance Record I/Solution.cpp +++ b/solution/0500-0599/0551.Student Attendance Record I/Solution.cpp @@ -1,7 +1,6 @@ class Solution { public: bool checkRecord(string s) { - return count(s.begin(), s.end(), 'A') < 2 && - s.find("LLL") == string::npos; + return count(s.begin(), s.end(), 'A') < 2 && s.find("LLL") == string::npos; } }; diff --git a/solution/0500-0599/0553.Optimal Division/Solution.cpp b/solution/0500-0599/0553.Optimal Division/Solution.cpp index 4c8104642e451..00b40367b56cf 100644 --- a/solution/0500-0599/0553.Optimal Division/Solution.cpp +++ b/solution/0500-0599/0553.Optimal Division/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: string optimalDivision(vector& nums) { - int n = nums.size(); + int n = nums.size(); if (n == 1) return to_string(nums[0]); if (n == 2) return to_string(nums[0]) + "/" + to_string(nums[1]); string ans = to_string(nums[0]) + "/("; diff --git a/solution/0500-0599/0556.Next Greater Element III/Solution.cpp b/solution/0500-0599/0556.Next Greater Element III/Solution.cpp index 385f9291a9291..d0476e3d05c05 100644 --- a/solution/0500-0599/0556.Next Greater Element III/Solution.cpp +++ b/solution/0500-0599/0556.Next Greater Element III/Solution.cpp @@ -4,9 +4,11 @@ class Solution { string s = to_string(n); n = s.size(); int i = n - 2, j = n - 1; - for (; i >= 0 && s[i] >= s[i + 1]; --i); + for (; i >= 0 && s[i] >= s[i + 1]; --i) + ; if (i < 0) return -1; - for (; s[i] >= s[j]; --j); + for (; s[i] >= s[j]; --j) + ; swap(s[i], s[j]); reverse(s.begin() + i + 1, s.end()); long ans = stol(s); diff --git a/solution/0500-0599/0557.Reverse Words in a String III/Solution.cpp b/solution/0500-0599/0557.Reverse Words in a String III/Solution.cpp index be89334752e02..33b5ffd55f74b 100644 --- a/solution/0500-0599/0557.Reverse Words in a String III/Solution.cpp +++ b/solution/0500-0599/0557.Reverse Words in a String III/Solution.cpp @@ -1,10 +1,10 @@ class Solution { public: string reverseWords(string s) { - for (int i = 0, n = s.size(); i < n; ++i) - { + for (int i = 0, n = s.size(); i < n; ++i) { int j = i; - while (++j < n && s[j] != ' '); + while (++j < n && s[j] != ' ') + ; reverse(s.begin() + i, s.begin() + j); i = j; } diff --git a/solution/0500-0599/0560.Subarray Sum Equals K/Solution.cpp b/solution/0500-0599/0560.Subarray Sum Equals K/Solution.cpp index f6c5393044de4..2515c7e555219 100644 --- a/solution/0500-0599/0560.Subarray Sum Equals K/Solution.cpp +++ b/solution/0500-0599/0560.Subarray Sum Equals K/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map counter; counter[0] = 1; int ans = 0, s = 0; - for (int& num : nums) - { + for (int& num : nums) { s += num; ans += counter[s - k]; ++counter[s]; diff --git a/solution/0500-0599/0564.Find the Closest Palindrome/Solution.cpp b/solution/0500-0599/0564.Find the Closest Palindrome/Solution.cpp index 8fa7475fd6bd3..c6b87951cc42f 100644 --- a/solution/0500-0599/0564.Find the Closest Palindrome/Solution.cpp +++ b/solution/0500-0599/0564.Find the Closest Palindrome/Solution.cpp @@ -15,8 +15,7 @@ class Solution { res.insert((long)pow(10, l - 1) - 1); res.insert((long)pow(10, l) + 1); long left = stol(n.substr(0, (l + 1) / 2)); - for (long i = left - 1; i <= left + 1; ++i) - { + for (long i = left - 1; i <= left + 1; ++i) { string prefix = to_string(i); string t = prefix + string(prefix.rbegin() + (l & 1), prefix.rend()); res.insert(stol(t)); diff --git a/solution/0500-0599/0565.Array Nesting/Solution.cpp b/solution/0500-0599/0565.Array Nesting/Solution.cpp index b7cf31d6761cc..a67ab3cb0554c 100644 --- a/solution/0500-0599/0565.Array Nesting/Solution.cpp +++ b/solution/0500-0599/0565.Array Nesting/Solution.cpp @@ -2,12 +2,10 @@ class Solution { public: int arrayNesting(vector& nums) { int ans = 0, n = nums.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int cnt = 0; int j = i; - while (nums[j] < n) - { + while (nums[j] < n) { int k = nums[j]; nums[j] = n; j = k; diff --git a/solution/0500-0599/0582.Kill Process/Solution.cpp b/solution/0500-0599/0582.Kill Process/Solution.cpp index 8b25efcdfa975..9e39e908c015c 100644 --- a/solution/0500-0599/0582.Kill Process/Solution.cpp +++ b/solution/0500-0599/0582.Kill Process/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map> g; vector ans; int n = pid.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int c = pid[i], p = ppid[i]; g[p].push_back(c); } diff --git a/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.cpp b/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.cpp index 5b7d111c6be92..8e676eb01d247 100644 --- a/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.cpp +++ b/solution/0500-0599/0583.Delete Operation for Two Strings/Solution.cpp @@ -5,12 +5,12 @@ class Solution { vector> dp(m + 1, vector(n + 1)); for (int i = 1; i <= m; ++i) dp[i][0] = i; for (int j = 1; j <= n; ++j) dp[0][j] = j; - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { - if (word1[i - 1] == word2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; - else dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]); + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (word1[i - 1] == word2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1]); } } return dp[m][n]; diff --git a/solution/0500-0599/0587.Erect the Fence/Solution.cpp b/solution/0500-0599/0587.Erect the Fence/Solution.cpp index b64f5b05042bc..fec27cc5ca467 100644 --- a/solution/0500-0599/0587.Erect the Fence/Solution.cpp +++ b/solution/0500-0599/0587.Erect the Fence/Solution.cpp @@ -7,15 +7,13 @@ class Solution { vector vis(n); vector stk(n + 10); int cnt = 1; - for (int i = 1; i < n; ++i) - { + for (int i = 1; i < n; ++i) { while (cnt > 1 && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) vis[stk[--cnt]] = false; vis[i] = true; stk[cnt++] = i; } int m = cnt; - for (int i = n - 1; i >= 0; --i) - { + for (int i = n - 1; i >= 0; --i) { if (vis[i]) continue; while (cnt > m && cross(trees[stk[cnt - 1]], trees[stk[cnt - 2]], trees[i]) < 0) --cnt; stk[cnt++] = i; diff --git a/solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution.cpp b/solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution.cpp index d7812d901a14f..732891393b3f8 100644 --- a/solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution.cpp +++ b/solution/0500-0599/0589.N-ary Tree Preorder Traversal/Solution.cpp @@ -25,8 +25,7 @@ class Solution { vector ans; stack stk; stk.push(root); - while (!stk.empty()) - { + while (!stk.empty()) { Node* node = stk.top(); ans.push_back(node->val); stk.pop(); diff --git a/solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.cpp b/solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.cpp index 746aee578169d..188c2455d0881 100644 --- a/solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.cpp +++ b/solution/0500-0599/0590.N-ary Tree Postorder Traversal/Solution.cpp @@ -23,9 +23,8 @@ class Solution { vector postorder(Node* root) { vector ans; if (!root) return ans; - stack stk{{root}}; - while (!stk.empty()) - { + stack stk {{root}}; + while (!stk.empty()) { root = stk.top(); ans.push_back(root->val); stk.pop(); diff --git a/solution/0500-0599/0591.Tag Validator/Solution.cpp b/solution/0500-0599/0591.Tag Validator/Solution.cpp index fce49dff57bbe..c467e8c61102a 100644 --- a/solution/0500-0599/0591.Tag Validator/Solution.cpp +++ b/solution/0500-0599/0591.Tag Validator/Solution.cpp @@ -2,26 +2,20 @@ class Solution { public: bool isValid(string code) { stack stk; - for (int i = 0; i < code.size(); ++i) - { + for (int i = 0; i < code.size(); ++i) { if (i && stk.empty()) return false; - if (code.substr(i, 9) == "", i + 9); if (i < 0) return false; i += 2; - } - else if (code.substr(i, 2) == "', j); if (i < 0) return false; string t = code.substr(j, i - j); if (!check(t) || stk.empty() || stk.top() != t) return false; stk.pop(); - } - else if (code.substr(i, 1) == "<") - { + } else if (code.substr(i, 1) == "<") { int j = i + 1; i = code.find('>', j); if (i < 0) return false; diff --git a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.cpp b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.cpp index db0b6b664eec9..d33f608f3b60b 100644 --- a/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.cpp +++ b/solution/0500-0599/0599.Minimum Index Sum of Two Lists/Solution.cpp @@ -5,19 +5,14 @@ class Solution { for (int i = 0; i < list2.size(); ++i) mp[list2[i]] = i; int mi = 2000; vector ans; - for (int i = 0; i < list1.size(); ++i) - { - if (mp.count(list1[i])) - { + for (int i = 0; i < list1.size(); ++i) { + if (mp.count(list1[i])) { int t = i + mp[list1[i]]; - if (t < mi) - { + if (t < mi) { ans.clear(); ans.push_back(list1[i]); mi = t; - } - else if (t == mi) - { + } else if (t == mi) { ans.push_back(list1[i]); } } diff --git a/solution/0600-0699/0605.Can Place Flowers/Solution.cpp b/solution/0600-0699/0605.Can Place Flowers/Solution.cpp index f0bd4ff7a434a..9b93861cfe7c8 100644 --- a/solution/0600-0699/0605.Can Place Flowers/Solution.cpp +++ b/solution/0600-0699/0605.Can Place Flowers/Solution.cpp @@ -2,12 +2,10 @@ public: bool canPlaceFlowers(vector& flowerbed, int n) { int m = flowerbed.size(); - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { int l = i == 0 ? 0 : flowerbed[i - 1]; int r = i == m - 1 ? 0 : flowerbed[i + 1]; - if (l + flowerbed[i] + r == 0) - { + if (l + flowerbed[i] + r == 0) { flowerbed[i] = 1; --n; } diff --git a/solution/0600-0699/0611.Valid Triangle Number/Solution.cpp b/solution/0600-0699/0611.Valid Triangle Number/Solution.cpp index df7aa2bb0810f..f3b34710dcdf3 100644 --- a/solution/0600-0699/0611.Valid Triangle Number/Solution.cpp +++ b/solution/0600-0699/0611.Valid Triangle Number/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int triangleNumber(vector& nums) { sort(nums.begin(), nums.end()); int ans = 0, n = nums.size(); - for (int i = 0; i < n - 2; ++i) - { - for (int j = i + 1; j < n - 1; ++j) - { + for (int i = 0; i < n - 2; ++i) { + for (int j = i + 1; j < n - 1; ++j) { int k = lower_bound(nums.begin() + j + 1, nums.end(), nums[i] + nums[j]) - nums.begin() - 1; ans += k - j; } diff --git a/solution/0600-0699/0616.Add Bold Tag in String/Solution.cpp b/solution/0600-0699/0616.Add Bold Tag in String/Solution.cpp index fe299a0295ce3..31d0de3dddffa 100644 --- a/solution/0600-0699/0616.Add Bold Tag in String/Solution.cpp +++ b/solution/0600-0699/0616.Add Bold Tag in String/Solution.cpp @@ -10,8 +10,7 @@ class Trie { void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; } @@ -26,11 +25,9 @@ class Solution { for (string w : words) trie->insert(w); int n = s.size(); vector> pairs; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { Trie* node = trie; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { int idx = s[j]; if (!node->children[idx]) break; node = node->children[idx]; @@ -40,23 +37,19 @@ class Solution { if (pairs.empty()) return s; vector> t; int st = pairs[0].first, ed = pairs[0].second; - for (int i = 1; i < pairs.size(); ++i) - { + for (int i = 1; i < pairs.size(); ++i) { int a = pairs[i].first, b = pairs[i].second; - if (ed + 1 < a) - { + if (ed + 1 < a) { t.push_back({st, ed}); st = a, ed = b; - } - else ed = max(ed, b); + } else + ed = max(ed, b); } t.push_back({st, ed}); string ans = ""; int i = 0, j = 0; - while (i < n) - { - if (j == t.size()) - { + while (i < n) { + if (j == t.size()) { ans += s.substr(i); break; } diff --git a/solution/0600-0699/0617.Merge Two Binary Trees/Solution.cpp b/solution/0600-0699/0617.Merge Two Binary Trees/Solution.cpp index b10e0467e5f1d..7720f110d828c 100644 --- a/solution/0600-0699/0617.Merge Two Binary Trees/Solution.cpp +++ b/solution/0600-0699/0617.Merge Two Binary Trees/Solution.cpp @@ -18,7 +18,7 @@ class Solution { if (root2 == nullptr) { return root1; } - TreeNode *node = new TreeNode(root1->val + root2->val); + TreeNode* node = new TreeNode(root1->val + root2->val); node->left = mergeTrees(root1->left, root2->left); node->right = mergeTrees(root1->right, root2->right); return node; diff --git a/solution/0600-0699/0622.Design Circular Queue/Solution.cpp b/solution/0600-0699/0622.Design Circular Queue/Solution.cpp index 13ef3dd9f3ca7..06dd0ced86f16 100644 --- a/solution/0600-0699/0622.Design Circular Queue/Solution.cpp +++ b/solution/0600-0699/0622.Design Circular Queue/Solution.cpp @@ -11,7 +11,7 @@ class MyCircularQueue { q = vector(k); front = size = 0; } - + bool enQueue(int value) { if (isFull()) return false; int idx = (front + size) % capacity; @@ -19,29 +19,29 @@ class MyCircularQueue { ++size; return true; } - + bool deQueue() { if (isEmpty()) return false; front = (front + 1) % capacity; --size; return true; } - + int Front() { if (isEmpty()) return -1; return q[front]; } - + int Rear() { if (isEmpty()) return -1; int idx = (front + size - 1) % capacity; return q[idx]; } - + bool isEmpty() { return size == 0; } - + bool isFull() { return size == capacity; } diff --git a/solution/0600-0699/0623.Add One Row to Tree/Solution.cpp b/solution/0600-0699/0623.Add One Row to Tree/Solution.cpp index 0e4bd7bf52a77..458848a26672b 100644 --- a/solution/0600-0699/0623.Add One Row to Tree/Solution.cpp +++ b/solution/0600-0699/0623.Add One Row to Tree/Solution.cpp @@ -24,8 +24,7 @@ class Solution { void dfs(TreeNode* root, int d) { if (!root) return; - if (d == depth - 1) - { + if (d == depth - 1) { auto l = new TreeNode(val, root->left, nullptr); auto r = new TreeNode(val, nullptr, root->right); root->left = l; diff --git a/solution/0600-0699/0630.Course Schedule III/Solution.cpp b/solution/0600-0699/0630.Course Schedule III/Solution.cpp index e41b4baee8ba1..54dfa87de5a1d 100644 --- a/solution/0600-0699/0630.Course Schedule III/Solution.cpp +++ b/solution/0600-0699/0630.Course Schedule III/Solution.cpp @@ -6,13 +6,11 @@ class Solution { }); int s = 0; priority_queue pq; - for (auto& course : courses) - { + for (auto& course : courses) { int d = course[0], e = course[1]; pq.push(d); s += d; - if (s > e) - { + if (s > e) { s -= pq.top(); pq.pop(); } diff --git a/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp b/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp index a2e2afd410059..64a57119e9c0b 100644 --- a/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp +++ b/solution/0600-0699/0633.Sum of Square Numbers/Solution.cpp @@ -1,13 +1,14 @@ class Solution { public: bool judgeSquareSum(int c) { - long a = 0, b = (long) sqrt(c); - while (a <= b) - { + long a = 0, b = (long)sqrt(c); + while (a <= b) { long s = a * a + b * b; if (s == c) return true; - if (s < c) ++a; - else --b; + if (s < c) + ++a; + else + --b; } return false; } diff --git a/solution/0600-0699/0636.Exclusive Time of Functions/Solution.cpp b/solution/0600-0699/0636.Exclusive Time of Functions/Solution.cpp index ddf9fe98f38ad..749e4fca5a255 100644 --- a/solution/0600-0699/0636.Exclusive Time of Functions/Solution.cpp +++ b/solution/0600-0699/0636.Exclusive Time of Functions/Solution.cpp @@ -4,19 +4,15 @@ class Solution { vector ans(n); stack stk; int curr = -1; - for (auto& log : logs) - { + for (auto& log : logs) { char type[10]; int fid, ts; sscanf(log.c_str(), "%d:%[^:]:%d", &fid, type, &ts); - if (type[0] == 's') - { + if (type[0] == 's') { if (!stk.empty()) ans[stk.top()] += ts - curr; curr = ts; stk.push(fid); - } - else - { + } else { fid = stk.top(); stk.pop(); ans[fid] += ts - curr + 1; diff --git a/solution/0600-0699/0638.Shopping Offers/Solution.cpp b/solution/0600-0699/0638.Shopping Offers/Solution.cpp index 9be88f2fec10b..cf1eeb13313ad 100644 --- a/solution/0600-0699/0638.Shopping Offers/Solution.cpp +++ b/solution/0600-0699/0638.Shopping Offers/Solution.cpp @@ -3,13 +3,10 @@ class Solution { int shoppingOffers(vector& price, vector>& special, vector& needs) { int ans = total(price, needs); vector t; - for (auto& offer : special) - { + for (auto& offer : special) { t.clear(); - for (int j = 0; j < needs.size(); ++j) - { - if (offer[j] > needs[j]) - { + for (int j = 0; j < needs.size(); ++j) { + if (offer[j] > needs[j]) { t.clear(); break; } diff --git a/solution/0600-0699/0646.Maximum Length of Pair Chain/Solution.cpp b/solution/0600-0699/0646.Maximum Length of Pair Chain/Solution.cpp index cb80c980d2fcb..c409253301c9a 100644 --- a/solution/0600-0699/0646.Maximum Length of Pair Chain/Solution.cpp +++ b/solution/0600-0699/0646.Maximum Length of Pair Chain/Solution.cpp @@ -1,14 +1,12 @@ class Solution { public: int findLongestChain(vector>& pairs) { - sort(pairs.begin(), pairs.end(), [](vector &a, vectorb) { + sort(pairs.begin(), pairs.end(), [](vector& a, vector b) { return a[1] < b[1]; }); int ans = 0, cur = INT_MIN; - for (auto& p : pairs) - { - if (cur < p[0]) - { + for (auto& p : pairs) { + if (cur < p[0]) { cur = p[1]; ++ans; } diff --git a/solution/0600-0699/0648.Replace Words/Solution.cpp b/solution/0600-0699/0648.Replace Words/Solution.cpp index 189502194160b..b1447f015af42 100644 --- a/solution/0600-0699/0648.Replace Words/Solution.cpp +++ b/solution/0600-0699/0648.Replace Words/Solution.cpp @@ -2,12 +2,13 @@ class Trie { public: vector children; string v; - Trie() : children(26), v("") {} + Trie() + : children(26) + , v("") { } void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; @@ -17,8 +18,7 @@ class Trie { string search(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) break; node = node->children[c]; diff --git a/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.cpp b/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.cpp index 1eee463e49006..f5f3ea19eed5b 100644 --- a/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.cpp +++ b/solution/0600-0699/0653.Two Sum IV - Input is a BST/Solution.cpp @@ -14,7 +14,7 @@ class Solution { unordered_set nodes; bool findTarget(TreeNode* root, int k) { - return find(root, k); + return find(root, k); } bool find(TreeNode* root, int k) { diff --git a/solution/0600-0699/0658.Find K Closest Elements/Solution.cpp b/solution/0600-0699/0658.Find K Closest Elements/Solution.cpp index 48b07c7f1f5d7..e7d9ca4ab0ab8 100644 --- a/solution/0600-0699/0658.Find K Closest Elements/Solution.cpp +++ b/solution/0600-0699/0658.Find K Closest Elements/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: vector findClosestElements(vector& arr, int k, int x) { int left = 0, right = arr.size() - k; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (x - arr[mid] <= arr[mid + k] - x) right = mid; - else left = mid + 1; + if (x - arr[mid] <= arr[mid + k] - x) + right = mid; + else + left = mid + 1; } return vector(arr.begin() + left, arr.begin() + left + k); } diff --git a/solution/0600-0699/0661.Image Smoother/Solution.cpp b/solution/0600-0699/0661.Image Smoother/Solution.cpp index a06e4978b806b..a0a552ba38375 100644 --- a/solution/0600-0699/0661.Image Smoother/Solution.cpp +++ b/solution/0600-0699/0661.Image Smoother/Solution.cpp @@ -3,15 +3,11 @@ class Solution { vector> imageSmoother(vector>& img) { int m = img.size(), n = img[0].size(); vector> ans(m, vector(n)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int s = 0, cnt = 0; - for (int x = i - 1; x <= i + 1; ++x) - { - for (int y = j - 1; y <= j + 1; ++y) - { + for (int x = i - 1; x <= i + 1; ++x) { + for (int y = j - 1; y <= j + 1; ++y) { if (x < 0 || x >= m || y < 0 || y >= n) continue; ++cnt; s += img[x][y]; diff --git a/solution/0600-0699/0662.Maximum Width of Binary Tree/Solution.cpp b/solution/0600-0699/0662.Maximum Width of Binary Tree/Solution.cpp index 003cd13836d02..6203eb0072e30 100644 --- a/solution/0600-0699/0662.Maximum Width of Binary Tree/Solution.cpp +++ b/solution/0600-0699/0662.Maximum Width of Binary Tree/Solution.cpp @@ -15,12 +15,10 @@ class Solution { queue> q; q.emplace(root, 1); int ans = 0; - while (!q.empty()) - { + while (!q.empty()) { ans = max(ans, q.back().second - q.front().second + 1); int start = q.front().second; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { auto node = q.front(); q.pop(); if (node.first->left != nullptr) q.emplace(node.first->left, node.second * 2 - start * 2); diff --git a/solution/0600-0699/0666.Path Sum IV/Solution.cpp b/solution/0600-0699/0666.Path Sum IV/Solution.cpp index dd5bc9411eba7..1bb53d802b4e1 100644 --- a/solution/0600-0699/0666.Path Sum IV/Solution.cpp +++ b/solution/0600-0699/0666.Path Sum IV/Solution.cpp @@ -17,8 +17,7 @@ class Solution { int d = node / 10, p = node % 10; int l = (d + 1) * 10 + (p * 2) - 1; int r = l + 1; - if (!mp.count(l) && !mp.count(r)) - { + if (!mp.count(l) && !mp.count(r)) { ans += t; return; } diff --git a/solution/0600-0699/0668.Kth Smallest Number in Multiplication Table/Solution.cpp b/solution/0600-0699/0668.Kth Smallest Number in Multiplication Table/Solution.cpp index 3d4abd0042578..764f28a8f469d 100644 --- a/solution/0600-0699/0668.Kth Smallest Number in Multiplication Table/Solution.cpp +++ b/solution/0600-0699/0668.Kth Smallest Number in Multiplication Table/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: int findKthNumber(int m, int n, int k) { int left = 1, right = m * n; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int cnt = 0; for (int i = 1; i <= m; ++i) cnt += min(mid / i, n); - if (cnt >= k) right = mid; - else left = mid + 1; + if (cnt >= k) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0600-0699/0670.Maximum Swap/Solution.cpp b/solution/0600-0699/0670.Maximum Swap/Solution.cpp index 5e1cb78034646..545d1c83055e2 100644 --- a/solution/0600-0699/0670.Maximum Swap/Solution.cpp +++ b/solution/0600-0699/0670.Maximum Swap/Solution.cpp @@ -3,15 +3,12 @@ class Solution { int maximumSwap(int num) { string s = to_string(num); int n = s.size(); - for (int i = 0; i < n - 1; ++i) - { + for (int i = 0; i < n - 1; ++i) { int mx = i + 1; - for (int j = i + 1; j < n; ++j) - { + for (int j = i + 1; j < n; ++j) { if (s[j] >= s[mx]) mx = j; } - if (s[i] < s[mx]) - { + if (s[i] < s[mx]) { swap(s[i], s[mx]); break; } diff --git a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.cpp b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.cpp index 293268c3ce9f9..710232f6a60eb 100644 --- a/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.cpp +++ b/solution/0600-0699/0674.Longest Continuous Increasing Subsequence/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int findLengthOfLCIS(vector& nums) { int res = 1; - for (int i = 1, f = 1; i < nums.size(); ++i) - { + for (int i = 1, f = 1; i < nums.size(); ++i) { f = 1 + (nums[i - 1] < nums[i] ? f : 0); res = max(res, f); } diff --git a/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.cpp b/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.cpp index c0e54397a4f25..765871c7013a1 100644 --- a/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.cpp +++ b/solution/0600-0699/0675.Cut Off Trees for Golf Event/Solution.cpp @@ -16,8 +16,7 @@ class Solution { sort(trees.begin(), trees.end()); int ans = 0; int start = 0; - for (auto& tree : trees) - { + for (auto& tree : trees) { int end = tree.second; int t = bfs(start, end, forest); if (t == -1) return -1; @@ -33,16 +32,13 @@ class Solution { fill(dist.begin(), dist.end(), INT_MAX); dist[start] = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { int state = q.top().second; q.pop(); if (state == end) return dist[state]; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = state / n + dirs[k], y = state % n + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && forest[x][y] && dist[x * n + y] > dist[state] + 1) - { + if (x >= 0 && x < m && y >= 0 && y < n && forest[x][y] && dist[x * n + y] > dist[state] + 1) { dist[x * n + y] = dist[state] + 1; q.push({dist[x * n + y] + f(x * n + y, end), x * n + y}); } diff --git a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.cpp b/solution/0600-0699/0676.Implement Magic Dictionary/Solution.cpp index d1c9f7e2ea5e6..617c913d4400a 100644 --- a/solution/0600-0699/0676.Implement Magic Dictionary/Solution.cpp +++ b/solution/0600-0699/0676.Implement Magic Dictionary/Solution.cpp @@ -2,20 +2,17 @@ class MagicDictionary { public: /** Initialize your data structure here. */ MagicDictionary() { - } - + void buildDict(vector dictionary) { - for (string word : dictionary) - { + for (string word : dictionary) { s.insert(word); for (string p : gen(word)) ++cnt[p]; } } - + bool search(string searchWord) { - for (string p : gen(searchWord)) - { + for (string p : gen(searchWord)) { if (cnt[p] > 1 || (cnt[p] == 1 && !s.count(searchWord))) return true; } return false; @@ -27,8 +24,7 @@ class MagicDictionary { vector gen(string word) { vector res; - for (int i = 0; i < word.size(); ++i) - { + for (int i = 0; i < word.size(); ++i) { char c = word[i]; word[i] = '*'; res.push_back(word); diff --git a/solution/0600-0699/0677.Map Sum Pairs/Solution.cpp b/solution/0600-0699/0677.Map Sum Pairs/Solution.cpp index 211a1f366580f..02a261264d990 100644 --- a/solution/0600-0699/0677.Map Sum Pairs/Solution.cpp +++ b/solution/0600-0699/0677.Map Sum Pairs/Solution.cpp @@ -5,19 +5,17 @@ class MapSum { /** Initialize your data structure here. */ MapSum() { - } - + void insert(string key, int val) { int old = t[key]; t[key] = val; - for (int i = 1; i < key.size() + 1; ++i) - { + for (int i = 1; i < key.size() + 1; ++i) { string k = key.substr(0, i); data[k] += (val - old); } } - + int sum(string prefix) { return data[prefix]; } diff --git a/solution/0600-0699/0678.Valid Parenthesis String/Solution.cpp b/solution/0600-0699/0678.Valid Parenthesis String/Solution.cpp index b35214503e01e..e107a2f6b6e2e 100644 --- a/solution/0600-0699/0678.Valid Parenthesis String/Solution.cpp +++ b/solution/0600-0699/0678.Valid Parenthesis String/Solution.cpp @@ -1,7 +1,7 @@ class Solution { - public: +public: bool checkValidString(string s) { - int n = s.size(); + int n = s.size(); int left = 0, asterisk = 0; for (int i = 0; i < n; ++i) { if (s[i] == '(') { @@ -18,7 +18,7 @@ class Solution { } } int right = 0; - asterisk = 0; + asterisk = 0; for (int i = n - 1; i >= 0; --i) { if (s[i] == ')') { ++right; diff --git a/solution/0600-0699/0682.Baseball Game/Solution.cpp b/solution/0600-0699/0682.Baseball Game/Solution.cpp index dbc9598274ac1..94b8acc343857 100644 --- a/solution/0600-0699/0682.Baseball Game/Solution.cpp +++ b/solution/0600-0699/0682.Baseball Game/Solution.cpp @@ -2,18 +2,18 @@ class Solution { public: int calPoints(vector& ops) { vector stk; - for (auto& op : ops) - { + for (auto& op : ops) { int n = stk.size(); - if (op == "+") - { + if (op == "+") { int a = stk[n - 1]; int b = stk[n - 2]; stk.push_back(a + b); - } - else if (op == "D") stk.push_back(stk[n - 1] * 2); - else if (op == "C") stk.pop_back(); - else stk.push_back(stoi(op)); + } else if (op == "D") + stk.push_back(stk[n - 1] * 2); + else if (op == "C") + stk.pop_back(); + else + stk.push_back(stoi(op)); } return accumulate(stk.begin(), stk.end(), 0); } diff --git a/solution/0600-0699/0683.K Empty Slots/Solution.cpp b/solution/0600-0699/0683.K Empty Slots/Solution.cpp index a03cef1c4d571..899b197fe7f08 100644 --- a/solution/0600-0699/0683.K Empty Slots/Solution.cpp +++ b/solution/0600-0699/0683.K Empty Slots/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -33,8 +33,7 @@ class Solution { int kEmptySlots(vector& bulbs, int k) { int n = bulbs.size(); BinaryIndexedTree* tree = new BinaryIndexedTree(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int x = bulbs[i]; tree->update(x, 1); bool case1 = x - k - 1 > 0 && tree->query(x - k - 1) - tree->query(x - k - 2) == 1 && tree->query(x - 1) - tree->query(x - k - 1) == 0; diff --git a/solution/0600-0699/0684.Redundant Connection/Solution.cpp b/solution/0600-0699/0684.Redundant Connection/Solution.cpp index 569576fb431e5..d16769f3fec15 100644 --- a/solution/0600-0699/0684.Redundant Connection/Solution.cpp +++ b/solution/0600-0699/0684.Redundant Connection/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector findRedundantConnection(vector>& edges) { p.resize(1010); for (int i = 0; i < p.size(); ++i) p[i] = i; - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; if (find(a) == find(b)) return e; p[find(a)] = find(b); diff --git a/solution/0600-0699/0685.Redundant Connection II/Solution.cpp b/solution/0600-0699/0685.Redundant Connection II/Solution.cpp index e0669a3bdf913..a8208d0703c61 100644 --- a/solution/0600-0699/0685.Redundant Connection II/Solution.cpp +++ b/solution/0600-0699/0685.Redundant Connection II/Solution.cpp @@ -3,7 +3,9 @@ class UnionFind { vector p; int n; - UnionFind(int _n): n(_n), p(_n) { + UnionFind(int _n) + : n(_n) + , p(_n) { iota(p.begin(), p.end(), 0); } @@ -29,12 +31,11 @@ class Solution { for (int i = 0; i <= n; ++i) p[i] = i; UnionFind uf(n + 1); int conflict = -1, cycle = -1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int u = edges[i][0], v = edges[i][1]; - if (p[v] != v) conflict = i; - else - { + if (p[v] != v) + conflict = i; + else { p[v] = u; if (!uf.unite(u, v)) cycle = i; } diff --git a/solution/0600-0699/0686.Repeated String Match/Solution.cpp b/solution/0600-0699/0686.Repeated String Match/Solution.cpp index ac7bb3e45fb57..0b1ab31c09c17 100644 --- a/solution/0600-0699/0686.Repeated String Match/Solution.cpp +++ b/solution/0600-0699/0686.Repeated String Match/Solution.cpp @@ -5,8 +5,7 @@ class Solution { int ans = (n + m - 1) / m; string t = ""; for (int i = 0; i < ans; ++i) t += a; - for (int i = 0; i < 3; ++i) - { + for (int i = 0; i < 3; ++i) { if (t.find(b) != -1) return ans; ++ans; t += a; diff --git a/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.cpp b/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.cpp index 492145c8387e2..43a7085b32703 100644 --- a/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.cpp +++ b/solution/0600-0699/0688.Knight Probability in Chessboard/Solution.cpp @@ -3,17 +3,13 @@ class Solution { double knightProbability(int n, int k, int row, int column) { vector>> dp(k + 1, vector>(n, vector(n))); vector dirs = {-2, -1, 2, 1, -2, 1, 2, -1, -2}; - for (int l = 0; l <= k; ++l) - { - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (l == 0) dp[l][i][j] = 1; - else - { - for (int d = 0; d < 8; ++d) - { + for (int l = 0; l <= k; ++l) { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (l == 0) + dp[l][i][j] = 1; + else { + for (int d = 0; d < 8; ++d) { int x = i + dirs[d], y = j + dirs[d + 1]; if (x >= 0 && x < n && y >= 0 && y < n) dp[l][i][j] += dp[l - 1][x][y] / 8; diff --git a/solution/0600-0699/0689.Maximum Sum of 3 Non-Overlapping Subarrays/Solution.cpp b/solution/0600-0699/0689.Maximum Sum of 3 Non-Overlapping Subarrays/Solution.cpp index 8087b1511e5ff..a0596d0b2084d 100644 --- a/solution/0600-0699/0689.Maximum Sum of 3 Non-Overlapping Subarrays/Solution.cpp +++ b/solution/0600-0699/0689.Maximum Sum of 3 Non-Overlapping Subarrays/Solution.cpp @@ -5,26 +5,21 @@ class Solution { int s = 0, s1 = 0, s2 = 0, s3 = 0; int mx1 = 0, mx12 = 0; int idx1 = 0, idx121 = 0, idx122 = 0; - for (int i = k * 2; i < nums.size(); ++i) - { + for (int i = k * 2; i < nums.size(); ++i) { s1 += nums[i - k * 2]; s2 += nums[i - k]; s3 += nums[i]; - if (i >= k * 3 - 1) - { - if (s1 > mx1) - { + if (i >= k * 3 - 1) { + if (s1 > mx1) { mx1 = s1; idx1 = i - k * 3 + 1; } - if (mx1 + s2 > mx12) - { + if (mx1 + s2 > mx12) { mx12 = mx1 + s2; idx121 = idx1; idx122 = i - k * 2 + 1; } - if (mx12 + s3 > s) - { + if (mx12 + s3 > s) { s = mx12 + s3; ans = {idx121, idx122, i - k + 1}; } diff --git a/solution/0600-0699/0691.Stickers to Spell Word/Solution.cpp b/solution/0600-0699/0691.Stickers to Spell Word/Solution.cpp index dd6c67a185463..bc591aba19e28 100644 --- a/solution/0600-0699/0691.Stickers to Spell Word/Solution.cpp +++ b/solution/0600-0699/0691.Stickers to Spell Word/Solution.cpp @@ -1,34 +1,28 @@ class Solution { public: int minStickers(vector& stickers, string target) { - queue q{{0}}; + queue q {{0}}; int ans = 0; int n = target.size(); vector vis(1 << n); vis[0] = true; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { int state = q.front(); if (state == (1 << n) - 1) return ans; q.pop(); - for (auto& s : stickers) - { + for (auto& s : stickers) { int nxt = state; vector cnt(26); for (char& c : s) ++cnt[c - 'a']; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int idx = target[i] - 'a'; - if (!(nxt & (1 << i)) && cnt[idx]) - { + if (!(nxt & (1 << i)) && cnt[idx]) { nxt |= 1 << i; --cnt[idx]; } } - if (!vis[nxt]) - { + if (!vis[nxt]) { vis[nxt] = true; q.push(nxt); } diff --git a/solution/0600-0699/0693.Binary Number with Alternating Bits/Solution.cpp b/solution/0600-0699/0693.Binary Number with Alternating Bits/Solution.cpp index 6e44e0223a205..c7c3dd2edce77 100644 --- a/solution/0600-0699/0693.Binary Number with Alternating Bits/Solution.cpp +++ b/solution/0600-0699/0693.Binary Number with Alternating Bits/Solution.cpp @@ -2,6 +2,6 @@ class Solution { public: bool hasAlternatingBits(int n) { n ^= (n >> 1); - return (n & ((long) n + 1)) == 0; + return (n & ((long)n + 1)) == 0; } }; \ No newline at end of file diff --git a/solution/0600-0699/0694.Number of Distinct Islands/Solution.cpp b/solution/0600-0699/0694.Number of Distinct Islands/Solution.cpp index 0c0db7b5aafde..16a8d56f0ae53 100644 --- a/solution/0600-0699/0694.Number of Distinct Islands/Solution.cpp +++ b/solution/0600-0699/0694.Number of Distinct Islands/Solution.cpp @@ -3,12 +3,9 @@ class Solution { int numDistinctIslands(vector>& grid) { unordered_set paths; string path; - for (int i = 0; i < grid.size(); ++i) - { - for (int j = 0; j < grid[0].size(); ++j) - { - if (grid[i][j] == 1) - { + for (int i = 0; i < grid.size(); ++i) { + for (int j = 0; j < grid[0].size(); ++j) { + if (grid[i][j] == 1) { path = ""; dfs(i, j, 0, grid, path); paths.insert(path); @@ -22,8 +19,7 @@ class Solution { grid[i][j] = 0; path += to_string(direction); vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 1; k < 5; ++k) - { + for (int k = 1; k < 5; ++k) { int x = i + dirs[k - 1], y = j + dirs[k]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 1) dfs(x, y, k, grid, path); diff --git a/solution/0600-0699/0695.Max Area of Island/Solution.cpp b/solution/0600-0699/0695.Max Area of Island/Solution.cpp index 95eb739ff22e9..548aa09ce7849 100644 --- a/solution/0600-0699/0695.Max Area of Island/Solution.cpp +++ b/solution/0600-0699/0695.Max Area of Island/Solution.cpp @@ -18,8 +18,7 @@ class Solution { grid[i][j] = 0; int ans = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k]; int y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) diff --git a/solution/0600-0699/0696.Count Binary Substrings/Solution.cpp b/solution/0600-0699/0696.Count Binary Substrings/Solution.cpp index e23ddccb565a0..a0037c36613b8 100644 --- a/solution/0600-0699/0696.Count Binary Substrings/Solution.cpp +++ b/solution/0600-0699/0696.Count Binary Substrings/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int countBinarySubstrings(string s) { int i = 0, n = s.size(); vector t; - while (i < n) - { + while (i < n) { int cnt = 1; - while (i + 1 < n && s[i + 1] == s[i]) - { + while (i + 1 < n && s[i + 1] == s[i]) { ++cnt; ++i; } diff --git a/solution/0600-0699/0698.Partition to K Equal Sum Subsets/Solution.cpp b/solution/0600-0699/0698.Partition to K Equal Sum Subsets/Solution.cpp index d88e63dd3629d..5398a0b66cf1d 100644 --- a/solution/0600-0699/0698.Partition to K Equal Sum Subsets/Solution.cpp +++ b/solution/0600-0699/0698.Partition to K Equal Sum Subsets/Solution.cpp @@ -4,7 +4,7 @@ class Solution { int sum = accumulate(nums.begin(), nums.end(), 0); if (sum % k != 0) return false; - int target = sum / k; + int target = sum / k; int n = nums.size(); vector cur(k, 0); diff --git a/solution/0600-0699/0699.Falling Squares/Solution.cpp b/solution/0600-0699/0699.Falling Squares/Solution.cpp index cf75968adc490..029b4939d6c71 100644 --- a/solution/0600-0699/0699.Falling Squares/Solution.cpp +++ b/solution/0600-0699/0699.Falling Squares/Solution.cpp @@ -30,10 +30,9 @@ class SegmentTree { modify(l, r, v, root); } - void modify(int l, int r,int v, Node* node) { + void modify(int l, int r, int v, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v = v; node->add = v; return; @@ -45,17 +44,17 @@ class SegmentTree { } int query(int l, int r) { - return query(l, r, root); + return query(l, r, root); } int query(int l, int r, Node* node) { if (l > r) return 0; - if (node->l >= l && node-> r <= r) return node->v; + if (node->l >= l && node->r <= r) return node->v; pushdown(node); int v = 0; if (l <= node->mid) v = max(v, query(l, r, node->left)); if (r > node->mid) v = max(v, query(l, r, node->right)); - return v; + return v; } void pushup(Node* node) { @@ -65,8 +64,7 @@ class SegmentTree { void pushdown(Node* node) { if (!node->left) node->left = new Node(node->l, node->mid); if (!node->right) node->right = new Node(node->mid + 1, node->r); - if (node->add) - { + if (node->add) { Node* left = node->left; Node* right = node->right; left->v = node->add; @@ -84,8 +82,7 @@ class Solution { vector ans; SegmentTree* tree = new SegmentTree(); int mx = 0; - for (auto& p : positions) - { + for (auto& p : positions) { int l = p[0], w = p[1], r = l + w - 1; int h = tree->query(l, r) + w; mx = max(mx, h); diff --git a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp index ef66a867d0c68..ea17aff065736 100644 --- a/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp +++ b/solution/0700-0799/0701.Insert into a Binary Search Tree/Solution.cpp @@ -13,8 +13,10 @@ class Solution { public: TreeNode* insertIntoBST(TreeNode* root, int val) { if (!root) return new TreeNode(val); - if (root->val < val) root->right = insertIntoBST(root->right, val); - else root->left = insertIntoBST(root->left, val); + if (root->val < val) + root->right = insertIntoBST(root->right, val); + else + root->left = insertIntoBST(root->left, val); return root; } }; \ No newline at end of file diff --git a/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.cpp b/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.cpp index 72f168d144f19..b1e5e3e44425a 100644 --- a/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.cpp +++ b/solution/0700-0799/0703.Kth Largest Element in a Stream/Solution.cpp @@ -7,7 +7,7 @@ class KthLargest { size = k; for (int num : nums) add(num); } - + int add(int val) { q.push(val); if (q.size() > size) q.pop(); diff --git a/solution/0700-0799/0704.Binary Search/Solution.cpp b/solution/0700-0799/0704.Binary Search/Solution.cpp index 1e4eff89aec0e..f319e60837348 100644 --- a/solution/0700-0799/0704.Binary Search/Solution.cpp +++ b/solution/0700-0799/0704.Binary Search/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: int search(vector& nums, int target) { int left = 0, right = nums.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (nums[mid] >= target) right = mid; - else left = mid + 1; + if (nums[mid] >= target) + right = mid; + else + left = mid + 1; } return nums[left] == target ? left : -1; } diff --git a/solution/0700-0799/0706.Design HashMap/Solution.cpp b/solution/0700-0799/0706.Design HashMap/Solution.cpp index 874eaf6f099e4..2623139672b82 100644 --- a/solution/0700-0799/0706.Design HashMap/Solution.cpp +++ b/solution/0700-0799/0706.Design HashMap/Solution.cpp @@ -5,15 +5,15 @@ class MyHashMap { MyHashMap() { memset(hash, -1, sizeof hash); } - + void put(int key, int value) { hash[key] = value; } - + int get(int key) { return hash[key]; } - + void remove(int key) { hash[key] = -1; } diff --git a/solution/0700-0799/0707.Design Linked List/Solution.cpp b/solution/0700-0799/0707.Design Linked List/Solution.cpp index 726648e473dfd..b7273af75e922 100644 --- a/solution/0700-0799/0707.Design Linked List/Solution.cpp +++ b/solution/0700-0799/0707.Design Linked List/Solution.cpp @@ -7,52 +7,52 @@ class MyLinkedList { int size = 0; MyLinkedList() { - } - + int get(int index) { if (index < 0 || index >= size) return -1; int i = head; - for (; index > 0; i = ne[i], index--); + for (; index > 0; i = ne[i], index--) + ; return e[i]; } - + void addAtHead(int val) { e[idx] = val; ne[idx] = head; head = idx++; size++; } - + void addAtTail(int val) { addAtIndex(size, val); } - + void addAtIndex(int index, int val) { if (index > size) return; - if (index <= 0) - { + if (index <= 0) { addAtHead(val); return; } int i = head; - for (; index > 1; i = ne[i], index--); + for (; index > 1; i = ne[i], index--) + ; e[idx] = val; ne[idx] = ne[i]; ne[i] = idx++; size++; } - + void deleteAtIndex(int index) { if (index < 0 || index >= size) return; size--; - if (index == 0) - { + if (index == 0) { head = ne[head]; return; } int i = head; - for (; index > 1; i = ne[i], index--); + for (; index > 1; i = ne[i], index--) + ; ne[i] = ne[ne[i]]; } }; diff --git a/solution/0700-0799/0708.Insert into a Sorted Circular Linked List/Solution.cpp b/solution/0700-0799/0708.Insert into a Sorted Circular Linked List/Solution.cpp index 6a51ad86e2075..84580318378eb 100644 --- a/solution/0700-0799/0708.Insert into a Sorted Circular Linked List/Solution.cpp +++ b/solution/0700-0799/0708.Insert into a Sorted Circular Linked List/Solution.cpp @@ -23,14 +23,12 @@ class Solution { public: Node* insert(Node* head, int insertVal) { Node* node = new Node(insertVal); - if (!head) - { + if (!head) { node->next = node; return node; } Node *prev = head, *curr = head->next; - while (curr != head) - { + while (curr != head) { if ((prev->val <= insertVal && insertVal <= curr->val) || (prev->val > curr->val && (insertVal >= prev->val || insertVal <= curr->val))) break; prev = curr; curr = curr->next; diff --git a/solution/0700-0799/0710.Random Pick with Blacklist/Solution.cpp b/solution/0700-0799/0710.Random Pick with Blacklist/Solution.cpp index 7183847c54fce..d7d2ca906234c 100644 --- a/solution/0700-0799/0710.Random Pick with Blacklist/Solution.cpp +++ b/solution/0700-0799/0710.Random Pick with Blacklist/Solution.cpp @@ -7,16 +7,14 @@ class Solution { k = n - blacklist.size(); int i = k; unordered_set black(blacklist.begin(), blacklist.end()); - for (int& b : blacklist) - { - if (b < k) - { + for (int& b : blacklist) { + if (b < k) { while (black.count(i)) ++i; d[b] = i++; } } } - + int pick() { int x = rand() % k; return d.count(x) ? d[x] : x; diff --git a/solution/0700-0799/0711.Number of Distinct Islands II/Solution.cpp b/solution/0700-0799/0711.Number of Distinct Islands II/Solution.cpp index 40052c10056f0..38b0abe1984e1 100644 --- a/solution/0700-0799/0711.Number of Distinct Islands II/Solution.cpp +++ b/solution/0700-0799/0711.Number of Distinct Islands II/Solution.cpp @@ -1,15 +1,12 @@ -typedef pair PII; +typedef pair PII; class Solution { public: int numDistinctIslands2(vector>& grid) { set> s; - for (int i = 0; i < grid.size(); ++i) - { - for (int j = 0; j < grid[0].size(); ++j) - { - if (grid[i][j]) - { + for (int i = 0; i < grid.size(); ++i) { + for (int j = 0; j < grid[0].size(); ++j) { + if (grid[i][j]) { vector shape; dfs(i, j, grid, shape); s.insert(normalize(shape)); @@ -21,8 +18,7 @@ class Solution { vector normalize(vector& shape) { vector> shapes(8); - for (auto& e : shape) - { + for (auto& e : shape) { int i = e.first, j = e.second; shapes[0].push_back({i, j}); shapes[1].push_back({i, -j}); @@ -33,11 +29,9 @@ class Solution { shapes[6].push_back({-j, -i}); shapes[7].push_back({-j, i}); } - for (auto& e : shapes) - { + for (auto& e : shapes) { sort(e.begin(), e.end()); - for (int k = e.size() - 1; k >= 0; --k) - { + for (int k = e.size() - 1; k >= 0; --k) { e[k].first -= e[0].first; e[k].second -= e[0].second; } @@ -50,8 +44,7 @@ class Solution { shape.push_back({i, j}); grid[i][j] = 0; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 1) dfs(x, y, grid, shape); diff --git a/solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/Solution.cpp b/solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/Solution.cpp index dc5f8ac10501c..cd601bef51f76 100644 --- a/solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/Solution.cpp +++ b/solution/0700-0799/0712.Minimum ASCII Delete Sum for Two Strings/Solution.cpp @@ -5,12 +5,12 @@ class Solution { vector> dp(m + 1, vector(n + 1)); for (int i = 1; i <= m; ++i) dp[i][0] = dp[i - 1][0] + s1[i - 1]; for (int j = 1; j <= n; ++j) dp[0][j] = dp[0][j - 1] + s2[j - 1]; - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { - if (s1[i - 1] == s2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; - else dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]); + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (s1[i - 1] == s2[j - 1]) + dp[i][j] = dp[i - 1][j - 1]; + else + dp[i][j] = min(dp[i - 1][j] + s1[i - 1], dp[i][j - 1] + s2[j - 1]); } } return dp[m][n]; diff --git a/solution/0700-0799/0713.Subarray Product Less Than K/Solution.cpp b/solution/0700-0799/0713.Subarray Product Less Than K/Solution.cpp index 1f3dffb27fb92..cf0cf7a1608e9 100644 --- a/solution/0700-0799/0713.Subarray Product Less Than K/Solution.cpp +++ b/solution/0700-0799/0713.Subarray Product Less Than K/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int numSubarrayProductLessThanK(vector& nums, int k) { int ans = 0; - for (int i = 0, j = 0, s = 1; i < nums.size(); ++i) - { + for (int i = 0, j = 0, s = 1; i < nums.size(); ++i) { s *= nums[i]; while (j <= i && s >= k) s /= nums[j++]; ans += i - j + 1; diff --git a/solution/0700-0799/0715.Range Module/Solution.cpp b/solution/0700-0799/0715.Range Module/Solution.cpp index 4a4bd96ef0def..212aa2e7cb68b 100644 --- a/solution/0700-0799/0715.Range Module/Solution.cpp +++ b/solution/0700-0799/0715.Range Module/Solution.cpp @@ -1,38 +1,34 @@ template class CachedObj { public: - void *operator new(size_t s) - { - if (!head) - { - T *a = new T[SIZE]; + void* operator new(size_t s) { + if (!head) { + T* a = new T[SIZE]; for (size_t i = 0; i < SIZE; ++i) add(a + i); } - T *p = head; + T* p = head; head = head->CachedObj::next; return p; } - void operator delete(void *p, size_t) - { - if (p) add(static_cast(p)); + void operator delete(void* p, size_t) { + if (p) add(static_cast(p)); } - virtual ~CachedObj() {} + virtual ~CachedObj() { } protected: - T *next; + T* next; private: - static T *head; + static T* head; static const size_t SIZE; - static void add(T *p) - { + static void add(T* p) { p->CachedObj::next = head; head = p; } }; template -T *CachedObj::head = 0; +T* CachedObj::head = 0; template const size_t CachedObj::SIZE = 10000; class Node : public CachedObj { @@ -57,8 +53,7 @@ class SegmentTree { } void modify(int left, int right, int v, int l, int r, Node* node) { - if (l >= left && r <= right) - { + if (l >= left && r <= right) { node->v = v == 1; node->add = v; return; @@ -91,8 +86,7 @@ class SegmentTree { void pushdown(Node* node) { if (!node->left) node->left = new Node(); if (!node->right) node->right = new Node(); - if (node->add) - { + if (node->add) { node->left->add = node->right->add = node->add; node->left->v = node->right->v = node->add == 1; node->add = 0; @@ -107,15 +101,15 @@ class RangeModule { RangeModule() { tree = new SegmentTree(); } - + void addRange(int left, int right) { tree->modify(left, right - 1, 1); } - + bool queryRange(int left, int right) { return tree->query(left, right - 1); } - + void removeRange(int left, int right) { tree->modify(left, right - 1, -1); } diff --git a/solution/0700-0799/0718.Maximum Length of Repeated Subarray/Solution.cpp b/solution/0700-0799/0718.Maximum Length of Repeated Subarray/Solution.cpp index e351ecbfa3d28..c1af816a20762 100644 --- a/solution/0700-0799/0718.Maximum Length of Repeated Subarray/Solution.cpp +++ b/solution/0700-0799/0718.Maximum Length of Repeated Subarray/Solution.cpp @@ -4,12 +4,9 @@ class Solution { int m = nums1.size(), n = nums2.size(); vector> dp(m + 1, vector(n + 1)); int ans = 0; - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { - if (nums1[i - 1] == nums2[j - 1]) - { + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (nums1[i - 1] == nums2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; ans = max(ans, dp[i][j]); } diff --git a/solution/0700-0799/0719.Find K-th Smallest Pair Distance/Solution.cpp b/solution/0700-0799/0719.Find K-th Smallest Pair Distance/Solution.cpp index 4956d3913b9d3..9da4e6971d25c 100644 --- a/solution/0700-0799/0719.Find K-th Smallest Pair Distance/Solution.cpp +++ b/solution/0700-0799/0719.Find K-th Smallest Pair Distance/Solution.cpp @@ -3,19 +3,19 @@ class Solution { int smallestDistancePair(vector& nums, int k) { sort(nums.begin(), nums.end()); int left = 0, right = nums.back() - nums.front(); - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (count(mid, k, nums) >= k) right = mid; - else left = mid + 1; + if (count(mid, k, nums) >= k) + right = mid; + else + left = mid + 1; } return left; } int count(int dist, int k, vector& nums) { int cnt = 0; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { int target = nums[i] - dist; int j = lower_bound(nums.begin(), nums.end(), target) - nums.begin(); cnt += i - j; diff --git a/solution/0700-0799/0720.Longest Word in Dictionary/Solution.cpp b/solution/0700-0799/0720.Longest Word in Dictionary/Solution.cpp index c81165ac83fe0..49ea37bbdd0b2 100644 --- a/solution/0700-0799/0720.Longest Word in Dictionary/Solution.cpp +++ b/solution/0700-0799/0720.Longest Word in Dictionary/Solution.cpp @@ -4,17 +4,14 @@ class Solution { unordered_set s(words.begin(), words.end()); int cnt = 0; string ans = ""; - for (auto w : s) - { + for (auto w : s) { int n = w.size(); - if (check(w, s)) - { - if (cnt < n) - { + if (check(w, s)) { + if (cnt < n) { cnt = n; ans = w; - } - else if (cnt == n && w < ans) ans = w; + } else if (cnt == n && w < ans) + ans = w; } } return ans; diff --git a/solution/0700-0799/0721.Accounts Merge/Solution.cpp b/solution/0700-0799/0721.Accounts Merge/Solution.cpp index a6d53276cdba5..808f1bd0a0ec9 100644 --- a/solution/0700-0799/0721.Accounts Merge/Solution.cpp +++ b/solution/0700-0799/0721.Accounts Merge/Solution.cpp @@ -7,30 +7,27 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; unordered_map emailId; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { auto account = accounts[i]; auto name = account[0]; - for (int j = 1; j < account.size(); ++j) - { + for (int j = 1; j < account.size(); ++j) { string email = account[j]; - if (emailId.count(email)) p[find(i)] = find(emailId[email]); - else emailId[email] = i; + if (emailId.count(email)) + p[find(i)] = find(emailId[email]); + else + emailId[email] = i; } } unordered_map> mp; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { auto account = accounts[i]; - for (int j = 1; j < account.size(); ++j) - { + for (int j = 1; j < account.size(); ++j) { string email = account[j]; mp[find(i)].insert(email); } } vector> ans; - for (auto& [i, emails] : mp) - { + for (auto& [i, emails] : mp) { vector t; t.push_back(accounts[i][0]); for (string email : emails) t.push_back(email); diff --git a/solution/0700-0799/0723.Candy Crush/Solution.cpp b/solution/0700-0799/0723.Candy Crush/Solution.cpp index 72ac73b41d5e3..3b5e9950a260f 100644 --- a/solution/0700-0799/0723.Candy Crush/Solution.cpp +++ b/solution/0700-0799/0723.Candy Crush/Solution.cpp @@ -3,46 +3,34 @@ class Solution { vector> candyCrush(vector>& board) { int m = board.size(), n = board[0].size(); bool run = true; - while (run) - { + while (run) { run = false; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n - 2; ++j) - { - if (board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j + 1]) && abs(board[i][j]) == abs(board[i][j + 2])) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n - 2; ++j) { + if (board[i][j] != 0 && abs(board[i][j]) == abs(board[i][j + 1]) && abs(board[i][j]) == abs(board[i][j + 2])) { run = true; board[i][j] = board[i][j + 1] = board[i][j + 2] = -abs(board[i][j]); } } } - for (int j = 0; j < n; ++j) - { - for (int i = 0; i < m - 2; ++i) - { - if (board[i][j] != 0 && abs(board[i][j]) == abs(board[i + 1][j]) && abs(board[i][j]) == abs(board[i + 2][j])) - { + for (int j = 0; j < n; ++j) { + for (int i = 0; i < m - 2; ++i) { + if (board[i][j] != 0 && abs(board[i][j]) == abs(board[i + 1][j]) && abs(board[i][j]) == abs(board[i + 2][j])) { run = true; board[i][j] = board[i + 1][j] = board[i + 2][j] = -abs(board[i][j]); } } } - if (run) - { - for (int j = 0; j < n; ++j) - { + if (run) { + for (int j = 0; j < n; ++j) { int curr = m - 1; - for (int i = m - 1; i >= 0; --i) - { - if (board[i][j] > 0) - { + for (int i = m - 1; i >= 0; --i) { + if (board[i][j] > 0) { board[curr][j] = board[i][j]; --curr; } } - while (curr > -1) - { + while (curr > -1) { board[curr][j] = 0; --curr; } diff --git a/solution/0700-0799/0724.Find Pivot Index/Solution.cpp b/solution/0700-0799/0724.Find Pivot Index/Solution.cpp index f5edb3dbf8ade..ac2cb310e8cc2 100644 --- a/solution/0700-0799/0724.Find Pivot Index/Solution.cpp +++ b/solution/0700-0799/0724.Find Pivot Index/Solution.cpp @@ -1,12 +1,11 @@ class Solution { public: - int pivotIndex(vector &nums) { + int pivotIndex(vector& nums) { int s = 0; for (int e : nums) s += e; int presum = 0; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { if (presum * 2 == s - nums[i]) return i; presum += nums[i]; diff --git a/solution/0700-0799/0728.Self Dividing Numbers/Solution.cpp b/solution/0700-0799/0728.Self Dividing Numbers/Solution.cpp index 6f6fbb5802e7d..1bdad0aca71f4 100644 --- a/solution/0700-0799/0728.Self Dividing Numbers/Solution.cpp +++ b/solution/0700-0799/0728.Self Dividing Numbers/Solution.cpp @@ -9,8 +9,7 @@ class Solution { } bool check(int num) { - for (int t = num; t; t /= 10) - { + for (int t = num; t; t /= 10) { int x = t % 10; if (x == 0 || num % x) return false; } diff --git a/solution/0700-0799/0729.My Calendar I/Solution.cpp b/solution/0700-0799/0729.My Calendar I/Solution.cpp index a94406474038f..8c83456ecc38c 100644 --- a/solution/0700-0799/0729.My Calendar I/Solution.cpp +++ b/solution/0700-0799/0729.My Calendar I/Solution.cpp @@ -3,18 +3,15 @@ class MyCalendar { map m; MyCalendar() { - } - + bool book(int start, int end) { ++m[start]; --m[end]; int s = 0; - for (auto& [k, v] : m) - { + for (auto& [k, v] : m) { s += v; - if (s > 1) - { + if (s > 1) { --m[start]; ++m[end]; return false; diff --git a/solution/0700-0799/0730.Count Different Palindromic Subsequences/Solution.cpp b/solution/0700-0799/0730.Count Different Palindromic Subsequences/Solution.cpp index 33ffa42d397e2..1734e631b9ac4 100644 --- a/solution/0700-0799/0730.Count Different Palindromic Subsequences/Solution.cpp +++ b/solution/0700-0799/0730.Count Different Palindromic Subsequences/Solution.cpp @@ -7,22 +7,23 @@ class Solution { int n = s.size(); vector>> dp(n, vector>(n, vector(4))); for (int i = 0; i < n; ++i) dp[i][i][s[i] - 'a'] = 1; - for (int l = 2; l <= n; ++l) - { - for (int i = 0; i + l <= n; ++i) - { + for (int l = 2; l <= n; ++l) { + for (int i = 0; i + l <= n; ++i) { int j = i + l - 1; - for (char c = 'a'; c <= 'd'; ++c) - { + for (char c = 'a'; c <= 'd'; ++c) { int k = c - 'a'; - if (s[i] == c && s[j] == c) dp[i][j][k] = 2 + accumulate(dp[i + 1][j - 1].begin(), dp[i + 1][j - 1].end(), 0ll) % mod; - else if (s[i] == c) dp[i][j][k] = dp[i][j - 1][k]; - else if (s[j] == c) dp[i][j][k] = dp[i + 1][j][k]; - else dp[i][j][k] = dp[i + 1][j - 1][k]; + if (s[i] == c && s[j] == c) + dp[i][j][k] = 2 + accumulate(dp[i + 1][j - 1].begin(), dp[i + 1][j - 1].end(), 0ll) % mod; + else if (s[i] == c) + dp[i][j][k] = dp[i][j - 1][k]; + else if (s[j] == c) + dp[i][j][k] = dp[i + 1][j][k]; + else + dp[i][j][k] = dp[i + 1][j - 1][k]; } } } ll ans = accumulate(dp[0][n - 1].begin(), dp[0][n - 1].end(), 0ll); - return (int) (ans % mod); + return (int)(ans % mod); } }; \ No newline at end of file diff --git a/solution/0700-0799/0731.My Calendar II/Solution.cpp b/solution/0700-0799/0731.My Calendar II/Solution.cpp index 9c1dfef958012..f2b351b1a1932 100644 --- a/solution/0700-0799/0731.My Calendar II/Solution.cpp +++ b/solution/0700-0799/0731.My Calendar II/Solution.cpp @@ -3,18 +3,15 @@ class MyCalendarTwo { map m; MyCalendarTwo() { - } - + bool book(int start, int end) { ++m[start]; --m[end]; int s = 0; - for (auto& [_, v] : m) - { + for (auto& [_, v] : m) { s += v; - if (s > 2) - { + if (s > 2) { --m[start]; ++m[end]; return false; diff --git a/solution/0700-0799/0732.My Calendar III/Solution.cpp b/solution/0700-0799/0732.My Calendar III/Solution.cpp index f00096812eeda..c98f72a193afd 100644 --- a/solution/0700-0799/0732.My Calendar III/Solution.cpp +++ b/solution/0700-0799/0732.My Calendar III/Solution.cpp @@ -30,10 +30,9 @@ class SegmentTree { modify(l, r, v, root); } - void modify(int l, int r,int v, Node* node) { + void modify(int l, int r, int v, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v += v; node->add += v; return; @@ -45,17 +44,17 @@ class SegmentTree { } int query(int l, int r) { - return query(l, r, root); + return query(l, r, root); } int query(int l, int r, Node* node) { if (l > r) return 0; - if (node->l >= l && node-> r <= r) return node->v; + if (node->l >= l && node->r <= r) return node->v; pushdown(node); int v = 0; if (l <= node->mid) v = max(v, query(l, r, node->left)); if (r > node->mid) v = max(v, query(l, r, node->right)); - return v; + return v; } void pushup(Node* node) { @@ -65,8 +64,7 @@ class SegmentTree { void pushdown(Node* node) { if (!node->left) node->left = new Node(node->l, node->mid); if (!node->right) node->right = new Node(node->mid + 1, node->r); - if (node->add) - { + if (node->add) { Node* left = node->left; Node* right = node->right; left->v += node->add; @@ -85,7 +83,7 @@ class MyCalendarThree { MyCalendarThree() { tree = new SegmentTree(); } - + int book(int start, int end) { tree->modify(start + 1, end, 1); return tree->query(1, 1e9 + 1); diff --git a/solution/0700-0799/0734.Sentence Similarity/Solution.cpp b/solution/0700-0799/0734.Sentence Similarity/Solution.cpp index bd8de2f9c817f..8882859e5051c 100644 --- a/solution/0700-0799/0734.Sentence Similarity/Solution.cpp +++ b/solution/0700-0799/0734.Sentence Similarity/Solution.cpp @@ -5,8 +5,7 @@ class Solution { if (m != n) return false; unordered_set s; for (auto e : similarPairs) s.insert(e[0] + "." + e[1]); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { string a = sentence1[i], b = sentence2[i]; if (a != b && !s.count(a + "." + b) && !s.count(b + "." + a)) return false; } diff --git a/solution/0700-0799/0736.Parse Lisp Expression/Solution.cpp b/solution/0700-0799/0736.Parse Lisp Expression/Solution.cpp index 8f82ca46e3fc3..c2f3576545ddb 100644 --- a/solution/0700-0799/0736.Parse Lisp Expression/Solution.cpp +++ b/solution/0700-0799/0736.Parse Lisp Expression/Solution.cpp @@ -13,15 +13,12 @@ class Solution { if (expr[i] != '(') return islower(expr[i]) ? scope[parseVar()].back() : parseInt(); int ans = 0; ++i; - if (expr[i] == 'l') - { + if (expr[i] == 'l') { i += 4; vector vars; - while (1) - { + while (1) { string var = parseVar(); - if (expr[i] == ')') - { + if (expr[i] == ')') { ans = scope[var].back(); break; } @@ -29,16 +26,13 @@ class Solution { vars.push_back(var); scope[var].push_back(eval()); ++i; - if (!islower(expr[i])) - { + if (!islower(expr[i])) { ans = eval(); break; } } for (string v : vars) scope[v].pop_back(); - } - else - { + } else { bool add = expr[i] == 'a'; i += add ? 4 : 5; int a = eval(); @@ -58,13 +52,11 @@ class Solution { int parseInt() { int sign = 1, v = 0; - if (expr[i] == '-') - { + if (expr[i] == '-') { sign = -1; ++i; } - while (i < expr.size() && expr[i] >= '0' && expr[i] <= '9') - { + while (i < expr.size() && expr[i] >= '0' && expr[i] <= '9') { v = v * 10 + (expr[i] - '0'); ++i; } diff --git a/solution/0700-0799/0737.Sentence Similarity II/Solution.cpp b/solution/0700-0799/0737.Sentence Similarity II/Solution.cpp index b6c34550ff2aa..2a502e2464a21 100644 --- a/solution/0700-0799/0737.Sentence Similarity II/Solution.cpp +++ b/solution/0700-0799/0737.Sentence Similarity II/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: vector p; - bool areSentencesSimilarTwo(vector &sentence1, vector &sentence2, vector> &similarPairs) { + bool areSentencesSimilarTwo(vector& sentence1, vector& sentence2, vector>& similarPairs) { if (sentence1.size() != sentence2.size()) return false; int n = similarPairs.size(); @@ -10,8 +10,7 @@ class Solution { p[i] = i; unordered_map words; int idx = 0; - for (auto e : similarPairs) - { + for (auto e : similarPairs) { string a = e[0], b = e[1]; if (!words.count(a)) words[a] = idx++; @@ -19,8 +18,7 @@ class Solution { words[b] = idx++; p[find(words[a])] = find(words[b]); } - for (int i = 0; i < sentence1.size(); ++i) - { + for (int i = 0; i < sentence1.size(); ++i) { if (sentence1[i] == sentence2[i]) continue; if (!words.count(sentence1[i]) || !words.count(sentence2[i]) || find(words[sentence1[i]]) != find(words[sentence2[i]])) diff --git a/solution/0700-0799/0739.Daily Temperatures/Solution.cpp b/solution/0700-0799/0739.Daily Temperatures/Solution.cpp index 5b49f3c253825..7b71f8274ef28 100644 --- a/solution/0700-0799/0739.Daily Temperatures/Solution.cpp +++ b/solution/0700-0799/0739.Daily Temperatures/Solution.cpp @@ -1,13 +1,11 @@ class Solution { public: - vector dailyTemperatures(vector &temperatures) { + vector dailyTemperatures(vector& temperatures) { int n = temperatures.size(); vector ans(n); stack stk; - for (int i = 0; i < n; ++i) - { - while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) - { + for (int i = 0; i < n; ++i) { + while (!stk.empty() && temperatures[stk.top()] < temperatures[i]) { ans[stk.top()] = i - stk.top(); stk.pop(); } diff --git a/solution/0700-0799/0741.Cherry Pickup/Solution.cpp b/solution/0700-0799/0741.Cherry Pickup/Solution.cpp index 8d6ed7908a5d5..d0d3ba8ef70ab 100644 --- a/solution/0700-0799/0741.Cherry Pickup/Solution.cpp +++ b/solution/0700-0799/0741.Cherry Pickup/Solution.cpp @@ -4,12 +4,9 @@ class Solution { int n = grid.size(); vector>> dp(n << 1, vector>(n, vector(n, -1e9))); dp[0][0][0] = grid[0][0]; - for (int k = 1; k < n * 2 - 1; ++k) - { - for (int i1 = 0; i1 < n; ++i1) - { - for (int i2 = 0; i2 < n; ++i2) - { + for (int k = 1; k < n * 2 - 1; ++k) { + for (int i1 = 0; i1 < n; ++i1) { + for (int i2 = 0; i2 < n; ++i2) { int j1 = k - i1, j2 = k - i2; if (j1 < 0 || j1 >= n || j2 < 0 || j2 >= n || grid[i1][j1] == -1 || grid[i2][j2] == -1) continue; int t = grid[i1][j1]; diff --git a/solution/0700-0799/0742.Closest Leaf in a Binary Tree/Solution.cpp b/solution/0700-0799/0742.Closest Leaf in a Binary Tree/Solution.cpp index 2a9d0cd39a6a5..856f619807630 100644 --- a/solution/0700-0799/0742.Closest Leaf in a Binary Tree/Solution.cpp +++ b/solution/0700-0799/0742.Closest Leaf in a Binary Tree/Solution.cpp @@ -16,25 +16,20 @@ class Solution { int findClosestLeaf(TreeNode* root, int k) { dfs(root, nullptr); queue q; - for (auto& e : g) - { - if (e.first && e.first->val == k) - { + for (auto& e : g) { + if (e.first && e.first->val == k) { q.push(e.first); break; } } unordered_set seen; - while (!q.empty()) - { + while (!q.empty()) { auto node = q.front(); q.pop(); seen.insert(node); - if (node) - { + if (node) { if (!node->left && !node->right) return node->val; - for (auto next : g[node]) - { + for (auto next : g[node]) { if (!seen.count(next)) q.push(next); } diff --git a/solution/0700-0799/0743.Network Delay Time/Solution.cpp b/solution/0700-0799/0743.Network Delay Time/Solution.cpp index 9b34c42685420..c68ed97c30279 100644 --- a/solution/0700-0799/0743.Network Delay Time/Solution.cpp +++ b/solution/0700-0799/0743.Network Delay Time/Solution.cpp @@ -8,19 +8,15 @@ class Solution { vector vis(n); vector dist(n, inf); dist[k - 1] = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int t = -1; - for (int j = 0; j < n; ++j) - { - if (!vis[j] && (t == -1 || dist[t] > dist[j])) - { + for (int j = 0; j < n; ++j) { + if (!vis[j] && (t == -1 || dist[t] > dist[j])) { t = j; } } vis[t] = true; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { dist[j] = min(dist[j], dist[t] + g[t][j]); } } diff --git a/solution/0700-0799/0745.Prefix and Suffix Search/Solution.cpp b/solution/0700-0799/0745.Prefix and Suffix Search/Solution.cpp index cdb889c07b0da..dd554dbe666b7 100644 --- a/solution/0700-0799/0745.Prefix and Suffix Search/Solution.cpp +++ b/solution/0700-0799/0745.Prefix and Suffix Search/Solution.cpp @@ -3,22 +3,19 @@ class WordFilter { unordered_map d; WordFilter(vector& words) { - for (int k = 0; k < words.size(); ++k) - { + for (int k = 0; k < words.size(); ++k) { string w = words[k]; int n = w.size(); - for (int i = 0; i <= n; ++i) - { + for (int i = 0; i <= n; ++i) { string a = w.substr(0, i); - for (int j = 0; j <= n; ++j) - { + for (int j = 0; j <= n; ++j) { string b = w.substr(j, n - j); d[a + "." + b] = k; } } } } - + int f(string pref, string suff) { string key = pref + "." + suff; if (d.count(key)) return d[key]; diff --git a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp index 6835d19a3b54f..3e00c2f9a9f15 100644 --- a/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp +++ b/solution/0700-0799/0747.Largest Number At Least Twice of Others/Solution.cpp @@ -3,15 +3,13 @@ class Solution { int dominantIndex(vector& nums) { int mx = 0, mid = 0; int ans = 0; - for (int i = 0; i < nums.size(); ++i) - { - if (nums[i] > mx) - { + for (int i = 0; i < nums.size(); ++i) { + if (nums[i] > mx) { mid = mx; mx = nums[i]; ans = i; - } - else if (nums[i] > mid) mid = nums[i]; + } else if (nums[i] > mid) + mid = nums[i]; } return mx >= mid * 2 ? ans : -1; } diff --git a/solution/0700-0799/0748.Shortest Completing Word/Solution.cpp b/solution/0700-0799/0748.Shortest Completing Word/Solution.cpp index e8cac8edbc586..0694afd751f49 100644 --- a/solution/0700-0799/0748.Shortest Completing Word/Solution.cpp +++ b/solution/0700-0799/0748.Shortest Completing Word/Solution.cpp @@ -4,12 +4,10 @@ class Solution { vector counter = count(licensePlate); int n = 16; string ans; - for (auto& word : words) - { + for (auto& word : words) { if (n <= word.size()) continue; vector t = count(word); - if (check(counter, t)) - { + if (check(counter, t)) { n = word.size(); ans = word; } diff --git a/solution/0700-0799/0749.Contain Virus/Solution.cpp b/solution/0700-0799/0749.Contain Virus/Solution.cpp index 9f09427631e54..7e7d2ddc33940 100644 --- a/solution/0700-0799/0749.Contain Virus/Solution.cpp +++ b/solution/0700-0799/0749.Contain Virus/Solution.cpp @@ -15,18 +15,15 @@ class Solution { n = infected[0].size(); vis.assign(m, vector(n)); int ans = 0; - while (1) - { - for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) vis[i][j] = false; + while (1) { + for (int i = 0; i < m; ++i) + for (int j = 0; j < n; ++j) vis[i][j] = false; c.clear(); areas.clear(); boundaries.clear(); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (infected[i][j] == 1 && !vis[i][j]) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (infected[i][j] == 1 && !vis[i][j]) { c.push_back(0); areas.push_back({}); boundaries.push_back({}); @@ -37,23 +34,16 @@ class Solution { if (areas.empty()) break; int idx = getMax(); ans += c[idx]; - for (int t = 0; t < areas.size(); ++t) - { - if (t == idx) - { - for (int v : areas[t]) - { + for (int t = 0; t < areas.size(); ++t) { + if (t == idx) { + for (int v : areas[t]) { int i = v / n, j = v % n; infected[i][j] = -1; } - } - else - { - for (int v : areas[t]) - { + } else { + for (int v : areas[t]) { int i = v / n, j = v % n; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && infected[x][y] == 0) infected[x][y] = 1; } @@ -67,11 +57,9 @@ class Solution { int getMax() { int idx = 0; int mx = boundaries[0].size(); - for (int i = 1; i < boundaries.size(); ++i) - { + for (int i = 1; i < boundaries.size(); ++i) { int t = boundaries[i].size(); - if (mx < t) - { + if (mx < t) { mx = t; idx = i; } @@ -82,14 +70,12 @@ class Solution { void dfs(int i, int j) { vis[i][j] = true; areas.back().push_back(i * n + j); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { - if (infected[x][y] == 1 && !vis[x][y]) dfs(x, y); - else if (infected[x][y] == 0) - { + if (x >= 0 && x < m && y >= 0 && y < n) { + if (infected[x][y] == 1 && !vis[x][y]) + dfs(x, y); + else if (infected[x][y] == 0) { c.back() += 1; boundaries.back().insert(x * n + y); } diff --git a/solution/0700-0799/0752.Open the Lock/Solution.cpp b/solution/0700-0799/0752.Open the Lock/Solution.cpp index 6ce90fe6e79e1..8acebaff3b0b5 100644 --- a/solution/0700-0799/0752.Open the Lock/Solution.cpp +++ b/solution/0700-0799/0752.Open the Lock/Solution.cpp @@ -10,7 +10,7 @@ class Solution { if (s.count("0000")) return -1; this->start = "0000"; this->target = target; - return bfs(); + return bfs(); } int bfs() { @@ -18,10 +18,9 @@ class Solution { unordered_map m2; m1[start] = 0; m2[target] = 0; - queue q1{{start}}; - queue q2{{target}}; - while (!q1.empty() && !q2.empty()) - { + queue q1 {{start}}; + queue q2 {{target}}; + while (!q1.empty() && !q2.empty()) { int t = q1.size() <= q2.size() ? extend(m1, m2, q1) : extend(m2, m1, q2); if (t != -1) return t; } @@ -29,13 +28,11 @@ class Solution { } int extend(unordered_map& m1, unordered_map& m2, queue& q) { - for (int n = q.size(); n > 0; --n) - { + for (int n = q.size(); n > 0; --n) { string p = q.front(); int step = m1[p]; q.pop(); - for (string t : next(p)) - { + for (string t : next(p)) { if (s.count(t) || m1.count(t)) continue; if (m2.count(t)) return step + 1 + m2[t]; m1[t] = step + 1; @@ -47,12 +44,11 @@ class Solution { vector next(string& t) { vector res; - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { char c = t[i]; - t[i] = c == '0' ? '9' : (char) (c - 1); + t[i] = c == '0' ? '9' : (char)(c - 1); res.push_back(t); - t[i] = c == '9' ? '0' : (char) (c + 1); + t[i] = c == '9' ? '0' : (char)(c + 1); res.push_back(t); t[i] = c; } diff --git a/solution/0700-0799/0757.Set Intersection Size At Least Two/Solution.cpp b/solution/0700-0799/0757.Set Intersection Size At Least Two/Solution.cpp index 50c529b8013db..0920a80a0a3b4 100644 --- a/solution/0700-0799/0757.Set Intersection Size At Least Two/Solution.cpp +++ b/solution/0700-0799/0757.Set Intersection Size At Least Two/Solution.cpp @@ -6,18 +6,14 @@ class Solution { }); int ans = 0; int s = -1, e = -1; - for (auto& v : intervals) - { + for (auto& v : intervals) { int a = v[0], b = v[1]; if (a <= s) continue; - if (a > e) - { + if (a > e) { ans += 2; s = b - 1; e = b; - } - else - { + } else { ans += 1; s = e; e = b; diff --git a/solution/0700-0799/0758.Bold Words in String/Solution.cpp b/solution/0700-0799/0758.Bold Words in String/Solution.cpp index 3216ca60cad96..20b2b881134e0 100644 --- a/solution/0700-0799/0758.Bold Words in String/Solution.cpp +++ b/solution/0700-0799/0758.Bold Words in String/Solution.cpp @@ -10,8 +10,7 @@ class Trie { void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; } @@ -26,11 +25,9 @@ class Solution { for (string w : words) trie->insert(w); int n = s.size(); vector> pairs; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { Trie* node = trie; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { int idx = s[j]; if (!node->children[idx]) break; node = node->children[idx]; @@ -40,23 +37,19 @@ class Solution { if (pairs.empty()) return s; vector> t; int st = pairs[0].first, ed = pairs[0].second; - for (int i = 1; i < pairs.size(); ++i) - { + for (int i = 1; i < pairs.size(); ++i) { int a = pairs[i].first, b = pairs[i].second; - if (ed + 1 < a) - { + if (ed + 1 < a) { t.push_back({st, ed}); st = a, ed = b; - } - else ed = max(ed, b); + } else + ed = max(ed, b); } t.push_back({st, ed}); string ans = ""; int i = 0, j = 0; - while (i < n) - { - if (j == t.size()) - { + while (i < n) { + if (j == t.size()) { ans += s.substr(i); break; } diff --git a/solution/0700-0799/0761.Special Binary String/Solution.cpp b/solution/0700-0799/0761.Special Binary String/Solution.cpp index 53a563d7a6f80..98ebe58e63ee9 100644 --- a/solution/0700-0799/0761.Special Binary String/Solution.cpp +++ b/solution/0700-0799/0761.Special Binary String/Solution.cpp @@ -4,16 +4,14 @@ class Solution { if (s == "") return s; vector ans; int cnt = 0; - for (int i = 0, j = 0; i < s.size(); ++i) - { + for (int i = 0, j = 0; i < s.size(); ++i) { cnt += s[i] == '1' ? 1 : -1; - if (cnt == 0) - { + if (cnt == 0) { ans.push_back("1" + makeLargestSpecial(s.substr(j + 1, i - j - 1)) + "0"); j = i + 1; } } - sort(ans.begin(), ans.end(), greater{}); + sort(ans.begin(), ans.end(), greater {}); return accumulate(ans.begin(), ans.end(), ""s); } }; \ No newline at end of file diff --git a/solution/0700-0799/0762.Prime Number of Set Bits in Binary Representation/Solution.cpp b/solution/0700-0799/0762.Prime Number of Set Bits in Binary Representation/Solution.cpp index 1d9f1b2aab73b..177839fde07d1 100644 --- a/solution/0700-0799/0762.Prime Number of Set Bits in Binary Representation/Solution.cpp +++ b/solution/0700-0799/0762.Prime Number of Set Bits in Binary Representation/Solution.cpp @@ -1,6 +1,6 @@ class Solution { public: - unordered_set primes{2, 3, 5, 7, 11, 13, 17, 19}; + unordered_set primes {2, 3, 5, 7, 11, 13, 17, 19}; int countPrimeSetBits(int left, int right) { int ans = 0; diff --git a/solution/0700-0799/0763.Partition Labels/Solution.cpp b/solution/0700-0799/0763.Partition Labels/Solution.cpp index 3d0fae78b4bce..693eea02b9b35 100644 --- a/solution/0700-0799/0763.Partition Labels/Solution.cpp +++ b/solution/0700-0799/0763.Partition Labels/Solution.cpp @@ -5,11 +5,9 @@ class Solution { int n = s.size(); for (int i = 0; i < n; ++i) last[s[i] - 'a'] = i; vector ans; - for (int i = 0, left = 0, right = 0; i < n; ++i) - { + for (int i = 0, left = 0, right = 0; i < n; ++i) { right = max(right, last[s[i] - 'a']); - if (i == right) - { + if (i == right) { ans.push_back(right - left + 1); left = right + 1; } diff --git a/solution/0700-0799/0765.Couples Holding Hands/Solution.cpp b/solution/0700-0799/0765.Couples Holding Hands/Solution.cpp index 93a7ae70e3a5c..00a7adba78cd8 100644 --- a/solution/0700-0799/0765.Couples Holding Hands/Solution.cpp +++ b/solution/0700-0799/0765.Couples Holding Hands/Solution.cpp @@ -2,21 +2,18 @@ class Solution { public: vector p; - int minSwapsCouples(vector &row) { + int minSwapsCouples(vector& row) { int n = row.size() >> 1; p.resize(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { p[i] = i; } - for (int i = 0; i < row.size(); i += 2) - { + for (int i = 0; i < row.size(); i += 2) { int a = row[i] >> 1, b = row[i + 1] >> 1; p[find(a)] = find(b); } int cnt = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (i == find(i)) ++cnt; } @@ -24,8 +21,7 @@ class Solution { } int find(int x) { - if (p[x] != x) - { + if (p[x] != x) { p[x] = find(p[x]); } return p[x]; diff --git a/solution/0700-0799/0767.Reorganize String/Solution.cpp b/solution/0700-0799/0767.Reorganize String/Solution.cpp index 90225a94b9758..fdf21639142c9 100644 --- a/solution/0700-0799/0767.Reorganize String/Solution.cpp +++ b/solution/0700-0799/0767.Reorganize String/Solution.cpp @@ -7,19 +7,16 @@ class Solution { int n = s.size(); if (mx > (n + 1) / 2) return ""; vector> m; - for (int i = 0; i < 26; ++i) - { + for (int i = 0; i < 26; ++i) { if (cnt[i]) m.push_back({cnt[i], i}); } sort(m.begin(), m.end()); reverse(m.begin(), m.end()); string ans = s; int k = 0; - for (auto& e : m) - { + for (auto& e : m) { int v = e[0], i = e[1]; - while (v--) - { + while (v--) { ans[k] = 'a' + i; k += 2; if (k >= n) k = 1; diff --git a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp index b46ce865aa75e..0d8825769adfd 100644 --- a/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp +++ b/solution/0700-0799/0768.Max Chunks To Make Sorted II/Solution.cpp @@ -2,11 +2,10 @@ class Solution { public: int maxChunksToSorted(vector& arr) { stack stk; - for (int& v : arr) - { - if (stk.empty() || stk.top() <= v) stk.push(v); - else - { + for (int& v : arr) { + if (stk.empty() || stk.top() <= v) + stk.push(v); + else { int mx = stk.top(); stk.pop(); while (!stk.empty() && stk.top() > v) stk.pop(); diff --git a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.cpp b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.cpp index 28cb2dbf41d69..2f9b3d12361c1 100644 --- a/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.cpp +++ b/solution/0700-0799/0769.Max Chunks To Make Sorted/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int maxChunksToSorted(vector& arr) { int ans = 0; int mx = 0; - for (int i = 0; i < arr.size(); ++i) - { + for (int i = 0; i < arr.size(); ++i) { mx = max(mx, arr[i]); ans += i == mx; } diff --git a/solution/0700-0799/0773.Sliding Puzzle/Solution.cpp b/solution/0700-0799/0773.Sliding Puzzle/Solution.cpp index 96f5aa41689d1..bc21ba79c6567 100644 --- a/solution/0700-0799/0773.Sliding Puzzle/Solution.cpp +++ b/solution/0700-0799/0773.Sliding Puzzle/Solution.cpp @@ -6,23 +6,20 @@ class Solution { int slidingPuzzle(vector>& board) { string start, seq; string end = "123450"; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { start += char(board[i][j] + '0'); if (board[i][j] != 0) seq += char(board[i][j] + '0'); } } if (!check(seq)) return -1; - typedef pair PIS; + typedef pair PIS; priority_queue, greater> q; unordered_map dist; dist[start] = 0; q.push({f(start), start}); vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { PIS t = q.top(); q.pop(); string state = t.second; @@ -30,14 +27,12 @@ class Solution { if (state == end) return step; int p1 = state.find('0'); int i = p1 / n, j = p1 % n; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x < 0 || x >= m || y < 0 || y >= n) continue; int p2 = x * n + y; swap(state[p1], state[p2]); - if (!dist.count(state) || dist[state] > step + 1) - { + if (!dist.count(state) || dist[state] > step + 1) { dist[state] = step + 1; q.push({step + 1 + f(state), state}); } @@ -45,7 +40,7 @@ class Solution { } } return -1; - } + } bool check(string s) { int n = s.size(); @@ -59,8 +54,7 @@ class Solution { int f(string s) { int ans = 0; - for (int i = 0; i < m * n; ++i) - { + for (int i = 0; i < m * n; ++i) { if (s[i] == '0') continue; int num = s[i] - '1'; ans += abs(num / n - i / n) + abs(num % n - i % n); diff --git a/solution/0700-0799/0778.Swim in Rising Water/Solution.cpp b/solution/0700-0799/0778.Swim in Rising Water/Solution.cpp index 61bfdee53f61b..08352805fd71e 100644 --- a/solution/0700-0799/0778.Swim in Rising Water/Solution.cpp +++ b/solution/0700-0799/0778.Swim in Rising Water/Solution.cpp @@ -11,11 +11,9 @@ class Solution { for (int j = 0; j < n; ++j) hi[grid[i][j]] = i * n + j; vector dirs = {-1, 0, 1, 0, -1}; - for (int t = 0; t < n * n; ++t) - { + for (int t = 0; t < n * n; ++t) { int i = hi[t] / n, j = hi[t] % n; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] <= t) p[find(x * n + y)] = find(hi[t]); diff --git a/solution/0700-0799/0780.Reaching Points/Solution.cpp b/solution/0700-0799/0780.Reaching Points/Solution.cpp index 7111482a78070..717fa4413b93b 100644 --- a/solution/0700-0799/0780.Reaching Points/Solution.cpp +++ b/solution/0700-0799/0780.Reaching Points/Solution.cpp @@ -1,10 +1,11 @@ class Solution { public: bool reachingPoints(int sx, int sy, int tx, int ty) { - while (tx > sx && ty > sy && tx != ty) - { - if (tx > ty) tx %= ty; - else ty %= tx; + while (tx > sx && ty > sy && tx != ty) { + if (tx > ty) + tx %= ty; + else + ty %= tx; } if (tx == sx && ty == sy) return true; if (tx == sx) return ty > sy && (ty - sy) % tx == 0; diff --git a/solution/0700-0799/0783.Minimum Distance Between BST Nodes/Solution.cpp b/solution/0700-0799/0783.Minimum Distance Between BST Nodes/Solution.cpp index 9d9fd9a060a54..64c21f404198c 100644 --- a/solution/0700-0799/0783.Minimum Distance Between BST Nodes/Solution.cpp +++ b/solution/0700-0799/0783.Minimum Distance Between BST Nodes/Solution.cpp @@ -14,7 +14,7 @@ class Solution { const int inf = INT_MAX; int ans; int prev; - + int minDiffInBST(TreeNode* root) { ans = inf, prev = inf; dfs(root); diff --git a/solution/0700-0799/0784.Letter Case Permutation/Solution.cpp b/solution/0700-0799/0784.Letter Case Permutation/Solution.cpp index 5c549766c7911..24e4cf6f26dcf 100644 --- a/solution/0700-0799/0784.Letter Case Permutation/Solution.cpp +++ b/solution/0700-0799/0784.Letter Case Permutation/Solution.cpp @@ -11,20 +11,16 @@ class Solution { } void dfs(int i, string t) { - if (i == s.size()) - { + if (i == s.size()) { ans.push_back(t); return; } - if (isalpha(s[i])) - { + if (isalpha(s[i])) { char c1 = toupper(s[i]); char c2 = tolower(s[i]); dfs(i + 1, t + c1); dfs(i + 1, t + c2); - } - else - { + } else { dfs(i + 1, t + s[i]); } } diff --git a/solution/0700-0799/0785.Is Graph Bipartite/Solution.cpp b/solution/0700-0799/0785.Is Graph Bipartite/Solution.cpp index c4f56c4ba4b1b..a70e6639c8023 100644 --- a/solution/0700-0799/0785.Is Graph Bipartite/Solution.cpp +++ b/solution/0700-0799/0785.Is Graph Bipartite/Solution.cpp @@ -11,13 +11,11 @@ class Solution { bool dfs(int u, int c, vector& color, vector>& g) { color[u] = c; - for (int& v : g[u]) - { - if (!color[v]) - { + for (int& v : g[u]) { + if (!color[v]) { if (!dfs(v, 3 - c, color, g)) return false; - } - else if (color[v] == c) return false; + } else if (color[v] == c) + return false; } return true; } diff --git a/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.cpp b/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.cpp index e95e4725be787..1e21f4539f531 100644 --- a/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.cpp +++ b/solution/0700-0799/0786.K-th Smallest Prime Fraction/Solution.cpp @@ -3,7 +3,7 @@ class Solution { vector kthSmallestPrimeFraction(vector& arr, int k) { using pii = pair; int n = arr.size(); - auto cmp = [&](const pii &a, const pii &b) { + auto cmp = [&](const pii& a, const pii& b) { return arr[a.first] * arr[b.second] > arr[b.first] * arr[a.second]; }; priority_queue, decltype(cmp)> pq(cmp); diff --git a/solution/0700-0799/0787.Cheapest Flights Within K Stops/Solution.cpp b/solution/0700-0799/0787.Cheapest Flights Within K Stops/Solution.cpp index 552818b489301..f370447f4e397 100644 --- a/solution/0700-0799/0787.Cheapest Flights Within K Stops/Solution.cpp +++ b/solution/0700-0799/0787.Cheapest Flights Within K Stops/Solution.cpp @@ -5,11 +5,9 @@ class Solution { vector dist(n, inf); vector backup; dist[src] = 0; - for (int i = 0; i < k + 1; ++i) - { + for (int i = 0; i < k + 1; ++i) { backup = dist; - for (auto& e : flights) - { + for (auto& e : flights) { int f = e[0], t = e[1], p = e[2]; dist[t] = min(dist[t], backup[f] + p); } diff --git a/solution/0700-0799/0792.Number of Matching Subsequences/Solution.cpp b/solution/0700-0799/0792.Number of Matching Subsequences/Solution.cpp index e7cb3c4780ed1..ad5f6132677f6 100644 --- a/solution/0700-0799/0792.Number of Matching Subsequences/Solution.cpp +++ b/solution/0700-0799/0792.Number of Matching Subsequences/Solution.cpp @@ -4,14 +4,14 @@ class Solution { vector> buckets(26); for (auto word : words) buckets[word[0] - 'a'].push_back(word); int res = 0; - for (auto c : s) - { + for (auto c : s) { auto old = buckets[c - 'a']; buckets[c - 'a'].clear(); - for (auto t : old) - { - if (t.size() == 1) ++res; - else buckets[t[1] - 'a'].push_back(t.substr(1)); + for (auto t : old) { + if (t.size() == 1) + ++res; + else + buckets[t[1] - 'a'].push_back(t.substr(1)); } } return res; diff --git a/solution/0700-0799/0797.All Paths From Source to Target/Solution.cpp b/solution/0700-0799/0797.All Paths From Source to Target/Solution.cpp index 914233a9ce92b..fcd84fdf595fd 100644 --- a/solution/0700-0799/0797.All Paths From Source to Target/Solution.cpp +++ b/solution/0700-0799/0797.All Paths From Source to Target/Solution.cpp @@ -12,13 +12,11 @@ } void dfs(int i, vector path) { - if (i == graph.size() - 1) - { + if (i == graph.size() - 1) { ans.push_back(path); return; } - for (int j : graph[i]) - { + for (int j : graph[i]) { path.push_back(j); dfs(j, path); path.pop_back(); diff --git a/solution/0700-0799/0798.Smallest Rotation with Highest Score/Solution.cpp b/solution/0700-0799/0798.Smallest Rotation with Highest Score/Solution.cpp index 101b6c33dffdc..18e2ae8240c27 100644 --- a/solution/0700-0799/0798.Smallest Rotation with Highest Score/Solution.cpp +++ b/solution/0700-0799/0798.Smallest Rotation with Highest Score/Solution.cpp @@ -4,19 +4,16 @@ class Solution { int n = nums.size(); int mx = -1, ans = n; vector d(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int l = (i + 1) % n; int r = (n + i + 1 - nums[i]) % n; ++d[l]; --d[r]; } int s = 0; - for (int k = 0; k < n; ++k) - { + for (int k = 0; k < n; ++k) { s += d[k]; - if (s > mx) - { + if (s > mx) { mx = s; ans = k; } diff --git a/solution/0800-0899/0802.Find Eventual Safe States/Solution.cpp b/solution/0800-0899/0802.Find Eventual Safe States/Solution.cpp index 5dc34efacccaf..4cba32551e75e 100644 --- a/solution/0800-0899/0802.Find Eventual Safe States/Solution.cpp +++ b/solution/0800-0899/0802.Find Eventual Safe States/Solution.cpp @@ -6,14 +6,16 @@ class Solution { int n = graph.size(); color.assign(n, 0); vector ans; - for (int i = 0; i < n; ++i) if (dfs(i, graph)) ans.push_back(i); + for (int i = 0; i < n; ++i) + if (dfs(i, graph)) ans.push_back(i); return ans; } bool dfs(int i, vector>& g) { if (color[i]) return color[i] == 2; color[i] = 1; - for (int j : g[i]) if (!dfs(j, g)) return false; + for (int j : g[i]) + if (!dfs(j, g)) return false; color[i] = 2; return true; } diff --git a/solution/0800-0899/0803.Bricks Falling When Hit/Solution.cpp b/solution/0800-0899/0803.Bricks Falling When Hit/Solution.cpp index ff68c86cde193..f3d188e4c93d3 100644 --- a/solution/0800-0899/0803.Bricks Falling When Hit/Solution.cpp +++ b/solution/0800-0899/0803.Bricks Falling When Hit/Solution.cpp @@ -7,8 +7,7 @@ class Solution { int m = grid.size(), n = grid[0].size(); p.resize(m * n + 1); size.resize(m * n + 1); - for (int i = 0; i < p.size(); ++i) - { + for (int i = 0; i < p.size(); ++i) { p[i] = i; size[i] = 1; } @@ -20,10 +19,8 @@ class Solution { for (int j = 0; j < n; ++j) if (g[0][j] == 1) merge(j, m * n); - for (int i = 1; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 1; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (g[i][j] == 0) continue; if (g[i - 1][j] == 1) merge(i * n + j, (i - 1) * n + j); if (j > 0 && g[i][j - 1] == 1) merge(i * n + j, i * n + j - 1); @@ -31,15 +28,13 @@ class Solution { } vector ans(hits.size()); vector dirs = {-1, 0, 1, 0, -1}; - for (int k = hits.size() - 1; k >= 0; --k) - { + for (int k = hits.size() - 1; k >= 0; --k) { int i = hits[k][0], j = hits[k][1]; if (grid[i][j] == 0) continue; g[i][j] = 1; int prev = size[find(m * n)]; if (i == 0) merge(j, m * n); - for (int l = 0; l < 4; ++l) - { + for (int l = 0; l < 4; ++l) { int x = i + dirs[l], y = j + dirs[l + 1]; if (x >= 0 && x < m && y >= 0 && y < n && g[x][y] == 1) merge(i * n + j, x * n + y); @@ -57,8 +52,7 @@ class Solution { void merge(int a, int b) { int pa = find(a), pb = find(b); - if (pa != pb) - { + if (pa != pb) { size[pb] += size[pa]; p[pa] = pb; } diff --git a/solution/0800-0899/0804.Unique Morse Code Words/Solution.cpp b/solution/0800-0899/0804.Unique Morse Code Words/Solution.cpp index 93562438d509b..93c2abc112bec 100644 --- a/solution/0800-0899/0804.Unique Morse Code Words/Solution.cpp +++ b/solution/0800-0899/0804.Unique Morse Code Words/Solution.cpp @@ -2,10 +2,9 @@ class Solution { public: int uniqueMorseRepresentations(vector& words) { vector codes = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", - "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; + "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."}; unordered_set s; - for (auto& word : words) - { + for (auto& word : words) { string t; for (char& c : word) t += codes[c - 'a']; s.insert(t); diff --git a/solution/0800-0899/0806.Number of Lines To Write String/Solution.cpp b/solution/0800-0899/0806.Number of Lines To Write String/Solution.cpp index 9007d2591f5e7..391f5533467a6 100644 --- a/solution/0800-0899/0806.Number of Lines To Write String/Solution.cpp +++ b/solution/0800-0899/0806.Number of Lines To Write String/Solution.cpp @@ -1,15 +1,14 @@ class Solution { public: const int MAX_WIDTH = 100; - + vector numberOfLines(vector& widths, string s) { int last = 0, row = 1; - for (char c : s) - { + for (char c : s) { int w = widths[c - 'a']; - if (last + w <= MAX_WIDTH) last += w; - else - { + if (last + w <= MAX_WIDTH) + last += w; + else { ++row; last = w; } diff --git a/solution/0800-0899/0807.Max Increase to Keep City Skyline/Solution.cpp b/solution/0800-0899/0807.Max Increase to Keep City Skyline/Solution.cpp index 243e90d5a9e75..28ac200e70e13 100644 --- a/solution/0800-0899/0807.Max Increase to Keep City Skyline/Solution.cpp +++ b/solution/0800-0899/0807.Max Increase to Keep City Skyline/Solution.cpp @@ -4,10 +4,8 @@ class Solution { int m = grid.size(), n = grid[0].size(); vector rmx(m, 0); vector cmx(n, 0); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { rmx[i] = max(rmx[i], grid[i][j]); cmx[j] = max(cmx[j], grid[i][j]); } diff --git a/solution/0800-0899/0812.Largest Triangle Area/Solution.cpp b/solution/0800-0899/0812.Largest Triangle Area/Solution.cpp index 71364aa6416ed..064a0598d7d8c 100644 --- a/solution/0800-0899/0812.Largest Triangle Area/Solution.cpp +++ b/solution/0800-0899/0812.Largest Triangle Area/Solution.cpp @@ -2,14 +2,11 @@ class Solution { public: double largestTriangleArea(vector>& points) { double ans = 0; - for (auto& p1 : points) - { + for (auto& p1 : points) { int x1 = p1[0], y1 = p1[1]; - for (auto& p2 : points) - { + for (auto& p2 : points) { int x2 = p2[0], y2 = p2[1]; - for (auto& p3 : points) - { + for (auto& p3 : points) { int x3 = p3[0], y3 = p3[1]; int u1 = x2 - x1, v1 = y2 - y1; int u2 = x3 - x1, v2 = y3 - y1; diff --git a/solution/0800-0899/0819.Most Common Word/Solution.cpp b/solution/0800-0899/0819.Most Common Word/Solution.cpp index 456eb0b6d6f99..05a43f17e6507 100644 --- a/solution/0800-0899/0819.Most Common Word/Solution.cpp +++ b/solution/0800-0899/0819.Most Common Word/Solution.cpp @@ -4,21 +4,18 @@ class Solution { unordered_set s(banned.begin(), banned.end()); unordered_map counter; string ans; - for (int i = 0, mx = 0, n = paragraph.size(); i < n;) - { + for (int i = 0, mx = 0, n = paragraph.size(); i < n;) { if (!isalpha(paragraph[i]) && (++i > 0)) continue; int j = i; string word; - while (j < n && isalpha(paragraph[j])) - { + while (j < n && isalpha(paragraph[j])) { word.push_back(tolower(paragraph[j])); ++j; } i = j + 1; if (s.count(word)) continue; ++counter[word]; - if (counter[word] > mx) - { + if (counter[word] > mx) { ans = word; mx = counter[word]; } diff --git a/solution/0800-0899/0820.Short Encoding of Words/Solution.cpp b/solution/0800-0899/0820.Short Encoding of Words/Solution.cpp index 71c6721b29261..e1a647698d5a9 100644 --- a/solution/0800-0899/0820.Short Encoding of Words/Solution.cpp +++ b/solution/0800-0899/0820.Short Encoding of Words/Solution.cpp @@ -1,5 +1,5 @@ struct Trie { - Trie* children[26] = { nullptr }; + Trie* children[26] = {nullptr}; }; class Solution { diff --git a/solution/0800-0899/0821.Shortest Distance to a Character/Solution.cpp b/solution/0800-0899/0821.Shortest Distance to a Character/Solution.cpp index b6538bd9a33f7..9f9efdce350bc 100644 --- a/solution/0800-0899/0821.Shortest Distance to a Character/Solution.cpp +++ b/solution/0800-0899/0821.Shortest Distance to a Character/Solution.cpp @@ -3,13 +3,11 @@ class Solution { vector shortestToChar(string s, char c) { int n = s.size(); vector ans(n); - for (int i = 0, j = INT_MAX; i < n; ++i) - { + for (int i = 0, j = INT_MAX; i < n; ++i) { if (s[i] == c) j = i; ans[i] = abs(i - j); } - for (int i = n - 1, j = INT_MAX; i >= 0; --i) - { + for (int i = n - 1, j = INT_MAX; i >= 0; --i) { if (s[i] == c) j = i; ans[i] = min(ans[i], abs(i - j)); } diff --git a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp index e17e8ad95be82..2391a481bb7ce 100644 --- a/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp +++ b/solution/0800-0899/0825.Friends Of Appropriate Ages/Solution.cpp @@ -4,14 +4,11 @@ class Solution { vector counter(121); for (int age : ages) ++counter[age]; int ans = 0; - for (int i = 1; i < 121; ++i) - { + for (int i = 1; i < 121; ++i) { int n1 = counter[i]; - for (int j = 1; j < 121; ++j) - { + for (int j = 1; j < 121; ++j) { int n2 = counter[j]; - if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) - { + if (!(j <= 0.5 * i + 7 || j > i || (j > 100 && i < 100))) { ans += n1 * n2; if (i == j) ans -= n2; } diff --git a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp index 1f239cc2532f4..53a38cb8ee18a 100644 --- a/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp +++ b/solution/0800-0899/0826.Most Profit Assigning Work/Solution.cpp @@ -1,20 +1,17 @@ class Solution { public: - int maxProfitAssignment(vector &difficulty, vector &profit, vector &worker) { + int maxProfitAssignment(vector& difficulty, vector& profit, vector& worker) { int n = difficulty.size(); vector> job; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { job.push_back({difficulty[i], profit[i]}); } sort(job.begin(), job.end()); sort(worker.begin(), worker.end()); int i = 0, t = 0; int res = 0; - for (auto w : worker) - { - while (i < n && job[i].first <= w) - { + for (auto w : worker) { + while (i < n && job[i].first <= w) { t = max(t, job[i++].second); } res += t; diff --git a/solution/0800-0899/0827.Making A Large Island/Solution.cpp b/solution/0800-0899/0827.Making A Large Island/Solution.cpp index c93d436ef5c7a..4e46fb2f92452 100644 --- a/solution/0800-0899/0827.Making A Large Island/Solution.cpp +++ b/solution/0800-0899/0827.Making A Large Island/Solution.cpp @@ -10,37 +10,27 @@ class Solution { mx = 1; p.resize(n * n); size.resize(n * n); - for (int i = 0; i < p.size(); ++i) - { + for (int i = 0; i < p.size(); ++i) { p[i] = i; size[i] = 1; } - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 1) - { - for (auto e : dirs) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 1) { + for (auto e : dirs) { if (check(i + e[0], j + e[1], grid)) unite(i * n + j, (i + e[0]) * n + j + e[1]); } } } } int res = mx; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 0) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 0) { int t = 1; unordered_set s; - for (auto e : dirs) - { - if (check(i + e[0], j + e[1], grid)) - { + for (auto e : dirs) { + if (check(i + e[0], j + e[1], grid)) { int root = find((i + e[0]) * n + j + e[1]); if (!s.count(root)) { t += size[root]; @@ -62,8 +52,7 @@ class Solution { void unite(int a, int b) { int pa = find(a), pb = find(b); - if (pa != pb) - { + if (pa != pb) { size[pb] += size[pa]; mx = max(mx, size[pb]); p[pa] = pb; diff --git a/solution/0800-0899/0836.Rectangle Overlap/Solution.cpp b/solution/0800-0899/0836.Rectangle Overlap/Solution.cpp index e5f4b0259521c..8d6922d72fd2c 100644 --- a/solution/0800-0899/0836.Rectangle Overlap/Solution.cpp +++ b/solution/0800-0899/0836.Rectangle Overlap/Solution.cpp @@ -1,14 +1,14 @@ class Solution { public: bool isRectangleOverlap(vector& rec1, vector& rec2) { - int l1 = rec1[1], u1 = rec1[0], r1 = rec1[3], d1 = rec1[2] ; - int l2 = rec2[1], u2 = rec2[0], r2 = rec2[3], d2 = rec2[2] ; - + int l1 = rec1[1], u1 = rec1[0], r1 = rec1[3], d1 = rec1[2]; + int l2 = rec2[1], u2 = rec2[0], r2 = rec2[3], d2 = rec2[2]; + //printf("1: (%d,%d), (%d,%d)\n", l1, u1, r1, d1) ; //printf("2: (%d,%d), (%d,%d)\n", l2, u2, r2, d2) ; - + if (l1 < r2 && u1 < d2 && l2 < r1 && u2 < d1) - return true ; - return false ; + return true; + return false; } }; \ No newline at end of file diff --git a/solution/0800-0899/0838.Push Dominoes/Solution.cpp b/solution/0800-0899/0838.Push Dominoes/Solution.cpp index 9c68e9bd63943..a0092fef991f8 100644 --- a/solution/0800-0899/0838.Push Dominoes/Solution.cpp +++ b/solution/0800-0899/0838.Push Dominoes/Solution.cpp @@ -3,10 +3,9 @@ class Solution { string pushDominoes(string dominoes) { int n = dominoes.size(); queue q; - vector time(n, - 1); + vector time(n, -1); vector force(n); - for (int i = 0; i < n; i++) - { + for (int i = 0; i < n; i++) { if (dominoes[i] == '.') continue; q.emplace(i); time[i] = 0; @@ -14,25 +13,21 @@ class Solution { } string ans(n, '.'); - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); - if (force[i].size() == 1) - { + if (force[i].size() == 1) { char f = force[i][0]; ans[i] = f; int j = (f == 'L') ? (i - 1) : (i + 1); - if (j >= 0 && j < n) - { + if (j >= 0 && j < n) { int t = time[i]; - if (time[j] == -1) - { + if (time[j] == -1) { q.emplace(j); time[j] = t + 1; force[j].push_back(f); - } - else if(time[j] == t + 1) force[j].push_back(f); + } else if (time[j] == t + 1) + force[j].push_back(f); } } } diff --git a/solution/0800-0899/0844.Backspace String Compare/Solution.cpp b/solution/0800-0899/0844.Backspace String Compare/Solution.cpp index e262b625bcb1a..a90503a21eef7 100644 --- a/solution/0800-0899/0844.Backspace String Compare/Solution.cpp +++ b/solution/0800-0899/0844.Backspace String Compare/Solution.cpp @@ -3,41 +3,31 @@ class Solution { bool backspaceCompare(string s, string t) { int i = s.size() - 1, j = t.size() - 1; int skip1 = 0, skip2 = 0; - for (; i >= 0 || j >= 0; --i, --j) - { - while (i >= 0) - { - if (s[i] == '#') - { + for (; i >= 0 || j >= 0; --i, --j) { + while (i >= 0) { + if (s[i] == '#') { ++skip1; --i; - } - else if (skip1) - { + } else if (skip1) { --skip1; --i; - } - else break; + } else + break; } - while (j >= 0) - { - if (t[j] == '#') - { + while (j >= 0) { + if (t[j] == '#') { ++skip2; --j; - } - else if (skip2) - { + } else if (skip2) { --skip2; --j; - } - else break; + } else + break; } - if (i >= 0 && j >= 0) - { + if (i >= 0 && j >= 0) { if (s[i] != t[j]) return false; - } - else if (i >= 0 || j >= 0) return false; + } else if (i >= 0 || j >= 0) + return false; } return true; } diff --git a/solution/0800-0899/0846.Hand of Straights/Solution.cpp b/solution/0800-0899/0846.Hand of Straights/Solution.cpp index 5b812a67c41a4..d36a24b98490c 100644 --- a/solution/0800-0899/0846.Hand of Straights/Solution.cpp +++ b/solution/0800-0899/0846.Hand of Straights/Solution.cpp @@ -4,14 +4,14 @@ class Solution { if (hand.size() % groupSize != 0) return false; map mp; for (int& h : hand) mp[h] += 1; - while (!mp.empty()) - { + while (!mp.empty()) { int v = mp.begin()->first; - for (int i = v; i < v + groupSize; ++i) - { + for (int i = v; i < v + groupSize; ++i) { if (!mp.count(i)) return false; - if (mp[i] == 1) mp.erase(i); - else mp[i] -= 1; + if (mp[i] == 1) + mp.erase(i); + else + mp[i] -= 1; } } return true; diff --git a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.cpp b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.cpp index eb913ad7c3c5f..458aaaace57a1 100644 --- a/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.cpp +++ b/solution/0800-0899/0847.Shortest Path Visiting All Nodes/Solution.cpp @@ -4,21 +4,17 @@ class Solution { int n = graph.size(); queue> q; vector> vis(n, vector(1 << n)); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { q.emplace(i, 1 << i, 0); vis[i][1 << i] = true; } - while (!q.empty()) - { + while (!q.empty()) { auto [u, state, dist] = q.front(); q.pop(); if (state == (1 << n) - 1) return dist; - for (int& v : graph[u]) - { + for (int& v : graph[u]) { int nxt = state | (1 << v); - if (!vis[v][nxt]) - { + if (!vis[v][nxt]) { q.emplace(v, nxt, dist + 1); vis[v][nxt] = true; } diff --git a/solution/0800-0899/0850.Rectangle Area II/Solution.cpp b/solution/0800-0899/0850.Rectangle Area II/Solution.cpp index 5b180f9972cfe..24e8fcf5ab1bd 100644 --- a/solution/0800-0899/0850.Rectangle Area II/Solution.cpp +++ b/solution/0800-0899/0850.Rectangle Area II/Solution.cpp @@ -20,8 +20,7 @@ class SegmentTree { void build(int u, int l, int r) { tr[u]->l = l; tr[u]->r = r; - if (l != r) - { + if (l != r) { int mid = (l + r) >> 1; build(u << 1, l, mid); build(u << 1 | 1, mid + 1, r); @@ -29,9 +28,9 @@ class SegmentTree { } void modify(int u, int l, int r, int k) { - if (tr[u]->l >= l && tr[u]->r <= r) tr[u]->cnt += k; - else - { + if (tr[u]->l >= l && tr[u]->r <= r) + tr[u]->cnt += k; + else { int mid = (tr[u]->l + tr[u]->r) >> 1; if (l <= mid) modify(u << 1, l, r, k); if (r > mid) modify(u << 1 | 1, l, r, k); @@ -44,9 +43,12 @@ class SegmentTree { } void pushup(int u) { - if (tr[u]->cnt) tr[u]->len = nums[tr[u]->r + 1] - nums[tr[u]->l]; - else if (tr[u]->l == tr[u]->r) tr[u]->len = 0; - else tr[u]->len = tr[u << 1]->len + tr[u << 1 | 1]->len; + if (tr[u]->cnt) + tr[u]->len = nums[tr[u]->r + 1] - nums[tr[u]->l]; + else if (tr[u]->l == tr[u]->r) + tr[u]->len = 0; + else + tr[u]->len = tr[u << 1]->len + tr[u << 1 | 1]->len; } }; @@ -57,8 +59,7 @@ class Solution { vector> segs; set ts; int mod = 1e9 + 7; - for (auto& rect : rectangles) - { + for (auto& rect : rectangles) { int x1 = rect[0], y1 = rect[1], x2 = rect[2], y2 = rect[3]; segs.push_back({x1, y1, y2, 1}); segs.push_back({x2, y1, y2, -1}); @@ -72,13 +73,12 @@ class Solution { vector nums(ts.begin(), ts.end()); SegmentTree* tree = new SegmentTree(nums); long long ans = 0; - for (int i = 0; i < segs.size(); ++i) - { + for (int i = 0; i < segs.size(); ++i) { int x = segs[i][0], y1 = segs[i][1], y2 = segs[i][2], k = segs[i][3]; - if (i > 0) ans += (long long) tree->query() * (x - segs[i - 1][0]); + if (i > 0) ans += (long long)tree->query() * (x - segs[i - 1][0]); tree->modify(1, m[y1], m[y2] - 1, k); } ans %= mod; - return (int) ans; + return (int)ans; } }; \ No newline at end of file diff --git a/solution/0800-0899/0851.Loud and Rich/Solution.cpp b/solution/0800-0899/0851.Loud and Rich/Solution.cpp index b8759f8fcddae..074f52c0dd588 100644 --- a/solution/0800-0899/0851.Loud and Rich/Solution.cpp +++ b/solution/0800-0899/0851.Loud and Rich/Solution.cpp @@ -8,8 +8,7 @@ class Solution { function dfs = [&](int i) { if (ans[i] != -1) return; ans[i] = i; - for (int j : g[i]) - { + for (int j : g[i]) { dfs(j); if (quiet[ans[j]] < quiet[ans[i]]) ans[i] = ans[j]; } diff --git a/solution/0800-0899/0852.Peak Index in a Mountain Array/Solution.cpp b/solution/0800-0899/0852.Peak Index in a Mountain Array/Solution.cpp index 262f84dfc9675..b48e04663f8df 100644 --- a/solution/0800-0899/0852.Peak Index in a Mountain Array/Solution.cpp +++ b/solution/0800-0899/0852.Peak Index in a Mountain Array/Solution.cpp @@ -2,11 +2,12 @@ class Solution { public: int peakIndexInMountainArray(vector& arr) { int left = 1, right = arr.size() - 2; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (arr[mid] > arr[mid + 1]) right = mid; - else left = mid + 1; + if (arr[mid] > arr[mid + 1]) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0800-0899/0854.K-Similar Strings/Solution.cpp b/solution/0800-0899/0854.K-Similar Strings/Solution.cpp index 2f46d96c96d17..9e34ccf3f7179 100644 --- a/solution/0800-0899/0854.K-Similar Strings/Solution.cpp +++ b/solution/0800-0899/0854.K-Similar Strings/Solution.cpp @@ -1,20 +1,16 @@ class Solution { public: int kSimilarity(string s1, string s2) { - queue q{{s1}}; - unordered_set vis{{s1}}; + queue q {{s1}}; + unordered_set vis {{s1}}; int ans = 0; - while (!q.empty()) - { - for (int i = q.size(); i; --i) - { + while (!q.empty()) { + for (int i = q.size(); i; --i) { s1 = q.front(); q.pop(); if (s1 == s2) return ans; - for (string nxt : next(s1, s2)) - { - if (!vis.count(nxt)) - { + for (string nxt : next(s1, s2)) { + if (!vis.count(nxt)) { vis.insert(nxt); q.push(nxt); } @@ -27,12 +23,11 @@ class Solution { vector next(string& s, string& s2) { int i = 0, n = s.size(); - for (; i < n && s[i] == s2[i]; ++i); + for (; i < n && s[i] == s2[i]; ++i) + ; vector res; - for (int j = i + 1; j < n; ++j) - { - if (s[j] == s2[i] && s[j] != s2[j]) - { + for (int j = i + 1; j < n; ++j) { + if (s[j] == s2[i] && s[j] != s2[j]) { swap(s[i], s[j]); res.push_back(s); swap(s[i], s[j]); diff --git a/solution/0800-0899/0859.Buddy Strings/Solution.cpp b/solution/0800-0899/0859.Buddy Strings/Solution.cpp index ae76cc7a6328e..986a12a27a8bf 100644 --- a/solution/0800-0899/0859.Buddy Strings/Solution.cpp +++ b/solution/0800-0899/0859.Buddy Strings/Solution.cpp @@ -6,15 +6,13 @@ class Solution { int diff = 0; vector cnt1(26); vector cnt2(26); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { ++cnt1[s[i] - 'a']; ++cnt2[goal[i] - 'a']; if (s[i] != goal[i]) ++diff; } bool f = false; - for (int i = 0; i < 26; ++i) - { + for (int i = 0; i < 26; ++i) { if (cnt1[i] != cnt2[i]) return false; if (cnt1[i] > 1) f = true; } diff --git a/solution/0800-0899/0860.Lemonade Change/Solution.cpp b/solution/0800-0899/0860.Lemonade Change/Solution.cpp index c670fcf364b5d..a59435026d8f2 100644 --- a/solution/0800-0899/0860.Lemonade Change/Solution.cpp +++ b/solution/0800-0899/0860.Lemonade Change/Solution.cpp @@ -2,18 +2,17 @@ class Solution { public: bool lemonadeChange(vector& bills) { int five = 0, ten = 0; - for (int v : bills) - { - if (v == 5) ++five; - else if (v == 10) - { + for (int v : bills) { + if (v == 5) + ++five; + else if (v == 10) { ++ten; --five; - } - else - { - if (ten) --ten, --five; - else five -= 3; + } else { + if (ten) + --ten, --five; + else + five -= 3; } if (five < 0) return false; } diff --git a/solution/0800-0899/0861.Score After Flipping Matrix/Solution.cpp b/solution/0800-0899/0861.Score After Flipping Matrix/Solution.cpp index c2b03d47e6d59..bcd4435e6701a 100644 --- a/solution/0800-0899/0861.Score After Flipping Matrix/Solution.cpp +++ b/solution/0800-0899/0861.Score After Flipping Matrix/Solution.cpp @@ -2,16 +2,13 @@ class Solution { public: int matrixScore(vector>& grid) { int m = grid.size(), n = grid[0].size(); - for (int i = 0; i < m; ++i) - { - if (grid[i][0] == 0) - { + for (int i = 0; i < m; ++i) { + if (grid[i][0] == 0) { for (int j = 0; j < n; ++j) grid[i][j] ^= 1; } } int res = 0; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { int cnt = 0; for (int i = 0; i < m; ++i) cnt += grid[i][j]; res += max(cnt, m - cnt) * (1 << (n - j - 1)); diff --git a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.cpp b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.cpp index 03c130d5cc835..afca5121e2ae3 100644 --- a/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.cpp +++ b/solution/0800-0899/0862.Shortest Subarray with Sum at Least K/Solution.cpp @@ -4,12 +4,10 @@ class Solution { int n = nums.size(); vector s(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; - deque q{{0}}; + deque q {{0}}; int ans = INT_MAX; - for (int i = 1; i <= n; ++i) - { - while (!q.empty() && s[i] - s[q.front()] >= k) - { + for (int i = 1; i <= n; ++i) { + while (!q.empty() && s[i] - s[q.front()] >= k) { ans = min(ans, i - q.front()); q.pop_front(); } diff --git a/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/Solution.cpp b/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/Solution.cpp index f0fc468b24fa8..fce0a0e63cbaf 100644 --- a/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/Solution.cpp +++ b/solution/0800-0899/0863.All Nodes Distance K in Binary Tree/Solution.cpp @@ -29,8 +29,7 @@ class Solution { void dfs(TreeNode* root, int k) { if (!root || vis.count(root->val)) return; vis.insert(root->val); - if (k == 0) - { + if (k == 0) { ans.push_back(root->val); return; } diff --git a/solution/0800-0899/0864.Shortest Path to Get All Keys/Solution.cpp b/solution/0800-0899/0864.Shortest Path to Get All Keys/Solution.cpp index 506fe01a14974..c8cdb526bb17c 100644 --- a/solution/0800-0899/0864.Shortest Path to Get All Keys/Solution.cpp +++ b/solution/0800-0899/0864.Shortest Path to Get All Keys/Solution.cpp @@ -4,14 +4,12 @@ class Solution { int m = grid.size(), n = grid[0].size(); int cnt = 0; int sx = 0, sy = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { char c = grid[i][j]; - if (islower(c)) ++cnt; - else if (c == '@') - { + if (islower(c)) + ++cnt; + else if (c == '@') { sx = i; sy = j; } @@ -24,25 +22,20 @@ class Solution { vis[sx][sy][0] = true; int ans = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { auto p = q.front(); q.pop(); int i = p[0], j = p[1], state = p[2]; if (state == mask) return ans; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int nxt = state; int x = i + dirs[k], y = j + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { + if (x >= 0 && x < m && y >= 0 && y < n) { char c = grid[x][y]; if (c == '#' || (isupper(c) && (nxt & (1 << (c - 'A'))) == 0)) continue; if (islower(c)) nxt |= 1 << (c - 'a'); - if (!vis[x][y][nxt]) - { + if (!vis[x][y][nxt]) { vis[x][y][nxt] = true; q.push({x, y, nxt}); } diff --git a/solution/0800-0899/0866.Prime Palindrome/Solution.cpp b/solution/0800-0899/0866.Prime Palindrome/Solution.cpp index a18ea12a1e261..ad6de1fc6c71c 100644 --- a/solution/0800-0899/0866.Prime Palindrome/Solution.cpp +++ b/solution/0800-0899/0866.Prime Palindrome/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: int primePalindrome(int n) { - while (1) - { + while (1) { if (reverse(n) == n && isPrime(n)) return n; if (n > 10000000 && n < 100000000) n = 100000000; ++n; @@ -19,8 +18,7 @@ class Solution { int reverse(int x) { int res = 0; - while (x) - { + while (x) { res = res * 10 + x % 10; x /= 10; } diff --git a/solution/0800-0899/0868.Binary Gap/Solution.cpp b/solution/0800-0899/0868.Binary Gap/Solution.cpp index 9c762ef4fb367..a360fde6cdeae 100644 --- a/solution/0800-0899/0868.Binary Gap/Solution.cpp +++ b/solution/0800-0899/0868.Binary Gap/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: int binaryGap(int n) { int ans = 0; - for (int i = 0, j = -1; n; ++i, n >>= 1) - { - if (n & 1) - { + for (int i = 0, j = -1; n; ++i, n >>= 1) { + if (n & 1) { if (j != -1) ans = max(ans, i - j); j = i; } diff --git a/solution/0800-0899/0870.Advantage Shuffle/Solution.cpp b/solution/0800-0899/0870.Advantage Shuffle/Solution.cpp index b455b317a2d1b..13747abf1c67d 100644 --- a/solution/0800-0899/0870.Advantage Shuffle/Solution.cpp +++ b/solution/0800-0899/0870.Advantage Shuffle/Solution.cpp @@ -8,10 +8,11 @@ class Solution { sort(nums1.begin(), nums1.end()); int i = 0, j = n - 1; vector ans(n); - for (int v : nums1) - { - if (v <= t[i].first) ans[t[j--].second] = v; - else ans[t[i++].second] = v; + for (int v : nums1) { + if (v <= t[i].first) + ans[t[j--].second] = v; + else + ans[t[i++].second] = v; } return ans; } diff --git a/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.cpp b/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.cpp index bd5049c13983c..59e83b3f71e4d 100644 --- a/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.cpp +++ b/solution/0800-0899/0871.Minimum Number of Refueling Stops/Solution.cpp @@ -4,12 +4,10 @@ class Solution { priority_queue q; stations.push_back({target, 0}); int ans = 0, prev = 0; - for (auto& s : stations) - { + for (auto& s : stations) { int d = s[0] - prev; startFuel -= d; - while (startFuel < 0 && !q.empty()) - { + while (startFuel < 0 && !q.empty()) { startFuel += q.top(); q.pop(); ++ans; diff --git a/solution/0800-0899/0873.Length of Longest Fibonacci Subsequence/Solution.cpp b/solution/0800-0899/0873.Length of Longest Fibonacci Subsequence/Solution.cpp index c7ba7d930f06b..116435eda8f19 100644 --- a/solution/0800-0899/0873.Length of Longest Fibonacci Subsequence/Solution.cpp +++ b/solution/0800-0899/0873.Length of Longest Fibonacci Subsequence/Solution.cpp @@ -9,16 +9,12 @@ class Solution { for (int j = 0; j < i; ++j) dp[j][i] = 2; int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < i; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { int d = arr[i] - arr[j]; - if (mp.count(d)) - { + if (mp.count(d)) { int k = mp[d]; - if (k < j) - { + if (k < j) { dp[j][i] = max(dp[j][i], dp[k][j] + 1); ans = max(ans, dp[j][i]); } diff --git a/solution/0800-0899/0874.Walking Robot Simulation/Solution.cpp b/solution/0800-0899/0874.Walking Robot Simulation/Solution.cpp index 13fb08a7ad95f..a92e06e8d4105 100644 --- a/solution/0800-0899/0874.Walking Robot Simulation/Solution.cpp +++ b/solution/0800-0899/0874.Walking Robot Simulation/Solution.cpp @@ -6,14 +6,13 @@ for (auto v : obstacles) s.insert(to_string(v[0]) + "." + to_string(v[1])); int ans = 0, p = 1; int x = 0, y = 0; - for (int v : commands) - { - if (v == -2) p = (p + 3) % 4; - else if (v == -1) p = (p + 1) % 4; - else - { - while (v--) - { + for (int v : commands) { + if (v == -2) + p = (p + 3) % 4; + else if (v == -1) + p = (p + 1) % 4; + else { + while (v--) { int nx = x + dirs[p][0], ny = y + dirs[p][1]; if (s.count(to_string(nx) + "." + to_string(ny))) break; x = nx; diff --git a/solution/0800-0899/0875.Koko Eating Bananas/Solution.cpp b/solution/0800-0899/0875.Koko Eating Bananas/Solution.cpp index 59f299178ecc0..6de7eb00ee07c 100644 --- a/solution/0800-0899/0875.Koko Eating Bananas/Solution.cpp +++ b/solution/0800-0899/0875.Koko Eating Bananas/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: int minEatingSpeed(vector& piles, int h) { int left = 1, right = 1e9; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int s = 0; - for (int& x : piles) s += (x + mid - 1) / mid; - if (s <= h) right = mid; - else left = mid + 1; + for (int& x : piles) s += (x + mid - 1) / mid; + if (s <= h) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.cpp b/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.cpp index e012c8c838cf7..00e832b84f695 100644 --- a/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.cpp +++ b/solution/0800-0899/0883.Projection Area of 3D Shapes/Solution.cpp @@ -2,11 +2,9 @@ class Solution { public: int projectionArea(vector>& grid) { int xy = 0, yz = 0, zx = 0; - for (int i = 0, n = grid.size(); i < n; ++i) - { + for (int i = 0, n = grid.size(); i < n; ++i) { int maxYz = 0, maxZx = 0; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { xy += grid[i][j] > 0; maxYz = max(maxYz, grid[i][j]); maxZx = max(maxZx, grid[j][i]); diff --git a/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.cpp b/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.cpp index 98cac41d81975..463d7cefce843 100644 --- a/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.cpp +++ b/solution/0800-0899/0884.Uncommon Words from Two Sentences/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: vector uncommonFromSentences(string s1, string s2) { unordered_map counter; - + auto add = [&](const string& s) { stringstream ss(s); string word; diff --git a/solution/0800-0899/0885.Spiral Matrix III/Solution.cpp b/solution/0800-0899/0885.Spiral Matrix III/Solution.cpp index 90f98cf5ec723..7b04c2c0585b0 100644 --- a/solution/0800-0899/0885.Spiral Matrix III/Solution.cpp +++ b/solution/0800-0899/0885.Spiral Matrix III/Solution.cpp @@ -5,18 +5,14 @@ class Solution { vector> ans; ans.push_back({rStart, cStart}); if (cnt == 1) return ans; - for (int k = 1;; k += 2) - { + for (int k = 1;; k += 2) { vector> dirs = {{0, 1, k}, {1, 0, k}, {0, -1, k + 1}, {-1, 0, k + 1}}; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int r = dir[0], c = dir[1], dk = dir[2]; - while (dk-- > 0) - { + while (dk-- > 0) { rStart += r; cStart += c; - if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) - { + if (rStart >= 0 && rStart < rows && cStart >= 0 && cStart < cols) { ans.push_back({rStart, cStart}); if (ans.size() == cnt) return ans; } diff --git a/solution/0800-0899/0886.Possible Bipartition/Solution.cpp b/solution/0800-0899/0886.Possible Bipartition/Solution.cpp index 636f03d812bab..2bb6d9d44ed84 100644 --- a/solution/0800-0899/0886.Possible Bipartition/Solution.cpp +++ b/solution/0800-0899/0886.Possible Bipartition/Solution.cpp @@ -6,16 +6,13 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; unordered_map> dis; - for (auto& d : dislikes) - { + for (auto& d : dislikes) { int a = d[0] - 1, b = d[1] - 1; dis[a].push_back(b); dis[b].push_back(a); } - for (int i = 0; i < n; ++i) - { - for (int j : dis[i]) - { + for (int i = 0; i < n; ++i) { + for (int j : dis[i]) { if (find(i) == find(j)) return false; p[find(j)] = find(dis[i][0]); } diff --git a/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp b/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp index 082af60dfc6cb..e15ead9c97dd8 100644 --- a/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp +++ b/solution/0800-0899/0888.Fair Candy Swap/Solution.cpp @@ -9,7 +9,7 @@ class Solution { for (int& a : aliceSizes) { int target = a - diff; if (s.count(target)) { - ans = vector{a, target}; + ans = vector {a, target}; break; } } diff --git a/solution/0800-0899/0890.Find and Replace Pattern/Solution.cpp b/solution/0800-0899/0890.Find and Replace Pattern/Solution.cpp index cf37b5bc9bf79..f5061818f4f1b 100644 --- a/solution/0800-0899/0890.Find and Replace Pattern/Solution.cpp +++ b/solution/0800-0899/0890.Find and Replace Pattern/Solution.cpp @@ -11,8 +11,7 @@ class Solution { bool match(string s, string t) { vector m1(128); vector m2(128); - for (int i = 0; i < s.size(); ++i) - { + for (int i = 0; i < s.size(); ++i) { if (m1[s[i]] != m2[t[i]]) return 0; m1[s[i]] = i + 1; m2[t[i]] = i + 1; diff --git a/solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.cpp b/solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.cpp index ffd36ad6f927d..661c0fa6a351d 100644 --- a/solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.cpp +++ b/solution/0800-0899/0893.Groups of Special-Equivalent Strings/Solution.cpp @@ -2,13 +2,13 @@ class Solution { public: int numSpecialEquivGroups(vector& words) { unordered_set s; - for (auto& word : words) - { + for (auto& word : words) { string a = "", b = ""; - for (int i = 0; i < word.size(); ++i) - { - if (i & 1) a += word[i]; - else b += word[i]; + for (int i = 0; i < word.size(); ++i) { + if (i & 1) + a += word[i]; + else + b += word[i]; } sort(a.begin(), a.end()); sort(b.begin(), b.end()); diff --git a/solution/0800-0899/0896.Monotonic Array/Solution.cpp b/solution/0800-0899/0896.Monotonic Array/Solution.cpp index aeb45985197b5..a95d063c1a935 100644 --- a/solution/0800-0899/0896.Monotonic Array/Solution.cpp +++ b/solution/0800-0899/0896.Monotonic Array/Solution.cpp @@ -3,8 +3,7 @@ class Solution { bool isMonotonic(vector& nums) { bool isIncr = false; bool isDecr = false; - for (int i = 1; i < nums.size(); ++i) - { + for (int i = 1; i < nums.size(); ++i) { if (nums[i] < nums[i - 1]) isIncr = true; if (nums[i] > nums[i - 1]) isDecr = true; if (isIncr && isDecr) return false; diff --git a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp index e4b8445947ba1..597fe0531bfc7 100644 --- a/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp +++ b/solution/0800-0899/0898.Bitwise ORs of Subarrays/Solution.cpp @@ -3,12 +3,10 @@ class Solution { int subarrayBitwiseORs(vector& arr) { unordered_set s; int prev = 0; - for (int i = 0; i < arr.size(); ++i) - { + for (int i = 0; i < arr.size(); ++i) { prev |= arr[i]; int curr = 0; - for (int j = i; ~j; --j) - { + for (int j = i; ~j; --j) { curr |= arr[j]; s.insert(curr); if (curr == prev) break; diff --git a/solution/0800-0899/0899.Orderly Queue/Solution.cpp b/solution/0800-0899/0899.Orderly Queue/Solution.cpp index 354dcc6eb7bbb..c063513aa7191 100644 --- a/solution/0800-0899/0899.Orderly Queue/Solution.cpp +++ b/solution/0800-0899/0899.Orderly Queue/Solution.cpp @@ -3,8 +3,7 @@ class Solution { string orderlyQueue(string s, int k) { if (k == 1) { string ans = s; - for (int i = 0; i < s.size() - 1; ++i) - { + for (int i = 0; i < s.size() - 1; ++i) { s = s.substr(1) + s[0]; if (s < ans) ans = s; } diff --git a/solution/0900-0999/0900.RLE Iterator/Solution.cpp b/solution/0900-0999/0900.RLE Iterator/Solution.cpp index b2ffd1b25a606..702f94303f459 100644 --- a/solution/0900-0999/0900.RLE Iterator/Solution.cpp +++ b/solution/0900-0999/0900.RLE Iterator/Solution.cpp @@ -9,18 +9,14 @@ class RLEIterator { this->curr = 0; this->i = 0; } - + int next(int n) { - while (i < encoding.size()) - { - if (curr + n > encoding[i]) - { + while (i < encoding.size()) { + if (curr + n > encoding[i]) { n -= encoding[i] - curr; curr = 0; i += 2; - } - else - { + } else { curr += n; return encoding[i + 1]; } diff --git a/solution/0900-0999/0901.Online Stock Span/Solution.cpp b/solution/0900-0999/0901.Online Stock Span/Solution.cpp index 7bda80373d2f5..07c6fedc3a0d9 100644 --- a/solution/0900-0999/0901.Online Stock Span/Solution.cpp +++ b/solution/0900-0999/0901.Online Stock Span/Solution.cpp @@ -7,8 +7,7 @@ class StockSpanner { int next(int price) { int res = 1; - while (!stk.empty() && stk.top().first <= price) - { + while (!stk.empty() && stk.top().first <= price) { pair t = stk.top(); stk.pop(); res += t.second; diff --git a/solution/0900-0999/0905.Sort Array By Parity/Solution.cpp b/solution/0900-0999/0905.Sort Array By Parity/Solution.cpp index 17baf1d77ec2e..4bc09afec3b3c 100644 --- a/solution/0900-0999/0905.Sort Array By Parity/Solution.cpp +++ b/solution/0900-0999/0905.Sort Array By Parity/Solution.cpp @@ -1,10 +1,11 @@ class Solution { public: vector sortArrayByParity(vector& nums) { - for (int i = 0, j = nums.size() - 1; i < j;) - { - if (nums[i] & 1) swap(nums[i], nums[j--]); - else ++i; + for (int i = 0, j = nums.size() - 1; i < j;) { + if (nums[i] & 1) + swap(nums[i], nums[j--]); + else + ++i; } return nums; } diff --git a/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.cpp b/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.cpp index 833e106dab218..ac78b40a835fb 100644 --- a/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.cpp +++ b/solution/0900-0999/0907.Sum of Subarray Minimums/Solution.cpp @@ -8,23 +8,20 @@ class Solution { vector left(n, -1); vector right(n, n); stack stk; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { while (!stk.empty() && arr[stk.top()] >= arr[i]) stk.pop(); if (!stk.empty()) left[i] = stk.top(); stk.push(i); } stk = stack(); - for (int i = n - 1; i >= 0; --i) - { + for (int i = n - 1; i >= 0; --i) { while (!stk.empty() && arr[stk.top()] > arr[i]) stk.pop(); if (!stk.empty()) right[i] = stk.top(); stk.push(i); } ll ans = 0; - for (int i = 0; i < n; ++i) - { - ans += (ll) (i - left[i]) * (right[i] - i) * arr[i] % mod; + for (int i = 0; i < n; ++i) { + ans += (ll)(i - left[i]) * (right[i] - i) * arr[i] % mod; ans %= mod; } return ans; diff --git a/solution/0900-0999/0908.Smallest Range I/Solution.cpp b/solution/0900-0999/0908.Smallest Range I/Solution.cpp index 3995d80a8e023..ac139a53b730b 100644 --- a/solution/0900-0999/0908.Smallest Range I/Solution.cpp +++ b/solution/0900-0999/0908.Smallest Range I/Solution.cpp @@ -3,6 +3,6 @@ class Solution { int smallestRangeI(vector& nums, int k) { int mx = *max_element(nums.begin(), nums.end()); int mi = *min_element(nums.begin(), nums.end()); - return max(0, mx - mi - k * 2); + return max(0, mx - mi - k * 2); } }; \ No newline at end of file diff --git a/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp b/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp index 6bcd55b56ea8a..b97c78435f552 100644 --- a/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp +++ b/solution/0900-0999/0909.Snakes and Ladders/Solution.cpp @@ -4,25 +4,21 @@ class Solution { int snakesAndLadders(vector>& board) { n = board.size(); - queue q{{1}}; + queue q {{1}}; vector vis(n * n + 1); vis[1] = true; int ans = 0; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { int curr = q.front(); if (curr == n * n) return ans; q.pop(); - for (int k = curr + 1; k <= min(curr + 6, n * n); ++k) - { + for (int k = curr + 1; k <= min(curr + 6, n * n); ++k) { auto p = get(k); int next = k; int i = p[0], j = p[1]; if (board[i][j] != -1) next = board[i][j]; - if (!vis[next]) - { + if (!vis[next]) { vis[next] = true; q.push(next); } diff --git a/solution/0900-0999/0911.Online Election/Solution.cpp b/solution/0900-0999/0911.Online Election/Solution.cpp index 9ab73809271e9..c107a0550c659 100644 --- a/solution/0900-0999/0911.Online Election/Solution.cpp +++ b/solution/0900-0999/0911.Online Election/Solution.cpp @@ -9,25 +9,24 @@ class TopVotedCandidate { int mx = 0, cur = 0; this->times = times; vector counter(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int p = persons[i]; - if (++counter[p] >= mx) - { + if (++counter[p] >= mx) { mx = counter[p]; cur = p; } wins[i] = cur; } } - + int q(int t) { int left = 0, right = wins.size() - 1; - while (left < right) - { + while (left < right) { int mid = left + right + 1 >> 1; - if (times[mid] <= t) left = mid; - else right = mid - 1; + if (times[mid] <= t) + left = mid; + else + right = mid - 1; } return wins[left]; } diff --git a/solution/0900-0999/0912.Sort an Array/Solution.cpp b/solution/0900-0999/0912.Sort an Array/Solution.cpp index e4ad4292fdab6..94119c5c71340 100644 --- a/solution/0900-0999/0912.Sort an Array/Solution.cpp +++ b/solution/0900-0999/0912.Sort an Array/Solution.cpp @@ -9,10 +9,11 @@ class Solution { if (left >= right) return; int i = left - 1, j = right + 1; int x = nums[left + right >> 1]; - while (i < j) - { - while (nums[++i] < x); - while (nums[--j] > x); + while (i < j) { + while (nums[++i] < x) + ; + while (nums[--j] > x) + ; if (i < j) swap(nums[i], nums[j]); } quick_sort(nums, left, j); diff --git a/solution/0900-0999/0913.Cat and Mouse/Solution.cpp b/solution/0900-0999/0913.Cat and Mouse/Solution.cpp index cc9d911f56416..7a20d5e1fa213 100644 --- a/solution/0900-0999/0913.Cat and Mouse/Solution.cpp +++ b/solution/0900-0999/0913.Cat and Mouse/Solution.cpp @@ -12,18 +12,18 @@ class Solution { int dfs(int i, int j, int k) { if (memo[i][j][k] != -1) return memo[i][j][k]; - if (k >= 2 * graph.size()) memo[i][j][k] = 0; - else if (i == 0) memo[i][j][k] = 1; - else if (i == j) memo[i][j][k] = 2; - else if (k % 2) - { + if (k >= 2 * graph.size()) + memo[i][j][k] = 0; + else if (i == 0) + memo[i][j][k] = 1; + else if (i == j) + memo[i][j][k] = 2; + else if (k % 2) { bool tie = false, win = false; - for (int next : graph[j]) - { + for (int next : graph[j]) { if (next == 0) continue; int x = dfs(i, next, k + 1); - if (x == 2) - { + if (x == 2) { win = true; memo[i][j][k] = 2; break; @@ -31,15 +31,11 @@ class Solution { if (x == 0) tie = true; } if (!win) memo[i][j][k] = tie ? 0 : 1; - } - else - { + } else { bool tie = false, win = false; - for (int next : graph[i]) - { + for (int next : graph[i]) { int x = dfs(next, j, k + 1); - if (x == 1) - { + if (x == 1) { win = true; memo[i][j][k] = 1; break; diff --git a/solution/0900-0999/0917.Reverse Only Letters/Solution.cpp b/solution/0900-0999/0917.Reverse Only Letters/Solution.cpp index 2798feb35fe54..82e826061b22f 100644 --- a/solution/0900-0999/0917.Reverse Only Letters/Solution.cpp +++ b/solution/0900-0999/0917.Reverse Only Letters/Solution.cpp @@ -2,12 +2,10 @@ class Solution { public: string reverseOnlyLetters(string s) { int i = 0, j = s.size() - 1; - while (i < j) - { + while (i < j) { while (i < j && !isalpha(s[i])) ++i; while (i < j && !isalpha(s[j])) --j; - if (i < j) - { + if (i < j) { swap(s[i], s[j]); ++i; --j; diff --git a/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.cpp b/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.cpp index 19776a18eb6a7..d6e25ec1c7d42 100644 --- a/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.cpp +++ b/solution/0900-0999/0919.Complete Binary Tree Inserter/Solution.cpp @@ -14,9 +14,8 @@ class CBTInserter { vector tree; CBTInserter(TreeNode* root) { - queue q{{root}}; - while (!q.empty()) - { + queue q {{root}}; + while (!q.empty()) { auto node = q.front(); q.pop(); tree.push_back(node); @@ -24,17 +23,19 @@ class CBTInserter { if (node->right) q.push(node->right); } } - + int insert(int val) { int pid = tree.size() - 1 >> 1; TreeNode* node = new TreeNode(val); tree.push_back(node); TreeNode* p = tree[pid]; - if (!p->left) p->left = node; - else p->right = node; + if (!p->left) + p->left = node; + else + p->right = node; return p->val; } - + TreeNode* get_root() { return tree[0]; } diff --git a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.cpp b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.cpp index dbcbdfe64cb1e..d65a95e16a3ad 100644 --- a/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.cpp +++ b/solution/0900-0999/0921.Minimum Add to Make Parentheses Valid/Solution.cpp @@ -2,15 +2,14 @@ class Solution { public: int minAddToMakeValid(string s) { stack stk; - for (char& c: s) - { - if (c == '(') stk.push(c); - else - { + for (char& c : s) { + if (c == '(') + stk.push(c); + else { if (!stk.empty() && stk.top() == '(') { stk.pop(); - } - else stk.push(c); + } else + stk.push(c); } } return stk.size(); diff --git a/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp b/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp index 72429ebb5f721..bbb85caafee9d 100644 --- a/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp +++ b/solution/0900-0999/0922.Sort Array By Parity II/Solution.cpp @@ -1,12 +1,9 @@ class Solution { public: - vector sortArrayByParityII(vector &nums) { - for (int i = 0, j = 1; i < nums.size(); i += 2) - { - if ((nums[i] & 1) == 1) - { - while ((nums[j] & 1) == 1) - { + vector sortArrayByParityII(vector& nums) { + for (int i = 0, j = 1; i < nums.size(); i += 2) { + if ((nums[i] & 1) == 1) { + while ((nums[j] & 1) == 1) { j += 2; } swap(nums[i], nums[j]); diff --git a/solution/0900-0999/0924.Minimize Malware Spread/Solution.cpp b/solution/0900-0999/0924.Minimize Malware Spread/Solution.cpp index 0a897f69b6e96..6da9b93e061d7 100644 --- a/solution/0900-0999/0924.Minimize Malware Spread/Solution.cpp +++ b/solution/0900-0999/0924.Minimize Malware Spread/Solution.cpp @@ -7,12 +7,9 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; vector size(n, 1); - for (int i = 0; i < n; ++i) - { - for (int j = i + 1; j < n; ++j) - { - if (graph[i][j]) - { + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + if (graph[i][j]) { int pa = find(i), pb = find(j); if (pa == pb) continue; p[pa] = pb; @@ -23,19 +20,16 @@ class Solution { int mi = 400; int res = initial[0]; sort(initial.begin(), initial.end()); - for (int i = 0; i < initial.size(); ++i) - { + for (int i = 0; i < initial.size(); ++i) { int t = 0; unordered_set s; - for (int j = 0; j < initial.size(); ++j) - { + for (int j = 0; j < initial.size(); ++j) { if (i == j) continue; if (s.count(find(initial[j]))) continue; s.insert(find(initial[j])); t += size[find(initial[j])]; } - if (mi > t) - { + if (mi > t) { mi = t; res = initial[i]; } diff --git a/solution/0900-0999/0925.Long Pressed Name/Solution.cpp b/solution/0900-0999/0925.Long Pressed Name/Solution.cpp index 78f11fe1ca018..2ad6af2ef0fd5 100644 --- a/solution/0900-0999/0925.Long Pressed Name/Solution.cpp +++ b/solution/0900-0999/0925.Long Pressed Name/Solution.cpp @@ -3,18 +3,15 @@ class Solution { bool isLongPressedName(string name, string typed) { int m = name.size(), n = typed.size(); int i = 0, j = 0; - for (; i < m && j < n; ++i, ++j) - { + for (; i < m && j < n; ++i, ++j) { if (name[i] != typed[j]) return false; int cnt1 = 0, cnt2 = 0; char c = name[i]; - while (i + 1 < m && name[i + 1] == c) - { + while (i + 1 < m && name[i + 1] == c) { ++i; ++cnt1; } - while (j + 1 < n && typed[j + 1] == c) - { + while (j + 1 < n && typed[j + 1] == c) { ++j; ++cnt2; } diff --git a/solution/0900-0999/0927.Three Equal Parts/Solution.cpp b/solution/0900-0999/0927.Three Equal Parts/Solution.cpp index 94e719ec17649..ea00a074018b0 100644 --- a/solution/0900-0999/0927.Three Equal Parts/Solution.cpp +++ b/solution/0900-0999/0927.Three Equal Parts/Solution.cpp @@ -10,8 +10,7 @@ class Solution { int i = find(arr, 1); int j = find(arr, cnt + 1); int k = find(arr, cnt * 2 + 1); - while (k < n && arr[i] == arr[j] && arr[j] == arr[k]) - { + while (k < n && arr[i] == arr[j] && arr[j] == arr[k]) { ++i; ++j; ++k; @@ -22,8 +21,7 @@ class Solution { int find(vector& arr, int cnt) { int s = 0; - for (int i = 0; i < arr.size(); ++i) - { + for (int i = 0; i < arr.size(); ++i) { s += arr[i]; if (s == cnt) return i; } diff --git a/solution/0900-0999/0928.Minimize Malware Spread II/Solution.cpp b/solution/0900-0999/0928.Minimize Malware Spread II/Solution.cpp index ab85e92d80608..e346e376c082f 100644 --- a/solution/0900-0999/0928.Minimize Malware Spread II/Solution.cpp +++ b/solution/0900-0999/0928.Minimize Malware Spread II/Solution.cpp @@ -11,16 +11,14 @@ class Solution { fill(size.begin(), size.end(), 1); vector clean(n, true); for (int i : initial) clean[i] = false; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (!clean[i]) continue; for (int j = i + 1; j < n; ++j) if (clean[j] && graph[i][j] == 1) merge(i, j); } vector cnt(n, 0); unordered_map> mp; - for (int i : initial) - { + for (int i : initial) { unordered_set s; for (int j = 0; j < n; ++j) if (clean[j] && graph[i][j] == 1) s.insert(find(j)); @@ -28,14 +26,12 @@ class Solution { mp[i] = s; } int mx = -1, ans = 0; - for (auto& [i, s] : mp) - { + for (auto& [i, s] : mp) { int t = 0; for (int root : s) if (cnt[root] == 1) t += size[root]; - if (mx < t || (mx == t && i < ans)) - { + if (mx < t || (mx == t && i < ans)) { mx = t; ans = i; } @@ -50,8 +46,7 @@ class Solution { void merge(int a, int b) { int pa = find(a), pb = find(b); - if (pa != pb) - { + if (pa != pb) { size[pb] += size[pa]; p[pa] = pb; } diff --git a/solution/0900-0999/0929.Unique Email Addresses/Solution.cpp b/solution/0900-0999/0929.Unique Email Addresses/Solution.cpp index a9a8cb42a7cfe..b4d747dad2a99 100644 --- a/solution/0900-0999/0929.Unique Email Addresses/Solution.cpp +++ b/solution/0900-0999/0929.Unique Email Addresses/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int numUniqueEmails(vector& emails) { unordered_set s; - for (auto& email : emails) - { + for (auto& email : emails) { int i = email.find('@'); string local = email.substr(0, i); string domain = email.substr(i + 1); diff --git a/solution/0900-0999/0930.Binary Subarrays With Sum/Solution.cpp b/solution/0900-0999/0930.Binary Subarrays With Sum/Solution.cpp index ce6a1825a7bd4..35a24bdc5561c 100644 --- a/solution/0900-0999/0930.Binary Subarrays With Sum/Solution.cpp +++ b/solution/0900-0999/0930.Binary Subarrays With Sum/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int numSubarraysWithSum(vector& nums, int goal) { int i1 = 0, i2 = 0, s1 = 0, s2 = 0, j = 0, ans = 0; int n = nums.size(); - while (j < n) - { + while (j < n) { s1 += nums[j]; s2 += nums[j]; while (i1 <= j && s1 > goal) s1 -= nums[i1++]; diff --git a/solution/0900-0999/0933.Number of Recent Calls/Solution.cpp b/solution/0900-0999/0933.Number of Recent Calls/Solution.cpp index f9efde6459894..3b28b2c570b3c 100644 --- a/solution/0900-0999/0933.Number of Recent Calls/Solution.cpp +++ b/solution/0900-0999/0933.Number of Recent Calls/Solution.cpp @@ -3,9 +3,8 @@ class RecentCounter { deque q; RecentCounter() { - } - + int ping(int t) { q.push_back(t); while (q.front() < t - 3000) { diff --git a/solution/0900-0999/0934.Shortest Bridge/Solution.cpp b/solution/0900-0999/0934.Shortest Bridge/Solution.cpp index df72b5b4c5d9f..ba6bdfe2fdea5 100644 --- a/solution/0900-0999/0934.Shortest Bridge/Solution.cpp +++ b/solution/0900-0999/0934.Shortest Bridge/Solution.cpp @@ -7,22 +7,17 @@ class Solution { queue> q; dfs(start[0], start[1], q, grid); int ans = -1; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int k = q.size(); k > 0; --k) - { + for (int k = q.size(); k > 0; --k) { auto p = q.front(); q.pop(); - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { int x = p[0] + dirs[i]; int y = p[1] + dirs[i + 1]; - if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) - { + if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size()) { if (grid[x][y] == 1) return ans; - if (grid[x][y] == 0) - { + if (grid[x][y] == 0) { grid[x][y] = 2; q.push({x, y}); } @@ -36,8 +31,7 @@ class Solution { void dfs(int i, int j, queue>& q, vector>& grid) { grid[i][j] = 2; q.push({i, j}); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k]; int y = j + dirs[k + 1]; if (x >= 0 && x < grid.size() && y >= 0 && y < grid[0].size() && grid[x][y] == 1) diff --git a/solution/0900-0999/0935.Knight Dialer/Solution.cpp b/solution/0900-0999/0935.Knight Dialer/Solution.cpp index 01181e7bd97ef..534b6af607ffb 100644 --- a/solution/0900-0999/0935.Knight Dialer/Solution.cpp +++ b/solution/0900-0999/0935.Knight Dialer/Solution.cpp @@ -6,8 +6,7 @@ class Solution { if (n == 1) return 10; int mod = 1e9 + 7; vector f(10, 1ll); - while (--n) - { + while (--n) { vector t(10); t[0] = f[4] + f[6]; t[1] = f[6] + f[8]; @@ -21,6 +20,6 @@ class Solution { for (int i = 0; i < 10; ++i) f[i] = t[i] % mod; } ll ans = accumulate(f.begin(), f.end(), 0ll); - return (int) (ans % mod); + return (int)(ans % mod); } }; \ No newline at end of file diff --git a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp index 5b17f149ad5b3..141f9e629b895 100644 --- a/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp +++ b/solution/0900-0999/0945.Minimum Increment to Make Array Unique/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int minIncrementForUnique(vector& nums) { sort(nums.begin(), nums.end()); int ans = 0; - for (int i = 1; i < nums.size(); ++i) - { - if (nums[i] <= nums[i - 1]) - { + for (int i = 1; i < nums.size(); ++i) { + if (nums[i] <= nums[i - 1]) { int d = nums[i - 1] - nums[i] + 1; nums[i] += d; ans += d; diff --git a/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp b/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp index 9bfd7cee0ea1d..83ec163300e86 100644 --- a/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp +++ b/solution/0900-0999/0946.Validate Stack Sequences/Solution.cpp @@ -3,11 +3,9 @@ class Solution { bool validateStackSequences(vector& pushed, vector& popped) { int j = 0, n = popped.size(); stack stk; - for (int x : pushed) - { + for (int x : pushed) { stk.push(x); - while (!stk.empty() && j < n && stk.top() == popped[j]) - { + while (!stk.empty() && j < n && stk.top() == popped[j]) { stk.pop(); ++j; } diff --git a/solution/0900-0999/0950.Reveal Cards In Increasing Order/Solution.cpp b/solution/0900-0999/0950.Reveal Cards In Increasing Order/Solution.cpp index ad6d918d106d9..0e1eefcaf4fdc 100644 --- a/solution/0900-0999/0950.Reveal Cards In Increasing Order/Solution.cpp +++ b/solution/0900-0999/0950.Reveal Cards In Increasing Order/Solution.cpp @@ -2,32 +2,29 @@ public: vector deckRevealedIncreasing(vector& deck) { if (deck.size() == 0) - return {} ; - - sort(deck.begin(), deck.end()) ; - int len = deck.size() ; - - int flg = deck.at(0)-1 ; - vector o(len, flg) ; + return {}; + + sort(deck.begin(), deck.end()); + int len = deck.size(); + + int flg = deck.at(0) - 1; + vector o(len, flg); int i = 0, p = 0; - for ( ; ;) - { - o[p] = deck.at(i) ; - - if (++i < len) - { + for (;;) { + o[p] = deck.at(i); + + if (++i < len) { do { - p = (p+1)%len ; - } while (o.at(p) != flg) ; + p = (p + 1) % len; + } while (o.at(p) != flg); do { - p = (p+1)%len ; - } while (o.at(p) != flg) ; - } - else - break ; + p = (p + 1) % len; + } while (o.at(p) != flg); + } else + break; } - - return o ; + + return o; } }; \ No newline at end of file diff --git a/solution/0900-0999/0952.Largest Component Size by Common Factor/Solution.cpp b/solution/0900-0999/0952.Largest Component Size by Common Factor/Solution.cpp index 127ad86099901..9aa07215cdf50 100644 --- a/solution/0900-0999/0952.Largest Component Size by Common Factor/Solution.cpp +++ b/solution/0900-0999/0952.Largest Component Size by Common Factor/Solution.cpp @@ -3,7 +3,9 @@ class UnionFind { vector p; int n; - UnionFind(int _n): n(_n), p(_n) { + UnionFind(int _n) + : n(_n) + , p(_n) { iota(p.begin(), p.end(), 0); } @@ -23,13 +25,10 @@ class Solution { int largestComponentSize(vector& nums) { int m = *max_element(nums.begin(), nums.end()); UnionFind* uf = new UnionFind(m + 1); - for (int v : nums) - { + for (int v : nums) { int i = 2; - while (i <= v / i) - { - if (v % i == 0) - { + while (i <= v / i) { + if (v % i == 0) { uf->unite(v, i); uf->unite(v, v / i); } @@ -38,8 +37,7 @@ class Solution { } vector cnt(m + 1); int ans = 0; - for (int v : nums) - { + for (int v : nums) { int t = uf->find(v); ++cnt[t]; ans = max(ans, cnt[t]); diff --git a/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.cpp b/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.cpp index bae98e2e3c082..cdbf1db888f66 100644 --- a/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.cpp +++ b/solution/0900-0999/0953.Verifying an Alien Dictionary/Solution.cpp @@ -3,12 +3,10 @@ bool isAlienSorted(vector& words, string order) { vector m(26); for (int i = 0; i < 26; ++i) m[order[i] - 'a'] = i; - for (int i = 0; i < 20; ++i) - { + for (int i = 0; i < 20; ++i) { int prev = -1; bool valid = true; - for (auto& x : words) - { + for (auto& x : words) { int curr = i >= x.size() ? -1 : m[x[i] - 'a']; if (prev > curr) return false; if (prev == curr) valid = false; diff --git a/solution/0900-0999/0954.Array of Doubled Pairs/Solution.cpp b/solution/0900-0999/0954.Array of Doubled Pairs/Solution.cpp index 59223c11b8868..0fa95728e2c70 100644 --- a/solution/0900-0999/0954.Array of Doubled Pairs/Solution.cpp +++ b/solution/0900-0999/0954.Array of Doubled Pairs/Solution.cpp @@ -7,8 +7,7 @@ class Solution { vector keys; for (auto& [k, _] : freq) keys.push_back(k); sort(keys.begin(), keys.end(), [](int a, int b) { return abs(a) < abs(b); }); - for (int& k : keys) - { + for (int& k : keys) { if (freq[k * 2] < freq[k]) return false; freq[k * 2] -= freq[k]; } diff --git a/solution/0900-0999/0958.Check Completeness of a Binary Tree/Solution.cpp b/solution/0900-0999/0958.Check Completeness of a Binary Tree/Solution.cpp index f525c11edbedf..f82a7a61e43b4 100644 --- a/solution/0900-0999/0958.Check Completeness of a Binary Tree/Solution.cpp +++ b/solution/0900-0999/0958.Check Completeness of a Binary Tree/Solution.cpp @@ -12,9 +12,8 @@ class Solution { public: bool isCompleteTree(TreeNode* root) { - queue q{{root}}; - while (q.front()) - { + queue q {{root}}; + while (q.front()) { root = q.front(); q.pop(); q.push(root->left); diff --git a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.cpp b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.cpp index 2a17b97269466..6187ccf5b0d6f 100644 --- a/solution/0900-0999/0959.Regions Cut By Slashes/Solution.cpp +++ b/solution/0900-0999/0959.Regions Cut By Slashes/Solution.cpp @@ -8,26 +8,19 @@ class Solution { size = n * n * 4; p.resize(size); for (int i = 0; i < size; ++i) p[i] = i; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { int k = i * n + j; if (i < n - 1) merge(4 * k + 2, (k + n) * 4); if (j < n - 1) merge(4 * k + 1, (k + 1) * 4 + 3); char v = grid[i][j]; - if (v == '/') - { + if (v == '/') { merge(4 * k, 4 * k + 3); merge(4 * k + 1, 4 * k + 2); - } - else if (v == '\\') - { + } else if (v == '\\') { merge(4 * k, 4 * k + 1); merge(4 * k + 2, 4 * k + 3); - } - else - { + } else { merge(4 * k, 4 * k + 1); merge(4 * k + 1, 4 * k + 2); merge(4 * k + 2, 4 * k + 3); diff --git a/solution/0900-0999/0960.Delete Columns to Make Sorted III/Solution.cpp b/solution/0900-0999/0960.Delete Columns to Make Sorted III/Solution.cpp index 84cccca5cfb92..26082b92f978e 100644 --- a/solution/0900-0999/0960.Delete Columns to Make Sorted III/Solution.cpp +++ b/solution/0900-0999/0960.Delete Columns to Make Sorted III/Solution.cpp @@ -4,12 +4,9 @@ class Solution { int n = strs[0].size(); vector dp(n, 1); int mx = 1; - for (int i = 1; i < n; ++i) - { - for (int j = 0; j < i; ++j) - { - if (check(i, j, strs)) - { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { + if (check(i, j, strs)) { dp[i] = max(dp[i], dp[j] + 1); } } diff --git a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.cpp b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.cpp index 2aab7f41e3158..d307de76cc196 100644 --- a/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.cpp +++ b/solution/0900-0999/0961.N-Repeated Element in Size 2N Array/Solution.cpp @@ -2,7 +2,7 @@ public: int repeatedNTimes(vector& nums) { unordered_set s; - for (auto &num : nums) { + for (auto& num : nums) { if (s.find(num) != s.end()) { return num; } diff --git a/solution/0900-0999/0962.Maximum Width Ramp/Solution.cpp b/solution/0900-0999/0962.Maximum Width Ramp/Solution.cpp index ee049bd136f58..d25523463b800 100644 --- a/solution/0900-0999/0962.Maximum Width Ramp/Solution.cpp +++ b/solution/0900-0999/0962.Maximum Width Ramp/Solution.cpp @@ -3,15 +3,12 @@ class Solution { int maxWidthRamp(vector& nums) { int n = nums.size(); stack stk; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (stk.empty() || nums[stk.top()] > nums[i]) stk.push(i); } int ans = 0; - for (int i = n - 1; i; --i) - { - while (!stk.empty() && nums[stk.top()] <= nums[i]) - { + for (int i = n - 1; i; --i) { + while (!stk.empty() && nums[stk.top()] <= nums[i]) { ans = max(ans, i - stk.top()); stk.pop(); } diff --git a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.cpp b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.cpp index 7cdd444529522..9e5b5b328b2c9 100644 --- a/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.cpp +++ b/solution/0900-0999/0967.Numbers With Same Consecutive Differences/Solution.cpp @@ -5,12 +5,11 @@ class Solution { vector numsSameConsecDiff(int n, int k) { for (int i = 1; i < 10; ++i) dfs(n - 1, k, i); - return ans; + return ans; } void dfs(int n, int k, int t) { - if (n == 0) - { + if (n == 0) { ans.push_back(t); return; } diff --git a/solution/0900-0999/0968.Binary Tree Cameras/Solution.cpp b/solution/0900-0999/0968.Binary Tree Cameras/Solution.cpp index ab981fecab662..7e5244e6fafe6 100644 --- a/solution/0900-0999/0968.Binary Tree Cameras/Solution.cpp +++ b/solution/0900-0999/0968.Binary Tree Cameras/Solution.cpp @@ -22,8 +22,7 @@ class Solution { int dfs(TreeNode* root) { if (!root) return 2; int left = dfs(root->left), right = dfs(root->right); - if (left == 0 || right == 0) - { + if (left == 0 || right == 0) { ++ans; return 1; } diff --git a/solution/0900-0999/0969.Pancake Sorting/Solution.cpp b/solution/0900-0999/0969.Pancake Sorting/Solution.cpp index 18384c849d0c9..07dbac85c269e 100644 --- a/solution/0900-0999/0969.Pancake Sorting/Solution.cpp +++ b/solution/0900-0999/0969.Pancake Sorting/Solution.cpp @@ -3,13 +3,12 @@ class Solution { vector pancakeSort(vector& arr) { int n = arr.size(); vector ans; - for (int i = n - 1; i > 0; --i) - { + for (int i = n - 1; i > 0; --i) { int j = i; - for (; j > 0 && arr[j] != i + 1; --j); + for (; j > 0 && arr[j] != i + 1; --j) + ; if (j == i) continue; - if (j > 0) - { + if (j > 0) { ans.push_back(j + 1); reverse(arr.begin(), arr.begin() + j + 1); } diff --git a/solution/0900-0999/0974.Subarray Sums Divisible by K/Solution.cpp b/solution/0900-0999/0974.Subarray Sums Divisible by K/Solution.cpp index 10a00f7363dcb..84c873149d2b7 100644 --- a/solution/0900-0999/0974.Subarray Sums Divisible by K/Solution.cpp +++ b/solution/0900-0999/0974.Subarray Sums Divisible by K/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map counter; counter[0] = 1; int s = 0, ans = 0; - for (int& num : nums) - { + for (int& num : nums) { s += num; int t = (s % k + k) % k; ans += counter[t]; diff --git a/solution/0900-0999/0976.Largest Perimeter Triangle/Solution.cpp b/solution/0900-0999/0976.Largest Perimeter Triangle/Solution.cpp index 96c1b271a615b..525d09c6653aa 100644 --- a/solution/0900-0999/0976.Largest Perimeter Triangle/Solution.cpp +++ b/solution/0900-0999/0976.Largest Perimeter Triangle/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int largestPerimeter(vector& nums) { sort(nums.begin(), nums.end()); - for (int i = nums.size() - 1; i >= 2; --i) - { + for (int i = nums.size() - 1; i >= 2; --i) { int c = nums[i - 1] + nums[i - 2]; if (c > nums[i]) return c + nums[i]; } diff --git a/solution/0900-0999/0985.Sum of Even Numbers After Queries/Solution.cpp b/solution/0900-0999/0985.Sum of Even Numbers After Queries/Solution.cpp index cd46326b4d9cf..0fb2f41c6593d 100644 --- a/solution/0900-0999/0985.Sum of Even Numbers After Queries/Solution.cpp +++ b/solution/0900-0999/0985.Sum of Even Numbers After Queries/Solution.cpp @@ -6,14 +6,16 @@ class Solution { if (num % 2 == 0) s += num; vector ans; - for (auto& q : queries) - { + for (auto& q : queries) { int v = q[0], i = q[1]; int old = nums[i]; nums[i] += v; - if (nums[i] % 2 == 0 && old % 2 == 0) s += v; - else if (nums[i] % 2 == 0 && old % 2 != 0) s += nums[i]; - else if (old % 2 == 0) s -= old; + if (nums[i] % 2 == 0 && old % 2 == 0) + s += v; + else if (nums[i] % 2 == 0 && old % 2 != 0) + s += nums[i]; + else if (old % 2 == 0) + s -= old; ans.push_back(s); } return ans; diff --git a/solution/0900-0999/0986.Interval List Intersections/Solution.cpp b/solution/0900-0999/0986.Interval List Intersections/Solution.cpp index 93299cde62aed..1f6260c079465 100644 --- a/solution/0900-0999/0986.Interval List Intersections/Solution.cpp +++ b/solution/0900-0999/0986.Interval List Intersections/Solution.cpp @@ -3,13 +3,14 @@ class Solution { vector> intervalIntersection(vector>& firstList, vector>& secondList) { vector> ans; int m = firstList.size(), n = secondList.size(); - for (int i = 0, j = 0; i < m && j < n;) - { + for (int i = 0, j = 0; i < m && j < n;) { int l = max(firstList[i][0], secondList[j][0]); int r = min(firstList[i][1], secondList[j][1]); if (l <= r) ans.push_back({l, r}); - if (firstList[i][1] < secondList[j][1]) ++i; - else ++j; + if (firstList[i][1] < secondList[j][1]) + ++i; + else + ++j; } return ans; } diff --git a/solution/0900-0999/0988.Smallest String Starting From Leaf/Solution.cpp b/solution/0900-0999/0988.Smallest String Starting From Leaf/Solution.cpp index 17dc8f166ff8e..5090bba99c5bd 100644 --- a/solution/0900-0999/0988.Smallest String Starting From Leaf/Solution.cpp +++ b/solution/0900-0999/0988.Smallest String Starting From Leaf/Solution.cpp @@ -22,8 +22,7 @@ class Solution { void dfs(TreeNode* root, string& path) { if (!root) return; path += 'a' + root->val; - if (!root->left && !root->right) - { + if (!root->left && !root->right) { string t = path; reverse(t.begin(), t.end()); if (ans == "" || t < ans) ans = t; diff --git a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp index c04fccd0154fb..c13bcf685c42a 100644 --- a/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp +++ b/solution/0900-0999/0989.Add to Array-Form of Integer/Solution.cpp @@ -3,8 +3,7 @@ class Solution { vector addToArrayForm(vector& num, int k) { int i = num.size() - 1, carry = 0; vector ans; - for (; i >= 0 || k || carry; --i) - { + for (; i >= 0 || k || carry; --i) { carry += (i < 0 ? 0 : num[i]) + k % 10; ans.push_back(carry % 10); carry /= 10; diff --git a/solution/0900-0999/0990.Satisfiability of Equality Equations/Solution.cpp b/solution/0900-0999/0990.Satisfiability of Equality Equations/Solution.cpp index 1d6ea3ad09d65..c4155b1cbb466 100644 --- a/solution/0900-0999/0990.Satisfiability of Equality Equations/Solution.cpp +++ b/solution/0900-0999/0990.Satisfiability of Equality Equations/Solution.cpp @@ -5,13 +5,11 @@ class Solution { bool equationsPossible(vector& equations) { p.resize(26); for (int i = 0; i < 26; ++i) p[i] = i; - for (auto& e : equations) - { + for (auto& e : equations) { int a = e[0] - 'a', b = e[3] - 'a'; if (e[1] == '=') p[find(a)] = find(b); } - for (auto& e : equations) - { + for (auto& e : equations) { int a = e[0] - 'a', b = e[3] - 'a'; if (e[1] == '!' && find(a) == find(b)) return false; } diff --git a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp index 35e47b89cc8f0..03f58aa7b2b7b 100644 --- a/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp +++ b/solution/0900-0999/0993.Cousins in Binary Tree/Solution.cpp @@ -17,21 +17,17 @@ class Solution { queue q; q.push(root); int i = 0; - while (!q.empty()) - { + while (!q.empty()) { int n = q.size(); - while (n--) - { + while (n--) { auto node = q.front(); d[node->val] = i; q.pop(); - if (node->left) - { + if (node->left) { q.push(node->left); p[node->left->val] = node->val; } - if (node->right) - { + if (node->right) { q.push(node->right); p[node->right->val] = node->val; } diff --git a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp index ee73cd14db37d..ed59f741edce5 100644 --- a/solution/0900-0999/0994.Rotting Oranges/Solution.cpp +++ b/solution/0900-0999/0994.Rotting Oranges/Solution.cpp @@ -5,29 +5,25 @@ class Solution { int cnt = 0; typedef pair pii; queue q; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 2) q.emplace(i, j); - else if (grid[i][j] == 1) ++cnt; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 2) + q.emplace(i, j); + else if (grid[i][j] == 1) + ++cnt; } } int ans = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty() && cnt > 0) - { + while (!q.empty() && cnt > 0) { ++ans; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { auto p = q.front(); q.pop(); - for (int j = 0; j < 4; ++j) - { + for (int j = 0; j < 4; ++j) { int x = p.first + dirs[j]; int y = p.second + dirs[j + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) - { + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) { --cnt; grid[x][y] = 2; q.emplace(x, y); diff --git a/solution/0900-0999/0997.Find the Town Judge/Solution.cpp b/solution/0900-0999/0997.Find the Town Judge/Solution.cpp index 28a89cf1144a7..7fb6850cd3b2b 100644 --- a/solution/0900-0999/0997.Find the Town Judge/Solution.cpp +++ b/solution/0900-0999/0997.Find the Town Judge/Solution.cpp @@ -4,13 +4,11 @@ class Solution { int N = 1001; vector c1(N); vector c2(N); - for (auto& e : trust) - { + for (auto& e : trust) { ++c1[e[0]]; ++c2[e[1]]; } - for (int i = 1; i < N; ++i) - { + for (int i = 1; i < N; ++i) { if (c1[i] == 0 && c2[i] == n - 1) return i; } return -1; diff --git a/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp b/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp index 549a4ca972541..e537b49d6eece 100644 --- a/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp +++ b/solution/0900-0999/0999.Available Captures for Rook/Solution.cpp @@ -3,16 +3,13 @@ class Solution { int numRookCaptures(vector>& board) { vector pos = find(board); int ans = 0, n = 8; - vector> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}}; - for (auto& dir : dirs) - { + vector> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + for (auto& dir : dirs) { int x = pos[0], y = pos[1], a = dir[0], b = dir[1]; - while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') - { + while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n && board[x + a][y + b] != 'B') { x += a; y += b; - if (board[x][y] == 'p') - { + if (board[x][y] == 'p') { ++ans; break; } @@ -23,12 +20,9 @@ class Solution { vector find(vector>& board) { int n = 8; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (board[i][j] == 'R') - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (board[i][j] == 'R') { return {i, j}; } } diff --git a/solution/1000-1099/1002.Find Common Characters/Solution.cpp b/solution/1000-1099/1002.Find Common Characters/Solution.cpp index e7ce00dac9ebb..b821a4185d47a 100644 --- a/solution/1000-1099/1002.Find Common Characters/Solution.cpp +++ b/solution/1000-1099/1002.Find Common Characters/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: vector commonChars(vector& words) { vector freq(26, 10000); - for (auto word : words) - { + for (auto word : words) { vector t(26); for (char c : word) ++t[c - 'a']; @@ -11,8 +10,7 @@ class Solution { freq[i] = min(freq[i], t[i]); } vector res; - for (int i = 0; i < 26; i++) - { + for (int i = 0; i < 26; i++) { while (freq[i]--) res.emplace_back(1, i + 'a'); } diff --git a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp index 000f7493a375e..cdf78071b9df8 100644 --- a/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp +++ b/solution/1000-1099/1004.Max Consecutive Ones III/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int longestOnes(vector& nums, int k) { int l = 0, r = 0; - while (r < nums.size()) - { + while (r < nums.size()) { if (nums[r++] == 0) --k; if (k < 0 && nums[l++] == 0) ++k; } diff --git a/solution/1000-1099/1005.Maximize Sum Of Array After K Negations/Solution.cpp b/solution/1000-1099/1005.Maximize Sum Of Array After K Negations/Solution.cpp index 4223da051b5c6..9a5b8f822aaf8 100644 --- a/solution/1000-1099/1005.Maximize Sum Of Array After K Negations/Solution.cpp +++ b/solution/1000-1099/1005.Maximize Sum Of Array After K Negations/Solution.cpp @@ -2,12 +2,10 @@ class Solution { public: int largestSumAfterKNegations(vector& nums, int k) { unordered_map counter; - for (int num: nums) ++counter[num]; + for (int num : nums) ++counter[num]; int ans = accumulate(nums.begin(), nums.end(), 0); - for (int i = -100; i < 0; ++i) - { - if (counter[i]) - { + for (int i = -100; i < 0; ++i) { + if (counter[i]) { int ops = min(counter[i], k); ans -= (i * ops * 2); counter[i] -= ops; @@ -16,12 +14,9 @@ class Solution { if (k == 0) break; } } - if (k > 0 && k % 2 == 1 && !counter[0]) - { - for (int i = 1; i < 101; ++i) - { - if (counter[i]) - { + if (k > 0 && k % 2 == 1 && !counter[0]) { + for (int i = 1; i < 101; ++i) { + if (counter[i]) { ans -= 2 * i; break; } diff --git a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.cpp b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.cpp index 462a40368ef3b..5e03ec536d15d 100644 --- a/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.cpp +++ b/solution/1000-1099/1008.Construct Binary Search Tree from Preorder Traversal/Solution.cpp @@ -19,11 +19,12 @@ class Solution { if (i > j || i >= preorder.size()) return nullptr; TreeNode* root = new TreeNode(preorder[i]); int left = i + 1, right = j + 1; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (preorder[mid] > preorder[i]) right = mid; - else left = mid + 1; + if (preorder[mid] > preorder[i]) + right = mid; + else + left = mid + 1; } root->left = dfs(preorder, i + 1, left - 1); root->right = dfs(preorder, left, j); diff --git a/solution/1000-1099/1009.Complement of Base 10 Integer/Solution.cpp b/solution/1000-1099/1009.Complement of Base 10 Integer/Solution.cpp index 4a93fe2108229..700de14c4970c 100644 --- a/solution/1000-1099/1009.Complement of Base 10 Integer/Solution.cpp +++ b/solution/1000-1099/1009.Complement of Base 10 Integer/Solution.cpp @@ -4,8 +4,7 @@ class Solution { if (n == 0) return 1; int ans = 0; bool find = false; - for (int i = 30; i >= 0; --i) - { + for (int i = 30; i >= 0; --i) { int b = n & (1 << i); if (!find && b == 0) continue; find = true; diff --git a/solution/1000-1099/1011.Capacity To Ship Packages Within D Days/Solution.cpp b/solution/1000-1099/1011.Capacity To Ship Packages Within D Days/Solution.cpp index 50843b753eebb..4b94495048f82 100644 --- a/solution/1000-1099/1011.Capacity To Ship Packages Within D Days/Solution.cpp +++ b/solution/1000-1099/1011.Capacity To Ship Packages Within D Days/Solution.cpp @@ -1,36 +1,27 @@ class Solution { public: - int shipWithinDays(vector &weights, int days) { + int shipWithinDays(vector& weights, int days) { int left = 1, right = 25000000; - while (left < right) - { + while (left < right) { int mid = left + right >> 1; - if (check(weights, days, mid)) - { + if (check(weights, days, mid)) { right = mid; - } - else - { + } else { left = mid + 1; } } return left; } - bool check(vector &weights, int days, int capacity) { + bool check(vector& weights, int days, int capacity) { int cnt = 1, t = 0; - for (auto w : weights) - { - if (w > capacity) - { + for (auto w : weights) { + if (w > capacity) { return false; } - if (t + w <= capacity) - { + if (t + w <= capacity) { t += w; - } - else - { + } else { ++cnt; t = w; } diff --git a/solution/1000-1099/1020.Number of Enclaves/Solution.cpp b/solution/1000-1099/1020.Number of Enclaves/Solution.cpp index e9021c51a89ae..a0387df6c1321 100644 --- a/solution/1000-1099/1020.Number of Enclaves/Solution.cpp +++ b/solution/1000-1099/1020.Number of Enclaves/Solution.cpp @@ -21,8 +21,7 @@ class Solution { void dfs(int i, int j, vector>& grid) { grid[i][j] = 0; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k]; int y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == 1) diff --git a/solution/1000-1099/1021.Remove Outermost Parentheses/Solution.cpp b/solution/1000-1099/1021.Remove Outermost Parentheses/Solution.cpp index 56605a8422ffb..ae7c7d52d753c 100644 --- a/solution/1000-1099/1021.Remove Outermost Parentheses/Solution.cpp +++ b/solution/1000-1099/1021.Remove Outermost Parentheses/Solution.cpp @@ -3,7 +3,7 @@ class Solution { string removeOuterParentheses(string s) { string res; int depth = 0; - for (char c: s) { + for (char c : s) { if (c == '(') { depth++; } diff --git a/solution/1000-1099/1024.Video Stitching/Solution.cpp b/solution/1000-1099/1024.Video Stitching/Solution.cpp index c2fcb2b87838b..c0392c915f1b3 100644 --- a/solution/1000-1099/1024.Video Stitching/Solution.cpp +++ b/solution/1000-1099/1024.Video Stitching/Solution.cpp @@ -3,17 +3,17 @@ class Solution { int videoStitching(vector>& clips, int T) { int maxEnding = 0; int count = 0; - vector maxEnd(T+1, 0); // maxEnd[i] : maximum ending time of all videos that start by time i + vector maxEnd(T + 1, 0); // maxEnd[i] : maximum ending time of all videos that start by time i for (int i = 0; i < clips.size(); ++i) { if (clips[i][0] >= T) continue; maxEnd[clips[i][0]] = max(maxEnd[clips[i][0]], clips[i][1]); } for (int i = 1; i < T; ++i) { - maxEnd[i] = max(maxEnd[i], maxEnd[i-1]); + maxEnd[i] = max(maxEnd[i], maxEnd[i - 1]); } - for (int i = 0; i < T; ++i) { - if (maxEnding == i) { // select video with maximum ending if one more video is necessary - count ++; + for (int i = 0; i < T; ++i) { + if (maxEnding == i) { // select video with maximum ending if one more video is necessary + count++; maxEnding = maxEnd[i]; } else if (maxEnding < i) { return -1; diff --git a/solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.cpp b/solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.cpp index d5b992f52616c..a36e7eb7154f5 100644 --- a/solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.cpp +++ b/solution/1000-1099/1027.Longest Arithmetic Subsequence/Solution.cpp @@ -4,10 +4,8 @@ class Solution { int n = nums.size(); int ans = 0; vector> dp(n, vector(1001, 1)); - for (int i = 1; i < n; ++i) - { - for (int j = 0; j < i; ++j) - { + for (int i = 1; i < n; ++i) { + for (int j = 0; j < i; ++j) { int d = nums[i] - nums[j] + 500; dp[i][d] = max(dp[i][d], dp[j][d] + 1); ans = max(ans, dp[i][d]); diff --git a/solution/1000-1099/1028.Recover a Tree From Preorder Traversal/Solution.cpp b/solution/1000-1099/1028.Recover a Tree From Preorder Traversal/Solution.cpp index 8dc264ae3b34d..e2dd575a9d8cd 100644 --- a/solution/1000-1099/1028.Recover a Tree From Preorder Traversal/Solution.cpp +++ b/solution/1000-1099/1028.Recover a Tree From Preorder Traversal/Solution.cpp @@ -15,12 +15,12 @@ class Solution { int num = 0; for (int i = 0; i < S.length(); ++i) { if (S[i] == '-') { - depth ++; + depth++; } else { - num = 10*num + S[i]-'0'; + num = 10 * num + S[i] - '0'; } - if (i+1 >= S.length() || (isdigit(S[i]) && S[i+1] == '-')) { - TreeNode * newNode = new TreeNode(num); + if (i + 1 >= S.length() || (isdigit(S[i]) && S[i + 1] == '-')) { + TreeNode* newNode = new TreeNode(num); while (st.size() > depth) { st.pop(); } diff --git a/solution/1000-1099/1029.Two City Scheduling/Solution.cpp b/solution/1000-1099/1029.Two City Scheduling/Solution.cpp index e8e4cb61d75f3..8922dc1253ddb 100644 --- a/solution/1000-1099/1029.Two City Scheduling/Solution.cpp +++ b/solution/1000-1099/1029.Two City Scheduling/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: int twoCitySchedCost(vector>& costs) { - sort(costs.begin(), costs.end(), [](const std::vector &a, const std::vector &b) { + sort(costs.begin(), costs.end(), [](const std::vector& a, const std::vector& b) { return a[0] - a[1] < (b[0] - b[1]); }); int ans = 0; diff --git a/solution/1000-1099/1030.Matrix Cells in Distance Order/Solution.cpp b/solution/1000-1099/1030.Matrix Cells in Distance Order/Solution.cpp index 8af2a14c269ac..515b7c28c228d 100644 --- a/solution/1000-1099/1030.Matrix Cells in Distance Order/Solution.cpp +++ b/solution/1000-1099/1030.Matrix Cells in Distance Order/Solution.cpp @@ -7,18 +7,14 @@ class Solution { vis[rCenter][cCenter] = true; vector> ans; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { - for (int n = q.size(); n > 0; --n) - { + while (!q.empty()) { + for (int n = q.size(); n > 0; --n) { auto p = q.front(); q.pop(); ans.push_back(p); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = p[0] + dirs[k], y = p[1] + dirs[k + 1]; - if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y]) - { + if (x >= 0 && x < rows && y >= 0 && y < cols && !vis[x][y]) { q.push({x, y}); vis[x][y] = true; } diff --git a/solution/1000-1099/1034.Coloring A Border/Solution.cpp b/solution/1000-1099/1034.Coloring A Border/Solution.cpp index adbf9fb9a69e7..b0c97af31ff92 100644 --- a/solution/1000-1099/1034.Coloring A Border/Solution.cpp +++ b/solution/1000-1099/1034.Coloring A Border/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: int m, n; - vector> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}}; + vector> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; vector> colorBorder(vector>& grid, int row, int col, int color) { m = grid.size(); @@ -14,18 +14,17 @@ class Solution { void dfs(int i, int j, int color, vector>& grid, vector>& vis) { vis[i][j] = true; int oldColor = grid[i][j]; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int x = i + dir[0], y = j + dir[1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { - if (!vis[x][y]) - { - if (grid[x][y] == oldColor) dfs(x, y, color, grid, vis); - else grid[i][j] = color; + if (x >= 0 && x < m && y >= 0 && y < n) { + if (!vis[x][y]) { + if (grid[x][y] == oldColor) + dfs(x, y, color, grid, vis); + else + grid[i][j] = color; } - } - else grid[i][j] = color; + } else + grid[i][j] = color; } } }; \ No newline at end of file diff --git a/solution/1000-1099/1036.Escape a Large Maze/Solution.cpp b/solution/1000-1099/1036.Escape a Large Maze/Solution.cpp index 65c4d9dc5c57c..80d5f3825d827 100644 --- a/solution/1000-1099/1036.Escape a Large Maze/Solution.cpp +++ b/solution/1000-1099/1036.Escape a Large Maze/Solution.cpp @@ -8,7 +8,7 @@ class Solution { bool isEscapePossible(vector>& blocked, vector& source, vector& target) { this->blocked.clear(); - for (auto& b : blocked) this->blocked.insert((ULL) b[0] * N + b[1]); + for (auto& b : blocked) this->blocked.insert((ULL)b[0] * N + b[1]); unordered_set s1; unordered_set s2; return dfs(source, target, s1) && dfs(target, source, s2); @@ -17,11 +17,10 @@ class Solution { bool dfs(vector& source, vector& target, unordered_set& seen) { int sx = source[0], sy = source[1]; int tx = target[0], ty = target[1]; - if (sx < 0 || sx >= N || sy < 0 || sy >= N || tx < 0 || tx >= N || ty < 0 || ty >= N || blocked.count((ULL) sx * N + sy) || seen.count((ULL) sx * N + sy)) return 0; - seen.insert((ULL) sx * N + sy); + if (sx < 0 || sx >= N || sy < 0 || sy >= N || tx < 0 || tx >= N || ty < 0 || ty >= N || blocked.count((ULL)sx * N + sy) || seen.count((ULL)sx * N + sy)) return 0; + seen.insert((ULL)sx * N + sy); if (seen.size() > 20000 || (sx == target[0] && sy == target[1])) return 1; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { vector next = {sx + dir[0], sy + dir[1]}; if (dfs(next, target, seen)) return 1; } diff --git a/solution/1000-1099/1038.Binary Search Tree to Greater Sum Tree/Solution.cpp b/solution/1000-1099/1038.Binary Search Tree to Greater Sum Tree/Solution.cpp index 0a4627e1e8837..26862bb00fbf1 100644 --- a/solution/1000-1099/1038.Binary Search Tree to Greater Sum Tree/Solution.cpp +++ b/solution/1000-1099/1038.Binary Search Tree to Greater Sum Tree/Solution.cpp @@ -14,27 +14,20 @@ class Solution { TreeNode* bstToGst(TreeNode* root) { int s = 0; TreeNode* node = root; - while (root) - { + while (root) { if (root->right == nullptr) { s += root->val; root->val = s; root = root->left; - } - else - { + } else { TreeNode* next = root->right; - while (next->left && next->left != root) - { + while (next->left && next->left != root) { next = next->left; } - if (next->left == nullptr) - { + if (next->left == nullptr) { next->left = root; root = root->right; - } - else - { + } else { s += root->val; root->val = s; next->left = nullptr; diff --git a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.cpp b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.cpp index ea46227936e7c..a6ac9ef6d1d55 100644 --- a/solution/1000-1099/1041.Robot Bounded In Circle/Solution.cpp +++ b/solution/1000-1099/1041.Robot Bounded In Circle/Solution.cpp @@ -3,11 +3,13 @@ class Solution { bool isRobotBounded(string instructions) { vector direction(4); int cur = 0; - for (char c : instructions) - { - if (c == 'L') cur = (cur + 1) % 4; - else if (c == 'R') cur = (cur + 3) % 4; - else ++direction[cur]; + for (char c : instructions) { + if (c == 'L') + cur = (cur + 1) % 4; + else if (c == 'R') + cur = (cur + 3) % 4; + else + ++direction[cur]; } return cur != 0 || (direction[0] == direction[2] && direction[1] == direction[3]); } diff --git a/solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.cpp b/solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.cpp index c75d22bf88f35..476437642a675 100644 --- a/solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.cpp +++ b/solution/1000-1099/1042.Flower Planting With No Adjacent/Solution.cpp @@ -2,21 +2,17 @@ class Solution { public: vector gardenNoAdj(int n, vector>& paths) { vector> g(n); - for (auto& p : paths) - { + for (auto& p : paths) { int x = p[0] - 1, y = p[1] - 1; g[x].push_back(y); g[y].push_back(x); } vector ans(n); - for (int u = 0; u < n; ++u) - { + for (int u = 0; u < n; ++u) { unordered_set colors; for (int v : g[u]) colors.insert(ans[v]); - for (int c = 1; c < 5; ++c) - { - if (!colors.count(c)) - { + for (int c = 1; c < 5; ++c) { + if (!colors.count(c)) { ans[u] = c; break; } diff --git a/solution/1000-1099/1044.Longest Duplicate Substring/Solution.cpp b/solution/1000-1099/1044.Longest Duplicate Substring/Solution.cpp index fb20d2f2f3bf1..d06586836fdb9 100644 --- a/solution/1000-1099/1044.Longest Duplicate Substring/Solution.cpp +++ b/solution/1000-1099/1044.Longest Duplicate Substring/Solution.cpp @@ -7,20 +7,18 @@ class Solution { string longestDupSubstring(string s) { int base = 131, n = s.size(); p[0] = 1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { p[i + 1] = p[i] * base; h[i + 1] = h[i] * base + s[i]; } int left = 0, right = n; string ans = ""; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; string t = check(s, mid); - if (t.empty()) right = mid - 1; - else - { + if (t.empty()) + right = mid - 1; + else { left = mid; ans = t; } @@ -31,8 +29,7 @@ class Solution { string check(string& s, int len) { int n = s.size(); unordered_set vis; - for (int i = 1; i + len - 1 <= n; ++i) - { + for (int i = 1; i + len - 1 <= n; ++i) { int j = i + len - 1; ULL t = h[j] - h[i - 1] * p[j - i + 1]; if (vis.count(t)) return s.substr(i - 1, len); diff --git a/solution/1000-1099/1046.Last Stone Weight/Solution.cpp b/solution/1000-1099/1046.Last Stone Weight/Solution.cpp index 27079be734df3..500202d605e29 100644 --- a/solution/1000-1099/1046.Last Stone Weight/Solution.cpp +++ b/solution/1000-1099/1046.Last Stone Weight/Solution.cpp @@ -8,7 +8,7 @@ class Solution { int y = pq.top(); pq.pop(); if (x != y) - pq.push(x-y); + pq.push(x - y); } return pq.empty() ? 0 : pq.top(); } diff --git a/solution/1000-1099/1048.Longest String Chain/Solution.cpp b/solution/1000-1099/1048.Longest String Chain/Solution.cpp index d67feef49334b..bdafcd5465f97 100644 --- a/solution/1000-1099/1048.Longest String Chain/Solution.cpp +++ b/solution/1000-1099/1048.Longest String Chain/Solution.cpp @@ -1,15 +1,12 @@ class Solution { public: - int longestStrChain(vector &words) { - sort(words.begin(), words.end(), [&](string a, string b) - { return a.size() < b.size(); }); + int longestStrChain(vector& words) { + sort(words.begin(), words.end(), [&](string a, string b) { return a.size() < b.size(); }); int res = 0; unordered_map map; - for (auto word : words) - { + for (auto word : words) { int x = 1; - for (int i = 0; i < word.size(); ++i) - { + for (int i = 0; i < word.size(); ++i) { string pre = word.substr(0, i) + word.substr(i + 1); x = max(x, map[pre] + 1); } diff --git a/solution/1000-1099/1052.Grumpy Bookstore Owner/Solution.cpp b/solution/1000-1099/1052.Grumpy Bookstore Owner/Solution.cpp index f603e17e8110e..5a7692eee7fc1 100644 --- a/solution/1000-1099/1052.Grumpy Bookstore Owner/Solution.cpp +++ b/solution/1000-1099/1052.Grumpy Bookstore Owner/Solution.cpp @@ -3,18 +3,15 @@ class Solution { int maxSatisfied(vector& customers, vector& grumpy, int minutes) { int s = 0, cs = 0; int n = customers.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { s += customers[i] * grumpy[i]; cs += customers[i]; } int t = 0, ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { t += customers[i] * grumpy[i]; int j = i - minutes + 1; - if (j >= 0) - { + if (j >= 0) { ans = max(ans, cs - (s - t)); t -= customers[j] * grumpy[j]; } diff --git a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cpp b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cpp index 50e294bd887fb..03f0b519cde8d 100644 --- a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cpp +++ b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/Solution.cpp @@ -6,8 +6,7 @@ class Solution { p.resize(26); for (int i = 0; i < 26; ++i) p[i] = i; - for (int i = 0; i < s1.size(); ++i) - { + for (int i = 0; i < s1.size(); ++i) { int a = s1[i] - 'a', b = s2[i] - 'a'; int pa = find(a), pb = find(b); if (pa < pb) @@ -16,8 +15,7 @@ class Solution { p[pa] = pb; } string res = ""; - for (char a : baseStr) - { + for (char a : baseStr) { char b = (char)(find(a - 'a') + 'a'); res += b; } diff --git a/solution/1000-1099/1065.Index Pairs of a String/Solution.cpp b/solution/1000-1099/1065.Index Pairs of a String/Solution.cpp index 028ffb9561a97..d44e1a15a8224 100644 --- a/solution/1000-1099/1065.Index Pairs of a String/Solution.cpp +++ b/solution/1000-1099/1065.Index Pairs of a String/Solution.cpp @@ -9,8 +9,7 @@ class Trie { void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; @@ -26,11 +25,9 @@ class Solution { for (auto w : words) trie->insert(w); int n = text.size(); vector> ans; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { Trie* node = trie; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { int idx = text[j] - 'a'; if (!node->children[idx]) break; node = node->children[idx]; diff --git a/solution/1000-1099/1072.Flip Columns For Maximum Number of Equal Rows/Solution.cpp b/solution/1000-1099/1072.Flip Columns For Maximum Number of Equal Rows/Solution.cpp index 2908d6132c6da..ee34edd79795c 100644 --- a/solution/1000-1099/1072.Flip Columns For Maximum Number of Equal Rows/Solution.cpp +++ b/solution/1000-1099/1072.Flip Columns For Maximum Number of Equal Rows/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int maxEqualRowsAfterFlips(vector>& matrix) { unordered_map cnt; int ans = 0; - for (auto& row : matrix) - { + for (auto& row : matrix) { string s = ""; - for (int v : row) - { + for (int v : row) { if (row[0] == 1) v ^= 1; s += to_string(v); } diff --git a/solution/1000-1099/1079.Letter Tile Possibilities/Solution.cpp b/solution/1000-1099/1079.Letter Tile Possibilities/Solution.cpp index e7fb896436a85..4ccf6f586c1cd 100644 --- a/solution/1000-1099/1079.Letter Tile Possibilities/Solution.cpp +++ b/solution/1000-1099/1079.Letter Tile Possibilities/Solution.cpp @@ -8,10 +8,8 @@ class Solution { int dfs(vector& cnt) { int res = 0; - for (int i = 0; i < 26; ++i) - { - if (cnt[i]) - { + for (int i = 0; i < 26; ++i) { + if (cnt[i]) { --cnt[i]; ++res; res += dfs(cnt); diff --git a/solution/1000-1099/1089.Duplicate Zeros/Solution.c b/solution/1000-1099/1089.Duplicate Zeros/Solution.c index fc09fd49279ff..dc2080238376f 100644 --- a/solution/1000-1099/1089.Duplicate Zeros/Solution.c +++ b/solution/1000-1099/1089.Duplicate Zeros/Solution.c @@ -1,4 +1,4 @@ -void duplicateZeros(int* arr, int arrSize){ +void duplicateZeros(int* arr, int arrSize) { int i = 0; int j = 0; while (j < arrSize) { diff --git a/solution/1000-1099/1089.Duplicate Zeros/Solution.cpp b/solution/1000-1099/1089.Duplicate Zeros/Solution.cpp index dcbf51fcef3e9..3361d331ad47d 100644 --- a/solution/1000-1099/1089.Duplicate Zeros/Solution.cpp +++ b/solution/1000-1099/1089.Duplicate Zeros/Solution.cpp @@ -3,19 +3,16 @@ class Solution { void duplicateZeros(vector& arr) { int n = arr.size(); int i = -1, k = 0; - while (k < n) - { + while (k < n) { ++i; k += arr[i] ? 1 : 2; } int j = n - 1; - if (k == n + 1) - { + if (k == n + 1) { arr[j--] = 0; --i; } - while (~j) - { + while (~j) { arr[j] = arr[i]; if (arr[i] == 0) arr[--j] = arr[i]; --i; diff --git a/solution/1000-1099/1090.Largest Values From Labels/Solution.cpp b/solution/1000-1099/1090.Largest Values From Labels/Solution.cpp index 657537ff150b0..e181f7f9bc079 100644 --- a/solution/1000-1099/1090.Largest Values From Labels/Solution.cpp +++ b/solution/1000-1099/1090.Largest Values From Labels/Solution.cpp @@ -7,11 +7,9 @@ class Solution { sort(p.begin(), p.end()); unordered_map counter; int ans = 0, num = 0; - for (int i = n - 1; i >= 0 && num < numWanted; --i) - { + for (int i = n - 1; i >= 0 && num < numWanted; --i) { int v = p[i].first, l = p[i].second; - if (counter[l] < useLimit) - { + if (counter[l] < useLimit) { ++counter[l]; ++num; ans += v; diff --git a/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.cpp b/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.cpp index cb13298ff4b4c..886db60a43e52 100644 --- a/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.cpp +++ b/solution/1000-1099/1091.Shortest Path in Binary Matrix/Solution.cpp @@ -7,21 +7,16 @@ class Solution { q.push({0, 0}); grid[0][0] = 1; int ans = 0; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int m = q.size(); m > 0; --m) - { + for (int m = q.size(); m > 0; --m) { auto p = q.front(); q.pop(); int i = p.first, j = p.second; if (i == n - 1 && j == n - 1) return ans; - for (int x = i - 1; x <= i + 1; ++x) - { - for (int y = j - 1; y <= j + 1; ++y) - { - if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) - { + for (int x = i - 1; x <= i + 1; ++x) { + for (int y = j - 1; y <= j + 1; ++y) { + if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) { q.push({x, y}); grid[x][y] = 1; } diff --git a/solution/1000-1099/1094.Car Pooling/Solution.cpp b/solution/1000-1099/1094.Car Pooling/Solution.cpp index 860fd4f1bcf54..c60b22713b329 100644 --- a/solution/1000-1099/1094.Car Pooling/Solution.cpp +++ b/solution/1000-1099/1094.Car Pooling/Solution.cpp @@ -2,13 +2,13 @@ class Solution { public: bool carPooling(vector>& trips, int capacity) { vector delta(1001); - for (auto &trip : trips) { + for (auto& trip : trips) { int num = trip[0], start = trip[1], end = trip[2]; delta[start] += num; delta[end] -= num; } int cur = 0; - for (auto &num : delta) { + for (auto& num : delta) { cur += num; if (cur > capacity) { return false; diff --git a/solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/Solution.cpp b/solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/Solution.cpp index 9fa42aaf43585..05e085d37b25a 100644 --- a/solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/Solution.cpp +++ b/solution/1100-1199/1100.Find K-Length Substrings With No Repeated Characters/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int numKLenSubstrNoRepeats(string s, int k) { int ans = 0; unordered_map mp; - for (int i = 0, j = 0; i < s.size(); ++i) - { + for (int i = 0, j = 0; i < s.size(); ++i) { char c = s[i]; if (mp.count(c)) j = max(j, mp[c] + 1); mp[c] = i; diff --git a/solution/1100-1199/1101.The Earliest Moment When Everyone Become Friends/Solution.cpp b/solution/1100-1199/1101.The Earliest Moment When Everyone Become Friends/Solution.cpp index fdd0d77817d44..e3831e3504915 100644 --- a/solution/1100-1199/1101.The Earliest Moment When Everyone Become Friends/Solution.cpp +++ b/solution/1100-1199/1101.The Earliest Moment When Everyone Become Friends/Solution.cpp @@ -6,8 +6,7 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; sort(logs.begin(), logs.end()); - for (auto& log : logs) - { + for (auto& log : logs) { int t = log[0], a = log[1], b = log[2]; if (find(a) == find(b)) continue; p[find(a)] = find(b); diff --git a/solution/1100-1199/1102.Path With Maximum Minimum Value/Solution.cpp b/solution/1100-1199/1102.Path With Maximum Minimum Value/Solution.cpp index 0207229a5b31d..4e3b3ef9aebc8 100644 --- a/solution/1100-1199/1102.Path With Maximum Minimum Value/Solution.cpp +++ b/solution/1100-1199/1102.Path With Maximum Minimum Value/Solution.cpp @@ -16,14 +16,12 @@ class Solution { scores.emplace_back(grid[i][j], i, j); sort(scores.begin(), scores.end()); vector dirs = {-1, 0, 1, 0, -1}; - while (find(0) != find(m * n - 1)) - { + while (find(0) != find(m * n - 1)) { auto [score, i, j] = scores.back(); scores.pop_back(); ans = min(ans, score); vis[i][j] = true; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && vis[x][y]) p[find(x * n + y)] = find(i * n + j); diff --git a/solution/1100-1199/1103.Distribute Candies to People/Solution.cpp b/solution/1100-1199/1103.Distribute Candies to People/Solution.cpp index 36f62406df908..5745f8d568468 100644 --- a/solution/1100-1199/1103.Distribute Candies to People/Solution.cpp +++ b/solution/1100-1199/1103.Distribute Candies to People/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: vector distributeCandies(int candies, int num_people) { vector ans(num_people); - for (int i = 0; candies > 0; ++i) - { + for (int i = 0; candies > 0; ++i) { ans[i % num_people] += min(candies, i + 1); candies -= min(candies, i + 1); } diff --git a/solution/1100-1199/1108.Defanging an IP Address/Solution.cpp b/solution/1100-1199/1108.Defanging an IP Address/Solution.cpp index 46706670fc19b..c0becaccaef98 100644 --- a/solution/1100-1199/1108.Defanging an IP Address/Solution.cpp +++ b/solution/1100-1199/1108.Defanging an IP Address/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: string defangIPaddr(string address) { - for (int i = address.size(); i >= 0; --i){ + for (int i = address.size(); i >= 0; --i) { if (address[i] == '.') { address.replace(i, 1, "[.]"); } diff --git a/solution/1100-1199/1109.Corporate Flight Bookings/Solution.cpp b/solution/1100-1199/1109.Corporate Flight Bookings/Solution.cpp index 3b70fcb849148..a39439a603ece 100644 --- a/solution/1100-1199/1109.Corporate Flight Bookings/Solution.cpp +++ b/solution/1100-1199/1109.Corporate Flight Bookings/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: vector corpFlightBookings(vector>& bookings, int n) { vector delta(n); - for (auto &booking : bookings) { + for (auto& booking : bookings) { int first = booking[0], last = booking[1], seats = booking[2]; delta[first - 1] += seats; if (last < n) { diff --git a/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/Solution.cpp b/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/Solution.cpp index f274ee5360faf..317a932e815c6 100644 --- a/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/Solution.cpp +++ b/solution/1100-1199/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/Solution.cpp @@ -4,18 +4,18 @@ class Solution { int n = seq.size(); vector ans(n); int a = 0, b = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { char c = seq[i]; - if (c == '(') - { - if (a < b) ++a; - else ++b, ans[i] = 1; - } - else - { - if (a > b) --a; - else --b, ans[i] = 1; + if (c == '(') { + if (a < b) + ++a; + else + ++b, ans[i] = 1; + } else { + if (a > b) + --a; + else + --b, ans[i] = 1; } } return ans; diff --git a/solution/1100-1199/1115.Print FooBar Alternately/Solution.cpp b/solution/1100-1199/1115.Print FooBar Alternately/Solution.cpp index 7df58bd6647b6..34239ffa5b7bf 100644 --- a/solution/1100-1199/1115.Print FooBar Alternately/Solution.cpp +++ b/solution/1100-1199/1115.Print FooBar Alternately/Solution.cpp @@ -12,7 +12,7 @@ class FooBar { void foo(function printFoo) { for (int i = 0; i < n; i++) { fooMu.lock(); - printFoo(); + printFoo(); barMu.unlock(); } } @@ -20,7 +20,7 @@ class FooBar { void bar(function printBar) { for (int i = 0; i < n; i++) { barMu.lock(); - printBar(); + printBar(); fooMu.unlock(); } } diff --git a/solution/1100-1199/1116.Print Zero Even Odd/Solution.cpp b/solution/1100-1199/1116.Print Zero Even Odd/Solution.cpp index 72e487922275d..4faa01789aedc 100644 --- a/solution/1100-1199/1116.Print Zero Even Odd/Solution.cpp +++ b/solution/1100-1199/1116.Print Zero Even Odd/Solution.cpp @@ -2,7 +2,7 @@ class ZeroEvenOdd { private: int n; int flag; - mutex m1,m2,m3; + mutex m1, m2, m3; public: ZeroEvenOdd(int n) { @@ -16,17 +16,18 @@ class ZeroEvenOdd { // printNumber(x) outputs "x", where x is an integer. void zero(function printNumber) { m3.unlock(); - for(int i = 0; i < n ;i++){ + for (int i = 0; i < n; i++) { m3.lock(); printNumber(0); - if(flag == 1)flag = 0,m2.unlock(); - else flag = 1,m1.unlock(); + if (flag == 1) + flag = 0, m2.unlock(); + else + flag = 1, m1.unlock(); } } - void odd(function printNumber) { //输出奇数 - for(int i = 1;i <= n; i+=2){ + for (int i = 1; i <= n; i += 2) { m2.lock(); printNumber(i); m3.unlock(); @@ -34,7 +35,7 @@ class ZeroEvenOdd { } void even(function printNumber) { //输出偶数 - for(int i = 2;i <= n; i+=2){ + for (int i = 2; i <= n; i += 2) { m1.lock(); printNumber(i); m3.unlock(); diff --git a/solution/1100-1199/1117.Building H2O/Solution.cpp b/solution/1100-1199/1117.Building H2O/Solution.cpp index 27fd4ace919f3..bf715554b4ffc 100644 --- a/solution/1100-1199/1117.Building H2O/Solution.cpp +++ b/solution/1100-1199/1117.Building H2O/Solution.cpp @@ -1,7 +1,8 @@ class H2O { private: int n_h; - mutex m_h,m_o; + mutex m_h, m_o; + public: H2O() { m_o.lock(); @@ -12,8 +13,10 @@ class H2O { m_h.lock(); releaseHydrogen(); n_h--; - if(n_h > 0)m_h.unlock(); - else m_o.unlock(); + if (n_h > 0) + m_h.unlock(); + else + m_o.unlock(); } void oxygen(function releaseOxygen) { diff --git a/solution/1100-1199/1122.Relative Sort Array/Solution.cpp b/solution/1100-1199/1122.Relative Sort Array/Solution.cpp index a6e744fc7b430..6b3c5fdd5b2c8 100644 --- a/solution/1100-1199/1122.Relative Sort Array/Solution.cpp +++ b/solution/1100-1199/1122.Relative Sort Array/Solution.cpp @@ -4,12 +4,10 @@ class Solution { vector mp(1001); for (int x : arr1) ++mp[x]; int i = 0; - for (int x : arr2) - { + for (int x : arr2) { while (mp[x]-- > 0) arr1[i++] = x; } - for (int j = 0; j < mp.size(); ++j) - { + for (int j = 0; j < mp.size(); ++j) { while (mp[j]-- > 0) arr1[i++] = j; } return arr1; diff --git a/solution/1100-1199/1124.Longest Well-Performing Interval/Solution.cpp b/solution/1100-1199/1124.Longest Well-Performing Interval/Solution.cpp index 23e49c27cbbcb..285c5024a1f6b 100644 --- a/solution/1100-1199/1124.Longest Well-Performing Interval/Solution.cpp +++ b/solution/1100-1199/1124.Longest Well-Performing Interval/Solution.cpp @@ -3,12 +3,11 @@ class Solution { int longestWPI(vector& hours) { int s = 0, ans = 0; unordered_map seen; - for (int i = 0; i < hours.size(); ++i) - { + for (int i = 0; i < hours.size(); ++i) { s += hours[i] > 8 ? 1 : -1; - if (s > 0) ans = i + 1; - else - { + if (s > 0) + ans = i + 1; + else { if (!seen.count(s)) seen[s] = i; if (seen.count(s - 1)) ans = max(ans, i - seen[s - 1]); } diff --git a/solution/1100-1199/1128.Number of Equivalent Domino Pairs/Solution.cpp b/solution/1100-1199/1128.Number of Equivalent Domino Pairs/Solution.cpp index 85863fb84c789..6929ffb021bdf 100644 --- a/solution/1100-1199/1128.Number of Equivalent Domino Pairs/Solution.cpp +++ b/solution/1100-1199/1128.Number of Equivalent Domino Pairs/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int numEquivDominoPairs(vector>& dominoes) { vector counter(100); int ans = 0; - for (auto& d : dominoes) - { + for (auto& d : dominoes) { int v = d[0] > d[1] ? d[0] * 10 + d[1] : d[1] * 10 + d[0]; ans += counter[v]; ++counter[v]; diff --git a/solution/1100-1199/1129.Shortest Path with Alternating Colors/Solution.cpp b/solution/1100-1199/1129.Shortest Path with Alternating Colors/Solution.cpp index ae6d8eb5cfa97..f9a0391509a6d 100644 --- a/solution/1100-1199/1129.Shortest Path with Alternating Colors/Solution.cpp +++ b/solution/1100-1199/1129.Shortest Path with Alternating Colors/Solution.cpp @@ -10,11 +10,9 @@ class Solution { q.push({0, false}); int d = -1; vector ans(n, -1); - while (!q.empty()) - { + while (!q.empty()) { ++d; - for (int t = q.size(); t > 0; --t) - { + for (int t = q.size(); t > 0; --t) { auto p = q.front(); q.pop(); int i = p.first; @@ -24,8 +22,7 @@ class Solution { vis[i] = true; vector& ne = b ? red[i] : blue[i]; vector& v = b ? visRed : visBlue; - for (int j : ne) - { + for (int j : ne) { if (v[j]) continue; v[j] = true; q.push({j, !b}); diff --git a/solution/1100-1199/1135.Connecting Cities With Minimum Cost/Solution.cpp b/solution/1100-1199/1135.Connecting Cities With Minimum Cost/Solution.cpp index cc671b55af7cb..c829f82e642c6 100644 --- a/solution/1100-1199/1135.Connecting Cities With Minimum Cost/Solution.cpp +++ b/solution/1100-1199/1135.Connecting Cities With Minimum Cost/Solution.cpp @@ -5,10 +5,9 @@ class Solution { int minimumCost(int n, vector>& connections) { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; - sort(connections.begin(), connections.end(), [](auto& a, auto& b) {return a[2] < b[2];}); + sort(connections.begin(), connections.end(), [](auto& a, auto& b) { return a[2] < b[2]; }); int ans = 0; - for (auto& e : connections) - { + for (auto& e : connections) { int x = e[0] - 1, y = e[1] - 1, cost = e[2]; if (find(x) == find(y)) continue; p[find(x)] = find(y); diff --git a/solution/1100-1199/1136.Parallel Courses/Solution.cpp b/solution/1100-1199/1136.Parallel Courses/Solution.cpp index de6929fbd8bcb..dc12eb5159a7a 100644 --- a/solution/1100-1199/1136.Parallel Courses/Solution.cpp +++ b/solution/1100-1199/1136.Parallel Courses/Solution.cpp @@ -3,24 +3,23 @@ class Solution { int minimumSemesters(int n, vector>& relations) { vector> g(n); vector indeg(n); - for (auto& r : relations) - { + for (auto& r : relations) { int a = r[0] - 1, b = r[1] - 1; g[a].push_back(b); ++indeg[b]; } queue q; - for (int i = 0; i < n; ++i) if (indeg[i] == 0) q.push(i); + for (int i = 0; i < n; ++i) + if (indeg[i] == 0) q.push(i); int ans = 0; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int k = q.size(); k; --k) - { + for (int k = q.size(); k; --k) { int i = q.front(); q.pop(); --n; - for (int j : g[i]) if (--indeg[j] == 0) q.push(j); + for (int j : g[i]) + if (--indeg[j] == 0) q.push(j); } } return n == 0 ? ans : -1; diff --git a/solution/1100-1199/1137.N-th Tribonacci Number/Solution.cpp b/solution/1100-1199/1137.N-th Tribonacci Number/Solution.cpp index 03324e8183c79..a66bfd56d5b03 100644 --- a/solution/1100-1199/1137.N-th Tribonacci Number/Solution.cpp +++ b/solution/1100-1199/1137.N-th Tribonacci Number/Solution.cpp @@ -8,6 +8,6 @@ class Solution { b = c; c = d; } - return (int) a; + return (int)a; } }; \ No newline at end of file diff --git a/solution/1100-1199/1143.Longest Common Subsequence/Solution.cpp b/solution/1100-1199/1143.Longest Common Subsequence/Solution.cpp index 42ac4d2e98a3f..179eda9e21bf8 100644 --- a/solution/1100-1199/1143.Longest Common Subsequence/Solution.cpp +++ b/solution/1100-1199/1143.Longest Common Subsequence/Solution.cpp @@ -3,12 +3,12 @@ class Solution { int longestCommonSubsequence(string text1, string text2) { int m = text1.size(), n = text2.size(); vector> dp(m + 1, vector(n + 1)); - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { - if (text1[i - 1] == text2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; - else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (text1[i - 1] == text2[j - 1]) + dp[i][j] = dp[i - 1][j - 1] + 1; + else + dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } return dp[m][n]; diff --git a/solution/1100-1199/1160.Find Words That Can Be Formed by Characters/Solution.cpp b/solution/1100-1199/1160.Find Words That Can Be Formed by Characters/Solution.cpp index 101b474174290..d2be851c46f38 100644 --- a/solution/1100-1199/1160.Find Words That Can Be Formed by Characters/Solution.cpp +++ b/solution/1100-1199/1160.Find Words That Can Be Formed by Characters/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int countCharacters(vector& words, string chars) { vector counter = count(chars); int ans = 0; - for (auto& word : words) - { + for (auto& word : words) { vector cnt = count(word); if (check(counter, cnt)) ans += word.size(); } diff --git a/solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/Solution.cpp b/solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/Solution.cpp index e38ec7131b54c..6d22342cdc37b 100644 --- a/solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/Solution.cpp +++ b/solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/Solution.cpp @@ -12,16 +12,14 @@ class Solution { public: int maxLevelSum(TreeNode* root) { - queue q{{root}}; + queue q {{root}}; int mx = INT_MIN; int ans = 0; int i = 0; - while (!q.empty()) - { + while (!q.empty()) { ++i; int s = 0; - for (int n = q.size(); n; --n) - { + for (int n = q.size(); n; --n) { root = q.front(); q.pop(); s += root->val; diff --git a/solution/1100-1199/1162.As Far from Land as Possible/Solution.cpp b/solution/1100-1199/1162.As Far from Land as Possible/Solution.cpp index 2ef34798d1912..a7db0aac16c73 100644 --- a/solution/1100-1199/1162.As Far from Land as Possible/Solution.cpp +++ b/solution/1100-1199/1162.As Far from Land as Possible/Solution.cpp @@ -11,19 +11,15 @@ class Solution { int ans = -1; bool valid = false; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int k = q.size(); k > 0; --k) - { + for (int k = q.size(); k > 0; --k) { pii p = q.front(); q.pop(); - for (int i = 0; i < 4; ++i) - { + for (int i = 0; i < 4; ++i) { int x = p.first + dirs[i]; int y = p.second + dirs[i + 1]; - if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) - { + if (x >= 0 && x < n && y >= 0 && y < n && grid[x][y] == 0) { valid = true; grid[x][y] = 1; q.push({x, y}); diff --git a/solution/1100-1199/1165.Single-Row Keyboard/Solution.cpp b/solution/1100-1199/1165.Single-Row Keyboard/Solution.cpp index 508aaf29acb41..4f713d41e818e 100644 --- a/solution/1100-1199/1165.Single-Row Keyboard/Solution.cpp +++ b/solution/1100-1199/1165.Single-Row Keyboard/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: int calculateTime(string keyboard, string word) { - unordered_map index; + unordered_map index; for (int i = 0; i < keyboard.size(); ++i) { index[keyboard[i]] = i; } diff --git a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.cpp b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.cpp index 3638890193e7a..2bdab8179ea46 100644 --- a/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.cpp +++ b/solution/1100-1199/1167.Minimum Cost to Connect Sticks/Solution.cpp @@ -1,11 +1,10 @@ class Solution { public: int connectSticks(vector& sticks) { - priority_queue , greater > pq; - for (int x: sticks) pq.push(x); + priority_queue, greater> pq; + for (int x : sticks) pq.push(x); int res = 0; - while (pq.size() > 1) - { + while (pq.size() > 1) { int val = pq.top(); pq.pop(); val += pq.top(); diff --git a/solution/1100-1199/1168.Optimize Water Distribution in a Village/Solution.cpp b/solution/1100-1199/1168.Optimize Water Distribution in a Village/Solution.cpp index d3b8a34e9926a..353e97910affe 100644 --- a/solution/1100-1199/1168.Optimize Water Distribution in a Village/Solution.cpp +++ b/solution/1100-1199/1168.Optimize Water Distribution in a Village/Solution.cpp @@ -10,8 +10,7 @@ class Solution { return a[2] < b[2]; }); int res = 0; - for (auto e : pipes) - { + for (auto e : pipes) { if (find(e[0]) == find(e[1])) continue; p[find(e[0])] = find(e[1]); res += e[2]; diff --git a/solution/1100-1199/1175.Prime Arrangements/Solution.cpp b/solution/1100-1199/1175.Prime Arrangements/Solution.cpp index 42e08ceb164f0..a760e05152f1c 100644 --- a/solution/1100-1199/1175.Prime Arrangements/Solution.cpp +++ b/solution/1100-1199/1175.Prime Arrangements/Solution.cpp @@ -6,7 +6,7 @@ class Solution { int numPrimeArrangements(int n) { int cnt = count(n); ll ans = f(cnt) * f(n - cnt); - return (int) (ans % MOD); + return (int)(ans % MOD); } ll f(int n) { @@ -18,10 +18,8 @@ class Solution { int count(int n) { vector primes(n + 1, true); int cnt = 0; - for (int i = 2; i <= n; ++i) - { - if (primes[i]) - { + for (int i = 2; i <= n; ++i) { + if (primes[i]) { ++cnt; for (int j = i + i; j <= n; j += i) primes[j] = false; } diff --git a/solution/1100-1199/1180.Count Substrings with Only One Distinct Letter/Solution.cpp b/solution/1100-1199/1180.Count Substrings with Only One Distinct Letter/Solution.cpp index 4b7d667b2fc69..90b5359b386db 100644 --- a/solution/1100-1199/1180.Count Substrings with Only One Distinct Letter/Solution.cpp +++ b/solution/1100-1199/1180.Count Substrings with Only One Distinct Letter/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int countLetters(string s) { int ans = 0; - for (int i = 0, n = s.size(); i < n;) - { + for (int i = 0, n = s.size(); i < n;) { int j = i; while (j < n && s[j] == s[i]) ++j; ans += (1 + j - i) * (j - i) / 2; diff --git a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp index 7a9a3e8b68e2f..2fff4b72927a0 100644 --- a/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp +++ b/solution/1100-1199/1184.Distance Between Bus Stops/Solution.cpp @@ -3,10 +3,11 @@ class Solution { int distanceBetweenBusStops(vector& distance, int start, int destination) { if (start > destination) return distanceBetweenBusStops(distance, destination, start); int a = 0, b = 0; - for (int i = 0; i < distance.size(); ++i) - { - if (i >= start && i < destination) a += distance[i]; - else b += distance[i]; + for (int i = 0; i < distance.size(); ++i) { + if (i >= start && i < destination) + a += distance[i]; + else + b += distance[i]; } return min(a, b); } diff --git a/solution/1100-1199/1185.Day of the Week/Solution.cpp b/solution/1100-1199/1185.Day of the Week/Solution.cpp index 70c423ff96ca3..93857cc219cd1 100644 --- a/solution/1100-1199/1185.Day of the Week/Solution.cpp +++ b/solution/1100-1199/1185.Day of the Week/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: string dayOfTheWeek(int d, int m, int y) { - if (m < 3) - { + if (m < 3) { m += 12; y -= 1; } diff --git a/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp index 59141210279a1..f6f049e38e2f1 100644 --- a/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp +++ b/solution/1100-1199/1195.Fizz Buzz Multithreaded/Solution.cpp @@ -25,7 +25,7 @@ class FizzBuzz { void buzz(function printBuzz) { while (index <= n) { std::lock_guard lk(mtx); - if (0 == index % 5 && 0 != index % 3 && index <= n){ + if (0 == index % 5 && 0 != index % 3 && index <= n) { printBuzz(); index++; } @@ -39,13 +39,13 @@ class FizzBuzz { if (0 == index % 15 && index <= n) { printFizzBuzz(); index++; - } + } } } // printNumber(x) outputs "x", where x is an integer. void number(function printNumber) { - while (index <= n) { + while (index <= n) { std::lock_guard lk(mtx); if (0 != index % 3 && 0 != index % 5 && index <= n) { printNumber(index); diff --git a/solution/1100-1199/1196.How Many Apples Can You Put into the Basket/Solution.cpp b/solution/1100-1199/1196.How Many Apples Can You Put into the Basket/Solution.cpp index 3a7b49d80fecb..f01c91bfb418a 100644 --- a/solution/1100-1199/1196.How Many Apples Can You Put into the Basket/Solution.cpp +++ b/solution/1100-1199/1196.How Many Apples Can You Put into the Basket/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int maxNumberOfApples(vector& weight) { sort(weight.begin(), weight.end()); int ans = 0, t = 0; - for (int v : weight) - { + for (int v : weight) { if (t + v > 5000) break; t += v; ++ans; diff --git a/solution/1100-1199/1197.Minimum Knight Moves/Solution.cpp b/solution/1100-1199/1197.Minimum Knight Moves/Solution.cpp index b0c3829c48f01..4fe09da86465a 100644 --- a/solution/1100-1199/1197.Minimum Knight Moves/Solution.cpp +++ b/solution/1100-1199/1197.Minimum Knight Moves/Solution.cpp @@ -9,18 +9,14 @@ class Solution { vector> vis(700, vector(700)); vis[310][310] = true; vector> dirs = {{-2, 1}, {-1, 2}, {1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}}; - while (!q.empty()) - { - for (int k = q.size(); k > 0; --k) - { + while (!q.empty()) { + for (int k = q.size(); k > 0; --k) { auto p = q.front(); q.pop(); if (p.first == x && p.second == y) return ans; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int c = p.first + dir[0], d = p.second + dir[1]; - if (!vis[c][d]) - { + if (!vis[c][d]) { vis[c][d] = true; q.push({c, d}); } diff --git a/solution/1200-1299/1200.Minimum Absolute Difference/Solution.cpp b/solution/1200-1299/1200.Minimum Absolute Difference/Solution.cpp index f7e0602a2aade..45346bc1388da 100644 --- a/solution/1200-1299/1200.Minimum Absolute Difference/Solution.cpp +++ b/solution/1200-1299/1200.Minimum Absolute Difference/Solution.cpp @@ -5,17 +5,15 @@ class Solution { int mi = INT_MAX; int n = arr.size(); vector> ans; - for (int i = 0; i < n - 1; ++i) - { + for (int i = 0; i < n - 1; ++i) { int a = arr[i], b = arr[i + 1]; int d = b - a; - if (d < mi) - { + if (d < mi) { mi = d; ans.clear(); ans.push_back({a, b}); - } - else if (d == mi) ans.push_back({a, b}); + } else if (d == mi) + ans.push_back({a, b}); } return ans; } diff --git a/solution/1200-1299/1202.Smallest String With Swaps/Solution.cpp b/solution/1200-1299/1202.Smallest String With Swaps/Solution.cpp index ce90cf4581301..bde79c3eeb061 100644 --- a/solution/1200-1299/1202.Smallest String With Swaps/Solution.cpp +++ b/solution/1200-1299/1202.Smallest String With Swaps/Solution.cpp @@ -11,8 +11,7 @@ class Solution { for (int i = 0; i < n; ++i) mp[find(i)].push_back(s[i]); for (auto& [k, v] : mp) sort(v.rbegin(), v.rend()); string ans; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { ans.push_back(mp[find(i)].back()); mp[find(i)].pop_back(); } diff --git a/solution/1200-1299/1206.Design Skiplist/Solution.cpp b/solution/1200-1299/1206.Design Skiplist/Solution.cpp index ee2657e83bdb5..ca993de10b011 100644 --- a/solution/1200-1299/1206.Design Skiplist/Solution.cpp +++ b/solution/1200-1299/1206.Design Skiplist/Solution.cpp @@ -1,7 +1,9 @@ struct Node { int val; vector next; - Node(int v, int level) : val(v), next(level, nullptr) {} + Node(int v, int level) + : val(v) + , next(level, nullptr) { } }; class Skiplist { @@ -15,41 +17,36 @@ class Skiplist { head = new Node(-1, maxLevel); level = 0; } - + bool search(int target) { Node* curr = head; - for (int i = level - 1; ~i; --i) - { + for (int i = level - 1; ~i; --i) { curr = findClosest(curr, i, target); if (curr->next[i] && curr->next[i]->val == target) return true; } return false; } - + void add(int num) { Node* curr = head; int lv = randomLevel(); Node* node = new Node(num, lv); level = max(level, lv); - for (int i = level - 1; ~i; --i) - { + for (int i = level - 1; ~i; --i) { curr = findClosest(curr, i, num); - if (i < lv) - { + if (i < lv) { node->next[i] = curr->next[i]; curr->next[i] = node; } } } - + bool erase(int num) { Node* curr = head; bool ok = false; - for (int i = level - 1; ~i; --i) - { + for (int i = level - 1; ~i; --i) { curr = findClosest(curr, i, num); - if (curr->next[i] && curr->next[i]->val == num) - { + if (curr->next[i] && curr->next[i]->val == num) { curr->next[i] = curr->next[i]->next[i]; ok = true; } diff --git a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.cpp b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.cpp index 3277f7336e8e4..639a94e709bad 100644 --- a/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.cpp +++ b/solution/1200-1299/1208.Get Equal Substrings Within Budget/Solution.cpp @@ -5,19 +5,19 @@ class Solution { vector presum(n + 1); for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + abs(s[i] - t[i]); int left = 0, right = n; - while (left < right) - { + while (left < right) { int mid = left + right + 1 >> 1; - if (check(mid, presum, maxCost, n)) left = mid; - else right = mid - 1; + if (check(mid, presum, maxCost, n)) + left = mid; + else + right = mid - 1; } return left; } bool check(int l, vector& s, int maxCost, int n) { int i = 0; - while (i + l - 1 < n) - { + while (i + l - 1 < n) { int j = i + l - 1; if (s[j + 1] - s[i] <= maxCost) return true; ++i; diff --git a/solution/1200-1299/1210.Minimum Moves to Reach Target with Rotations/Solution.cpp b/solution/1200-1299/1210.Minimum Moves to Reach Target with Rotations/Solution.cpp index c8dff66e4d2c6..4cf1c7a3084d5 100644 --- a/solution/1200-1299/1210.Minimum Moves to Reach Target with Rotations/Solution.cpp +++ b/solution/1200-1299/1210.Minimum Moves to Reach Target with Rotations/Solution.cpp @@ -8,23 +8,19 @@ class Solution { vector> vis(n * n, vector(n * n)); int ans = 0; vis[0][1] = true; - while (!q.empty()) - { - for (int k = q.size(); k; --k) - { + while (!q.empty()) { + for (int k = q.size(); k; --k) { auto p = q.front(); if (p == target) return ans; q.pop(); int a = p[0], b = p[1]; int i1 = a / n, j1 = a % n; int i2 = b / n, j2 = b % n; - if (j1 + 1 < n && j2 + 1 < n && grid[i1][j1 + 1] == 0 && grid[i2][j2 + 1] == 0) - { + if (j1 + 1 < n && j2 + 1 < n && grid[i1][j1 + 1] == 0 && grid[i2][j2 + 1] == 0) { check(i1 * n + j1 + 1, i2 * n + j2 + 1, q, vis); if (j1 == j2) check(a, i1 * n + j2 + 1, q, vis); } - if (i1 + 1 < n && i2 + 1 < n && grid[i1 + 1][j1] == 0 && grid[i2 + 1][j2] == 0) - { + if (i1 + 1 < n && i2 + 1 < n && grid[i1 + 1][j1] == 0 && grid[i2 + 1][j2] == 0) { check((i1 + 1) * n + j1, (i2 + 1) * n + j2, q, vis); if (i1 == i2) check(a, (i2 + 1) * n + j1, q, vis); } @@ -35,8 +31,7 @@ class Solution { } void check(int a, int b, queue>& q, vector>& vis) { - if (!vis[a][b]) - { + if (!vis[a][b]) { vis[a][b] = true; q.push({a, b}); } diff --git a/solution/1200-1299/1214.Two Sum BSTs/Solution.cpp b/solution/1200-1299/1214.Two Sum BSTs/Solution.cpp index 29d3df69a3f86..3c6d082c1c93d 100644 --- a/solution/1200-1299/1214.Two Sum BSTs/Solution.cpp +++ b/solution/1200-1299/1214.Two Sum BSTs/Solution.cpp @@ -11,13 +11,12 @@ */ class Solution { public: - bool twoSumBSTs(TreeNode *root1, TreeNode *root2, int target) { + bool twoSumBSTs(TreeNode* root1, TreeNode* root2, int target) { vector vals1, vals2; inorder(root1, vals1); inorder(root2, vals2); int i = 0, j = vals2.size() - 1; - while (i < vals1.size() && j >= 0) - { + while (i < vals1.size() && j >= 0) { int s = vals1[i] + vals2[j]; if (s == target) return true; @@ -29,9 +28,8 @@ class Solution { return false; } - void inorder(TreeNode *root, vector &vals) { - if (root) - { + void inorder(TreeNode* root, vector& vals) { + if (root) { inorder(root->left, vals); vals.push_back(root->val); inorder(root->right, vals); diff --git a/solution/1200-1299/1215.Stepping Numbers/Solution.cpp b/solution/1200-1299/1215.Stepping Numbers/Solution.cpp index 32d5074add169..c847744f1743a 100644 --- a/solution/1200-1299/1215.Stepping Numbers/Solution.cpp +++ b/solution/1200-1299/1215.Stepping Numbers/Solution.cpp @@ -5,8 +5,7 @@ class Solution { if (low == 0) ans.push_back(0); queue q; for (int i = 1; i < 10; ++i) q.push(i); - while (!q.empty()) - { + while (!q.empty()) { int v = q.front(); q.pop(); if (v > high) break; diff --git a/solution/1200-1299/1221.Split a String in Balanced Strings/Solution.cpp b/solution/1200-1299/1221.Split a String in Balanced Strings/Solution.cpp index 6751c3b3a35a4..af1c24b6f1d07 100644 --- a/solution/1200-1299/1221.Split a String in Balanced Strings/Solution.cpp +++ b/solution/1200-1299/1221.Split a String in Balanced Strings/Solution.cpp @@ -2,10 +2,11 @@ class Solution { public: int balancedStringSplit(string s) { int ans = 0, l = 0; - for (char c : s) - { - if (c == 'L') ++l; - else --l; + for (char c : s) { + if (c == 'L') + ++l; + else + --l; if (l == 0) ++ans; } return ans; diff --git a/solution/1200-1299/1222.Queens That Can Attack the King/Solution.cpp b/solution/1200-1299/1222.Queens That Can Attack the King/Solution.cpp index aa5161a57644d..ef516e02f98aa 100644 --- a/solution/1200-1299/1222.Queens That Can Attack the King/Solution.cpp +++ b/solution/1200-1299/1222.Queens That Can Attack the King/Solution.cpp @@ -4,18 +4,15 @@ class Solution { unordered_set s; int n = 8; for (auto& queen : queens) s.insert(queen[0] * n + queen[1]); - vector> dirs = {{0, 1}, {0, - 1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; + vector> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}}; vector> ans; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int x = king[0], y = king[1]; int a = dir[0], b = dir[1]; - while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n) - { + while (x + a >= 0 && x + a < n && y + b >= 0 && y + b < n) { x += a; y += b; - if (s.count(x * n + y)) - { + if (s.count(x * n + y)) { ans.push_back({x, y}); break; } diff --git a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.cpp b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.cpp index 9e9a83b82147f..e6e938d6ab51b 100644 --- a/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.cpp +++ b/solution/1200-1299/1228.Missing Number In Arithmetic Progression/Solution.cpp @@ -3,7 +3,8 @@ class Solution { int missingNumber(vector& arr) { int n = arr.size(); int d = (arr[n - 1] - arr[0]) / n; - for (int i = 1; i < n; ++i) if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d; + for (int i = 1; i < n; ++i) + if (arr[i] != arr[i - 1] + d) return arr[i - 1] + d; return arr[0]; } }; \ No newline at end of file diff --git a/solution/1200-1299/1230.Toss Strange Coins/Solution.cpp b/solution/1200-1299/1230.Toss Strange Coins/Solution.cpp index 603ee482f124b..7cd1f8ac5c089 100644 --- a/solution/1200-1299/1230.Toss Strange Coins/Solution.cpp +++ b/solution/1200-1299/1230.Toss Strange Coins/Solution.cpp @@ -3,10 +3,8 @@ class Solution { double probabilityOfHeads(vector& prob, int target) { vector dp(target + 1); dp[0] = 1; - for (auto v : prob) - { - for (int j = target; j >= 0; --j) - { + for (auto v : prob) { + for (int j = target; j >= 0; --j) { dp[j] *= (1 - v); if (j >= 1) dp[j] += dp[j - 1] * v; } diff --git a/solution/1200-1299/1236.Web Crawler/Solution.cpp b/solution/1200-1299/1236.Web Crawler/Solution.cpp index b26250cb1027a..ba89d08700fda 100644 --- a/solution/1200-1299/1236.Web Crawler/Solution.cpp +++ b/solution/1200-1299/1236.Web Crawler/Solution.cpp @@ -29,8 +29,7 @@ class Solution { string host(string url) { int i = 7; string res; - for (; i < url.size(); ++i) - { + for (; i < url.size(); ++i) { if (url[i] == '/') break; res += url[i]; } diff --git a/solution/1200-1299/1245.Tree Diameter/Solution.cpp b/solution/1200-1299/1245.Tree Diameter/Solution.cpp index 9eb6d88f7ece4..d7e0d367b8103 100644 --- a/solution/1200-1299/1245.Tree Diameter/Solution.cpp +++ b/solution/1200-1299/1245.Tree Diameter/Solution.cpp @@ -6,8 +6,7 @@ class Solution { int next; int treeDiameter(vector>& edges) { - for (auto& e : edges) - { + for (auto& e : edges) { g[e[0]].insert(e[1]); g[e[1]].insert(e[0]); } @@ -24,8 +23,7 @@ class Solution { void dfs(int u, int t) { if (vis[u]) return; vis[u] = true; - if (ans < t) - { + if (ans < t) { ans = t; next = u; } diff --git a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp index 485ca6186317e..68dddddc2c4ab 100644 --- a/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp +++ b/solution/1200-1299/1252.Cells with Odd Values in a Matrix/Solution.cpp @@ -3,14 +3,14 @@ class Solution { int oddCells(int m, int n, vector>& indices) { vector row(m); vector col(n); - for (auto& e : indices) - { + for (auto& e : indices) { int r = e[0], c = e[1]; row[r]++; col[c]++; } int ans = 0; - for (int i : row) for (int j : col) ans += (i + j) % 2; + for (int i : row) + for (int j : col) ans += (i + j) % 2; return ans; } }; \ No newline at end of file diff --git a/solution/1200-1299/1254.Number of Closed Islands/Solution.cpp b/solution/1200-1299/1254.Number of Closed Islands/Solution.cpp index e2fe4ec501e58..37da948ae70cc 100644 --- a/solution/1200-1299/1254.Number of Closed Islands/Solution.cpp +++ b/solution/1200-1299/1254.Number of Closed Islands/Solution.cpp @@ -6,10 +6,8 @@ class Solution { int m = grid.size(), n = grid[0].size(); p.resize(m * n); for (int i = 0; i < m * n; ++i) p[i] = i; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (grid[i][j] == 1) continue; int idx = i * n + j; if (i < m - 1 && grid[i + 1][j] == 0) p[find(idx)] = find((i + 1) * n + j); @@ -17,25 +15,20 @@ class Solution { } } vector s(m * n, false); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (grid[i][j] == 0) s[find(i * n + j)] = true; } } - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int root = find(i * n + j); if (!s[root]) continue; if (i == 0 || i == m - 1 || j == 0 || j == n - 1) s[root] = false; } } int res = 0; - for (auto e : s) - { + for (auto e : s) { if (e) ++res; } return res; diff --git a/solution/1200-1299/1257.Smallest Common Region/Solution.cpp b/solution/1200-1299/1257.Smallest Common Region/Solution.cpp index 89f7a68fac0b6..594fdfe20fa72 100644 --- a/solution/1200-1299/1257.Smallest Common Region/Solution.cpp +++ b/solution/1200-1299/1257.Smallest Common Region/Solution.cpp @@ -6,13 +6,11 @@ class Solution { for (int i = 1; i < region.size(); ++i) m[region[i]] = region[0]; unordered_set s; - while (m.count(region1)) - { + while (m.count(region1)) { s.insert(region1); region1 = m[region1]; } - while (m.count(region2)) - { + while (m.count(region2)) { if (s.count(region2)) return region2; region2 = m[region2]; } diff --git a/solution/1200-1299/1260.Shift 2D Grid/Solution.cpp b/solution/1200-1299/1260.Shift 2D Grid/Solution.cpp index c80a1caaf4b18..d15c5b084cd39 100644 --- a/solution/1200-1299/1260.Shift 2D Grid/Solution.cpp +++ b/solution/1200-1299/1260.Shift 2D Grid/Solution.cpp @@ -3,10 +3,8 @@ class Solution { vector> shiftGrid(vector>& grid, int k) { int m = grid.size(), n = grid[0].size(); vector> ans(m, vector(n)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int t = (i * n + j + k) % (m * n); ans[t / n][t % n] = grid[i][j]; } diff --git a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp index b644b3949815d..76b6d74dde6a7 100644 --- a/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp +++ b/solution/1200-1299/1261.Find Elements in a Contaminated Binary Tree/Solution.cpp @@ -19,20 +19,18 @@ class FindElements { nodes.insert(0); dfs(root); } - + bool find(int target) { return nodes.count(target); } void dfs(TreeNode* root) { if (!root) return; - if (root->left) - { + if (root->left) { root->left->val = root->val * 2 + 1; nodes.insert(root->left->val); } - if (root->right) - { + if (root->right) { root->right->val = root->val * 2 + 2; nodes.insert(root->right->val); } diff --git a/solution/1200-1299/1267.Count Servers that Communicate/Solution.cpp b/solution/1200-1299/1267.Count Servers that Communicate/Solution.cpp index 204b3b0fa9576..79844e5a9d2d0 100644 --- a/solution/1200-1299/1267.Count Servers that Communicate/Solution.cpp +++ b/solution/1200-1299/1267.Count Servers that Communicate/Solution.cpp @@ -4,26 +4,19 @@ class Solution { int m = grid.size(), n = grid[0].size(); vector rows(m); vector cols(n); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 1) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 1) { ++rows[i]; ++cols[j]; } } } int res = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 1) - { - if (rows[i] > 1 || cols[j] > 1) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 1) { + if (rows[i] > 1 || cols[j] > 1) { ++res; } } diff --git a/solution/1200-1299/1273.Delete Tree Nodes/Solution.cpp b/solution/1200-1299/1273.Delete Tree Nodes/Solution.cpp index 46e8f22ce2628..8168d009f3646 100644 --- a/solution/1200-1299/1273.Delete Tree Nodes/Solution.cpp +++ b/solution/1200-1299/1273.Delete Tree Nodes/Solution.cpp @@ -15,8 +15,7 @@ class Solution { } void dfs(int u) { - for (int v : g[u]) - { + for (int v : g[u]) { dfs(v); value[u] += value[v]; counter[u] += counter[v]; diff --git a/solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients/Solution.cpp b/solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients/Solution.cpp index 0c59a31bab1cf..1d2d734777ad7 100644 --- a/solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients/Solution.cpp +++ b/solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: vector numOfBurgers(int tomatoSlices, int cheeseSlices) { - int x = (tomatoSlices - 2*cheeseSlices); + int x = (tomatoSlices - 2 * cheeseSlices); if (x < 0 || x % 2 == 1) return {}; x /= 2; diff --git a/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.cpp b/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.cpp index 6940aa675a2dd..75f9da14ffd31 100644 --- a/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.cpp +++ b/solution/1200-1299/1277.Count Square Submatrices with All Ones/Solution.cpp @@ -4,13 +4,13 @@ class Solution { int m = matrix.size(), n = matrix[0].size(); int ans = 0; vector> f(m, vector(n)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (matrix[i][j] == 0) continue; - if (i == 0 || j == 0) f[i][j] = 1; - else f[i][j] = min(f[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1; + if (i == 0 || j == 0) + f[i][j] = 1; + else + f[i][j] = min(f[i - 1][j - 1], min(f[i - 1][j], f[i][j - 1])) + 1; ans += f[i][j]; } } diff --git a/solution/1200-1299/1278.Palindrome Partitioning III/Solution.cpp b/solution/1200-1299/1278.Palindrome Partitioning III/Solution.cpp index 86cb3a34a1511..4139aaff9b061 100644 --- a/solution/1200-1299/1278.Palindrome Partitioning III/Solution.cpp +++ b/solution/1200-1299/1278.Palindrome Partitioning III/Solution.cpp @@ -3,30 +3,30 @@ class Solution { int palindromePartition(string s, int k) { int len = s.length(); // cost[i][j] = min #changes needed to turn (s[i],...,s[i+j]) into a palindrome - vector> cost(len, vector(len)); + vector> cost(len, vector(len)); for (int i = 0; i < len; i++) { for (int j = i; j < len; j++) { int begin = i, end = j, cnt = 0; - while(begin < end) { + while (begin < end) { if (s[begin] != s[end]) cnt++; - begin ++, end--; + begin++, end--; } - cost[i][j-i] = cnt; + cost[i][j - i] = cnt; } } // dp[i][j] = min #changes needed to split (s[i],...,s[len]) into j+1 palindromes vector> dp(len, vector(k, INT_MAX)); for (int i = 0; i < len; i++) { - dp[i][0] = cost[i][len-1-i]; + dp[i][0] = cost[i][len - 1 - i]; } for (int kk = 1; kk < k; kk++) { for (int i = 0; i < len; i++) { - for (int j = i+1; j+kk-1 < len; j++) { - dp[i][kk] = min(dp[i][kk], dp[j][kk-1] + cost[i][j-i-1]); + for (int j = i + 1; j + kk - 1 < len; j++) { + dp[i][kk] = min(dp[i][kk], dp[j][kk - 1] + cost[i][j - i - 1]); } } } - return dp[0][k-1]; + return dp[0][k - 1]; } }; \ No newline at end of file diff --git a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.cpp b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.cpp index 54e004292be59..faf2252c54f2e 100644 --- a/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.cpp +++ b/solution/1200-1299/1283.Find the Smallest Divisor Given a Threshold/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: int smallestDivisor(vector& nums, int threshold) { int left = 1, right = 1e6; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int s = 0; for (int& v : nums) s += (v + mid - 1) / mid; - if (s <= threshold) right = mid; - else left = mid + 1; + if (s <= threshold) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/1200-1299/1284.Minimum Number of Flips to Convert Binary Matrix to Zero Matrix/Solution.cpp b/solution/1200-1299/1284.Minimum Number of Flips to Convert Binary Matrix to Zero Matrix/Solution.cpp index 3cda00650cdfb..84269b947d6e0 100644 --- a/solution/1200-1299/1284.Minimum Number of Flips to Convert Binary Matrix to Zero Matrix/Solution.cpp +++ b/solution/1200-1299/1284.Minimum Number of Flips to Convert Binary Matrix to Zero Matrix/Solution.cpp @@ -7,31 +7,27 @@ class Solution { for (int j = 0; j < n; ++j) if (mat[i][j]) state |= (1 << (i * n + j)); - queue q{{state}}; - unordered_set vis{{state}}; + queue q {{state}}; + unordered_set vis {{state}}; int ans = 0; vector dirs = {0, -1, 0, 1, 0, 0}; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { state = q.front(); if (state == 0) return ans; q.pop(); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int nxt = state; - for (int k = 0; k < 5; ++k) - { + for (int k = 0; k < 5; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x < 0 || x >= m || y < 0 || y >= n) continue; - if ((nxt & (1 << (x * n + y))) != 0) nxt -= 1 << (x * n + y); - else nxt |= 1 << (x * n + y); + if ((nxt & (1 << (x * n + y))) != 0) + nxt -= 1 << (x * n + y); + else + nxt |= 1 << (x * n + y); } - if (!vis.count(nxt)) - { + if (!vis.count(nxt)) { vis.insert(nxt); q.push(nxt); } diff --git a/solution/1200-1299/1286.Iterator for Combination/Solution.cpp b/solution/1200-1299/1286.Iterator for Combination/Solution.cpp index eb09981f5c952..2b362b77f0770 100644 --- a/solution/1200-1299/1286.Iterator for Combination/Solution.cpp +++ b/solution/1200-1299/1286.Iterator for Combination/Solution.cpp @@ -11,7 +11,7 @@ class CombinationIterator { cs = characters; size = combinationLength; } - + string next() { while (curr >= 0 && __builtin_popcount(curr) != size) --curr; string ans; @@ -24,7 +24,7 @@ class CombinationIterator { --curr; return ans; } - + bool hasNext() { while (curr >= 0 && __builtin_popcount(curr) != size) --curr; return curr >= 0; diff --git a/solution/1200-1299/1288.Remove Covered Intervals/Solution.cpp b/solution/1200-1299/1288.Remove Covered Intervals/Solution.cpp index 46b9b0cec8d87..8ff31789a0147 100644 --- a/solution/1200-1299/1288.Remove Covered Intervals/Solution.cpp +++ b/solution/1200-1299/1288.Remove Covered Intervals/Solution.cpp @@ -1,14 +1,11 @@ class Solution { public: - int removeCoveredIntervals(vector> &intervals) { - sort(intervals.begin(), intervals.end(), [](const vector &a, const vector &b) - { return a[0] == b[0] ? b[1] < a[1] : a[0] < b[0]; }); + int removeCoveredIntervals(vector>& intervals) { + sort(intervals.begin(), intervals.end(), [](const vector& a, const vector& b) { return a[0] == b[0] ? b[1] < a[1] : a[0] < b[0]; }); int cnt = 1; vector pre = intervals[0]; - for (int i = 1; i < intervals.size(); ++i) - { - if (pre[1] < intervals[i][1]) - { + for (int i = 1; i < intervals.size(); ++i) { + if (pre[1] < intervals[i][1]) { ++cnt; pre = intervals[i]; } diff --git a/solution/1200-1299/1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold/Solution.cpp b/solution/1200-1299/1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold/Solution.cpp index 88426620e18f1..3c296dc39a5dc 100644 --- a/solution/1200-1299/1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold/Solution.cpp +++ b/solution/1200-1299/1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold/Solution.cpp @@ -7,22 +7,20 @@ class Solution { for (int j = 0; j < n; ++j) s[i + 1][j + 1] = s[i][j + 1] + s[i + 1][j] - s[i][j] + mat[i][j]; int left = 0, right = min(m, n); - while (left < right) - { + while (left < right) { int mid = left + right + 1 >> 1; - if (check(mid, s, m, n, threshold)) left = mid; - else right = mid - 1; + if (check(mid, s, m, n, threshold)) + left = mid; + else + right = mid - 1; } return left; } bool check(int l, vector>& s, int m, int n, int threshold) { - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (i + l - 1 < m && j + l - 1 < n) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (i + l - 1 < m && j + l - 1 < n) { int i1 = i + l - 1, j1 = j + l - 1; int t = s[i1 + 1][j1 + 1] - s[i1 + 1][j] - s[i][j1 + 1] + s[i][j]; if (t <= threshold) return true; diff --git a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.cpp b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.cpp index a02155673c27a..4fc1c5997c6ec 100644 --- a/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.cpp +++ b/solution/1200-1299/1293.Shortest Path in a Grid with Obstacles Elimination/Solution.cpp @@ -9,27 +9,20 @@ class Solution { vis[0][0][k] = true; int ans = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { auto p = q.front(); k = p[2]; q.pop(); - for (int j = 0; j < 4; ++j) - { + for (int j = 0; j < 4; ++j) { int x = p[0] + dirs[j], y = p[1] + dirs[j + 1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { + if (x >= 0 && x < m && y >= 0 && y < n) { if (x == m - 1 && y == n - 1) return ans; - if (grid[x][y] == 0 && !vis[x][y][k]) - { + if (grid[x][y] == 0 && !vis[x][y][k]) { q.push({x, y, k}); vis[x][y][k] = true; - } - else if (grid[x][y] == 1 && k > 0 && !vis[x][y][k - 1]) - { + } else if (grid[x][y] == 1 && k > 0 && !vis[x][y][k - 1]) { q.push({x, y, k - 1}); vis[x][y][k - 1] = true; } diff --git a/solution/1200-1299/1296.Divide Array in Sets of K Consecutive Numbers/Solution.cpp b/solution/1200-1299/1296.Divide Array in Sets of K Consecutive Numbers/Solution.cpp index 5fc6f2362ad71..8a7c3e252a772 100644 --- a/solution/1200-1299/1296.Divide Array in Sets of K Consecutive Numbers/Solution.cpp +++ b/solution/1200-1299/1296.Divide Array in Sets of K Consecutive Numbers/Solution.cpp @@ -4,14 +4,14 @@ class Solution { if (nums.size() % k != 0) return false; map mp; for (int& h : nums) mp[h] += 1; - while (!mp.empty()) - { + while (!mp.empty()) { int v = mp.begin()->first; - for (int i = v; i < v + k; ++i) - { + for (int i = v; i < v + k; ++i) { if (!mp.count(i)) return false; - if (mp[i] == 1) mp.erase(i); - else mp[i] -= 1; + if (mp[i] == 1) + mp.erase(i); + else + mp[i] -= 1; } } return true; diff --git a/solution/1200-1299/1298.Maximum Candies You Can Get from Boxes/Solution.cpp b/solution/1200-1299/1298.Maximum Candies You Can Get from Boxes/Solution.cpp index b9fd6d7fd67e3..fa5b26442c51b 100644 --- a/solution/1200-1299/1298.Maximum Candies You Can Get from Boxes/Solution.cpp +++ b/solution/1200-1299/1298.Maximum Candies You Can Get from Boxes/Solution.cpp @@ -6,35 +6,28 @@ class Solution { vector has(n); vector took(n); queue q; - for (int& i : initialBoxes) - { + for (int& i : initialBoxes) { has[i] = true; - if (status[i]) - { + if (status[i]) { ans += candies[i]; took[i] = true; q.push(i); } } - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); - for (int k : keys[i]) - { + for (int k : keys[i]) { status[k] = 1; - if (has[k] && !took[k]) - { + if (has[k] && !took[k]) { ans += candies[k]; took[k] = true; q.push(k); } } - for (int j : containedBoxes[i]) - { + for (int j : containedBoxes[i]) { has[j] = true; - if (status[j] && !took[j]) - { + if (status[j] && !took[j]) { ans += candies[j]; took[j] = true; q.push(j); diff --git a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.cpp b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.cpp index af5ec7ad739fc..b650806cc4d61 100644 --- a/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.cpp +++ b/solution/1300-1399/1305.All Elements in Two Binary Search Trees/Solution.cpp @@ -29,10 +29,11 @@ class Solution { vector merge(vector& t1, vector& t2) { vector ans; int i = 0, j = 0; - while (i < t1.size() && j < t2.size()) - { - if (t1[i] <= t2[j]) ans.push_back(t1[i++]); - else ans.push_back(t2[j++]); + while (i < t1.size() && j < t2.size()) { + if (t1[i] <= t2[j]) + ans.push_back(t1[i++]); + else + ans.push_back(t2[j++]); } while (i < t1.size()) ans.push_back(t1[i++]); while (j < t2.size()) ans.push_back(t2[j++]); diff --git a/solution/1300-1399/1306.Jump Game III/Solution.cpp b/solution/1300-1399/1306.Jump Game III/Solution.cpp index f62e2129ccc77..0831bebd92c8d 100644 --- a/solution/1300-1399/1306.Jump Game III/Solution.cpp +++ b/solution/1300-1399/1306.Jump Game III/Solution.cpp @@ -1,16 +1,14 @@ class Solution { public: - bool canReach(vector &arr, int start) { + bool canReach(vector& arr, int start) { int n = arr.size(); - queue q{{start}}; - while (!q.empty()) - { + queue q {{start}}; + while (!q.empty()) { int i = q.front(); if (arr[i] == 0) return 1; q.pop(); - for (int j : {i + arr[i], i - arr[i]}) - { + for (int j : {i + arr[i], i - arr[i]}) { if (j >= 0 && j < n && arr[j] >= 0) q.push(j); } diff --git a/solution/1300-1399/1310.XOR Queries of a Subarray/Solution.cpp b/solution/1300-1399/1310.XOR Queries of a Subarray/Solution.cpp index 57b0967474225..dce6ddb667227 100644 --- a/solution/1300-1399/1310.XOR Queries of a Subarray/Solution.cpp +++ b/solution/1300-1399/1310.XOR Queries of a Subarray/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector xors(n + 1); for (int i = 0; i < n; ++i) xors[i + 1] = xors[i] ^ arr[i]; vector ans; - for (auto& q : queries) - { + for (auto& q : queries) { int l = q[0], r = q[1]; ans.push_back(xors[l] ^ xors[r + 1]); } diff --git a/solution/1300-1399/1314.Matrix Block Sum/Solution.cpp b/solution/1300-1399/1314.Matrix Block Sum/Solution.cpp index 17dd0920822fd..fde9ecbd76387 100644 --- a/solution/1300-1399/1314.Matrix Block Sum/Solution.cpp +++ b/solution/1300-1399/1314.Matrix Block Sum/Solution.cpp @@ -5,7 +5,7 @@ class Solution { vector> pre(m + 1, vector(n + 1)); for (int i = 1; i < m + 1; ++i) { for (int j = 1; j < n + 1; ++j) { - pre[i][j] = pre[i - 1][j] + pre[i][j - 1] + - pre[i - 1][j - 1] + mat[i - 1][j - 1]; + pre[i][j] = pre[i - 1][j] + pre[i][j - 1] + -pre[i - 1][j - 1] + mat[i - 1][j - 1]; } } vector> ans(m, vector(n)); diff --git a/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/Solution.cpp b/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/Solution.cpp index 3d33dbd0f2e4c..d73298535fe7d 100644 --- a/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/Solution.cpp +++ b/solution/1300-1399/1315.Sum of Nodes with Even-Valued Grandparent/Solution.cpp @@ -22,8 +22,7 @@ class Solution { void dfs(TreeNode* g, TreeNode* p) { if (!p) return; - if (g->val % 2 == 0) - { + if (g->val % 2 == 0) { if (p->left) res += p->left->val; if (p->right) res += p->right->val; } diff --git a/solution/1300-1399/1316.Distinct Echo Substrings/Solution.cpp b/solution/1300-1399/1316.Distinct Echo Substrings/Solution.cpp index 188f5fc411332..690671227acca 100644 --- a/solution/1300-1399/1316.Distinct Echo Substrings/Solution.cpp +++ b/solution/1300-1399/1316.Distinct Echo Substrings/Solution.cpp @@ -8,17 +8,14 @@ class Solution { vector p(n + 10); vector h(n + 10); p[0] = 1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int t = text[i] - 'a' + 1; p[i + 1] = p[i] * base; h[i + 1] = h[i] * base + t; } unordered_set vis; - for (int i = 0; i < n - 1; ++i) - { - for (int j = i + 1; j < n; j += 2) - { + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; j += 2) { int k = (i + j) >> 1; ull a = get(i + 1, k + 1, p, h); ull b = get(k + 2, j + 1, p, h); diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp index ee165f1dbab4f..20c990700c2cd 100644 --- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp +++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int minFlips(int a, int b, int c) { int ans = 0; - for (int i = 0; i < 31; ++i) - { + for (int i = 0; i < 31; ++i) { int x = (a >> i) & 1, y = (b >> i) & 1, z = (c >> i) & 1; if ((x | y) == z) continue; if (x == 1 && y == 1 && z == 0) ++ans; diff --git a/solution/1300-1399/1319.Number of Operations to Make Network Connected/Solution.cpp b/solution/1300-1399/1319.Number of Operations to Make Network Connected/Solution.cpp index 9efd501c11eb9..c022f7088473e 100644 --- a/solution/1300-1399/1319.Number of Operations to Make Network Connected/Solution.cpp +++ b/solution/1300-1399/1319.Number of Operations to Make Network Connected/Solution.cpp @@ -6,12 +6,11 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; int cnt = 0; - for (auto& e : connections) - { + for (auto& e : connections) { int a = e[0], b = e[1]; - if (find(a) == find(b)) ++cnt; - else - { + if (find(a) == find(b)) + ++cnt; + else { p[find(a)] = find(b); --n; } diff --git a/solution/1300-1399/1323.Maximum 69 Number/Solution.cpp b/solution/1300-1399/1323.Maximum 69 Number/Solution.cpp index 81b013bf7732e..a31c8f389ad13 100644 --- a/solution/1300-1399/1323.Maximum 69 Number/Solution.cpp +++ b/solution/1300-1399/1323.Maximum 69 Number/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: int maximum69Number(int num) { string s = to_string(num); - for (char& ch: s) - { - if (ch == '6') - { + for (char& ch : s) { + if (ch == '6') { ch = '9'; break; } diff --git a/solution/1300-1399/1324.Print Words Vertically/Solution.cpp b/solution/1300-1399/1324.Print Words Vertically/Solution.cpp index 35ecf3ac56a32..c8bbca91e72ce 100644 --- a/solution/1300-1399/1324.Print Words Vertically/Solution.cpp +++ b/solution/1300-1399/1324.Print Words Vertically/Solution.cpp @@ -5,23 +5,19 @@ class Solution { vector words; string word; int n = 0; - while (in >> word) - { + while (in >> word) { words.push_back(word); - n = max(n, (int) word.size()); + n = max(n, (int)word.size()); } int m = words.size(); vector ans; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { string t = ""; - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { word = words[i]; t += j < word.size() ? word[j] : ' '; } - while (t.back() == ' ') - { + while (t.back() == ' ') { t.pop_back(); } ans.push_back(t); diff --git a/solution/1300-1399/1325.Delete Leaves With a Given Value/Solution.cpp b/solution/1300-1399/1325.Delete Leaves With a Given Value/Solution.cpp index 7463c638fc1eb..b6333d4f22700 100644 --- a/solution/1300-1399/1325.Delete Leaves With a Given Value/Solution.cpp +++ b/solution/1300-1399/1325.Delete Leaves With a Given Value/Solution.cpp @@ -21,10 +21,11 @@ class Solution { if (!root) return; dfs(root->left, root, target); dfs(root->right, root, target); - if (!root->left && !root->right && root->val == target) - { - if (prev->left == root) prev->left = nullptr; - else prev->right = nullptr; + if (!root->left && !root->right && root->val == target) { + if (prev->left == root) + prev->left = nullptr; + else + prev->right = nullptr; } } }; \ No newline at end of file diff --git a/solution/1300-1399/1337.The K Weakest Rows in a Matrix/Solution.cpp b/solution/1300-1399/1337.The K Weakest Rows in a Matrix/Solution.cpp index b2cb4c95edc1a..3364d07cdf923 100644 --- a/solution/1300-1399/1337.The K Weakest Rows in a Matrix/Solution.cpp +++ b/solution/1300-1399/1337.The K Weakest Rows in a Matrix/Solution.cpp @@ -1,11 +1,9 @@ class Solution { public: - int search(vector& m) - { + int search(vector& m) { int l = 0; int h = m.size() - 1; - while (l <= h) - { + while (l <= h) { int mid = l + (h - l) / 2; if (m[mid] == 0) h = mid - 1; @@ -14,18 +12,16 @@ class Solution { } return l; } - + vector kWeakestRows(vector>& mat, int k) { vector> p; vector res; - for (int i = 0; i < mat.size(); i++) - { + for (int i = 0; i < mat.size(); i++) { int count = search(mat[i]); p.push_back({count, i}); } sort(p.begin(), p.end()); - for (int i = 0; i < k; i++) - { + for (int i = 0; i < k; i++) { res.push_back(p[i].second); } return res; diff --git a/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp b/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp index 897dd612ff359..e22473b1eaaf7 100644 --- a/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp +++ b/solution/1300-1399/1338.Reduce Array Size to The Half/Solution.cpp @@ -8,8 +8,7 @@ class Solution { sort(t.begin(), t.end(), greater()); int ans = 0; int n = 0; - for (int cnt : t) - { + for (int cnt : t) { n += cnt; ++ans; if (n * 2 >= arr.size()) break; diff --git a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.cpp b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.cpp index 3a26227805339..367861cf916c7 100644 --- a/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.cpp +++ b/solution/1300-1399/1342.Number of Steps to Reduce a Number to Zero/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int numberOfSteps(int num) { int ans = 0; - while (num) - { + while (num) { num = num & 1 ? num - 1 : num >> 1; ++ans; } diff --git a/solution/1300-1399/1345.Jump Game IV/Solution.cpp b/solution/1300-1399/1345.Jump Game IV/Solution.cpp index 5056be6f4c40a..b1fe17852b940 100644 --- a/solution/1300-1399/1345.Jump Game IV/Solution.cpp +++ b/solution/1300-1399/1345.Jump Game IV/Solution.cpp @@ -8,33 +8,27 @@ class Solution { q.emplace(0, 0); unordered_set vis; vis.insert(0); - while (!q.empty()) - { + while (!q.empty()) { auto e = q.front(); q.pop(); int i = e.first, step = e.second; if (i == n - 1) return step; int v = arr[i]; ++step; - if (idx.count(v)) - { - for (int j : idx[v]) - { - if (!vis.count(j)) - { + if (idx.count(v)) { + for (int j : idx[v]) { + if (!vis.count(j)) { vis.insert(j); q.emplace(j, step); } } idx.erase(v); } - if (i + 1 < n && !vis.count(i + 1)) - { + if (i + 1 < n && !vis.count(i + 1)) { vis.insert(i + 1); q.emplace(i + 1, step); } - if (i - 1 >= 0 && !vis.count(i - 1)) - { + if (i - 1 >= 0 && !vis.count(i - 1)) { vis.insert(i - 1); q.emplace(i - 1, step); } diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp index e8becb1eb98c9..daf87b00cfd4a 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp @@ -4,10 +4,11 @@ class Solution { vector counter(26); for (char c : s) ++counter[c - 'a']; int res = 0; - for (char c : t) - { - if (counter[c - 'a'] > 0) --counter[c - 'a']; - else ++res; + for (char c : t) { + if (counter[c - 'a'] > 0) + --counter[c - 'a']; + else + ++res; } return res; } diff --git a/solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/Solution.cpp b/solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/Solution.cpp index a62b09c615a7a..be894f7d055bf 100644 --- a/solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/Solution.cpp +++ b/solution/1300-1399/1351.Count Negative Numbers in a Sorted Matrix/Solution.cpp @@ -3,14 +3,12 @@ class Solution { int countNegatives(vector>& grid) { int m = grid.size(), n = grid[0].size(); int ans = 0; - for (int i = m - 1, j = 0; i >= 0 && j < n;) - { - if (grid[i][j] < 0) - { + for (int i = m - 1, j = 0; i >= 0 && j < n;) { + if (grid[i][j] < 0) { ans += n - j; --i; - } - else ++j; + } else + ++j; } return ans; } diff --git a/solution/1300-1399/1361.Validate Binary Tree Nodes/Solution.cpp b/solution/1300-1399/1361.Validate Binary Tree Nodes/Solution.cpp index cc9ebfcbcc62a..8b6fb21b938a9 100644 --- a/solution/1300-1399/1361.Validate Binary Tree Nodes/Solution.cpp +++ b/solution/1300-1399/1361.Validate Binary Tree Nodes/Solution.cpp @@ -6,18 +6,15 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; vector vis(n, false); - for (int i = 0, t = n; i < t; ++i) - { + for (int i = 0, t = n; i < t; ++i) { int l = leftChild[i], r = rightChild[i]; - if (l != -1) - { + if (l != -1) { if (vis[l] || find(i) == find(l)) return false; vis[l] = true; p[find(i)] = find(l); --n; } - if (r != -1) - { + if (r != -1) { if (vis[r] || find(i) == find(r)) return false; vis[r] = true; p[find(i)] = find(r); diff --git a/solution/1300-1399/1366.Rank Teams by Votes/Solution.cpp b/solution/1300-1399/1366.Rank Teams by Votes/Solution.cpp index a0b1808cd46bc..2498238121161 100644 --- a/solution/1300-1399/1366.Rank Teams by Votes/Solution.cpp +++ b/solution/1300-1399/1366.Rank Teams by Votes/Solution.cpp @@ -3,17 +3,15 @@ class Solution { string rankTeams(vector& votes) { unordered_map> counter; int n = votes[0].size(); - for (auto& vote : votes) - { - for (int i = 0; i < n; ++i) - { + for (auto& vote : votes) { + for (int i = 0; i < n; ++i) { char v = vote[i]; counter[v].resize(n); ++counter[v][i]; } } string ans = votes[0]; - sort(ans.begin(), ans.end(), [&](char a, char b){ + sort(ans.begin(), ans.end(), [&](char a, char b) { return counter[a] > counter[b] || (counter[a] == counter[b] && a < b); }); return ans; diff --git a/solution/1300-1399/1368.Minimum Cost to Make at Least One Valid Path in a Grid/Solution.cpp b/solution/1300-1399/1368.Minimum Cost to Make at Least One Valid Path in a Grid/Solution.cpp index a6282d9912a3b..3bd8768db7244 100644 --- a/solution/1300-1399/1368.Minimum Cost to Make at Least One Valid Path in a Grid/Solution.cpp +++ b/solution/1300-1399/1368.Minimum Cost to Make at Least One Valid Path in a Grid/Solution.cpp @@ -6,21 +6,20 @@ class Solution { vector> dirs = {{0, 0}, {0, 1}, {0, -1}, {1, 0}, {-1, 0}}; deque> q; q.push_back({0, 0}); - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop_front(); int i = p.first / n, j = p.first % n, d = p.second; if (i == m - 1 && j == n - 1) return d; if (vis[i][j]) continue; vis[i][j] = true; - for (int k = 1; k <= 4; ++k) - { + for (int k = 1; k <= 4; ++k) { int x = i + dirs[k][0], y = j + dirs[k][1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { - if (grid[i][j] == k) q.push_front({x * n + y, d}); - else q.push_back({x * n + y, d + 1}); + if (x >= 0 && x < m && y >= 0 && y < n) { + if (grid[i][j] == k) + q.push_front({x * n + y, d}); + else + q.push_back({x * n + y, d + 1}); } } } diff --git a/solution/1300-1399/1370.Increasing Decreasing String/Solution.cpp b/solution/1300-1399/1370.Increasing Decreasing String/Solution.cpp index 07711535cfddd..f4c3141e2e883 100644 --- a/solution/1300-1399/1370.Increasing Decreasing String/Solution.cpp +++ b/solution/1300-1399/1370.Increasing Decreasing String/Solution.cpp @@ -4,20 +4,15 @@ class Solution { vector counter(26); for (char c : s) ++counter[c - 'a']; string ans = ""; - while (ans.size() < s.size()) - { - for (int i = 0; i < 26; ++i) - { - if (counter[i]) - { + while (ans.size() < s.size()) { + for (int i = 0; i < 26; ++i) { + if (counter[i]) { ans += (i + 'a'); --counter[i]; } } - for (int i = 25; i >= 0; --i) - { - if (counter[i]) - { + for (int i = 25; i >= 0; --i) { + if (counter[i]) { ans += (i + 'a'); --counter[i]; } diff --git a/solution/1300-1399/1371.Find the Longest Substring Containing Vowels in Even Counts/Solution.cpp b/solution/1300-1399/1371.Find the Longest Substring Containing Vowels in Even Counts/Solution.cpp index da63fe7ddd7ec..f5c7028e58149 100644 --- a/solution/1300-1399/1371.Find the Longest Substring Containing Vowels in Even Counts/Solution.cpp +++ b/solution/1300-1399/1371.Find the Longest Substring Containing Vowels in Even Counts/Solution.cpp @@ -5,8 +5,7 @@ class Solution { pos[0] = -1; string vowels = "aeiou"; int state = 0, ans = 0; - for (int i = 0; i < s.size(); ++i) - { + for (int i = 0; i < s.size(); ++i) { for (int j = 0; j < 5; ++j) if (s[i] == vowels[j]) state ^= (1 << j); diff --git a/solution/1300-1399/1375.Number of Times Binary String Is Prefix-Aligned/Solution.cpp b/solution/1300-1399/1375.Number of Times Binary String Is Prefix-Aligned/Solution.cpp index 2c35946a04dfe..14afc620cd385 100644 --- a/solution/1300-1399/1375.Number of Times Binary String Is Prefix-Aligned/Solution.cpp +++ b/solution/1300-1399/1375.Number of Times Binary String Is Prefix-Aligned/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int numTimesAllBlue(vector& flips) { int ans = 0, mx = 0; - for (int i = 1; i <= flips.size(); ++i) - { + for (int i = 1; i <= flips.size(); ++i) { mx = max(mx, flips[i - 1]); ans += mx == i; } diff --git a/solution/1300-1399/1377.Frog Position After T Seconds/Solution.cpp b/solution/1300-1399/1377.Frog Position After T Seconds/Solution.cpp index b8d77e6a82c8a..f07d565c84786 100644 --- a/solution/1300-1399/1377.Frog Position After T Seconds/Solution.cpp +++ b/solution/1300-1399/1377.Frog Position After T Seconds/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: double frogPosition(int n, vector>& edges, int t, int target) { vector> g(n + 1); - for (auto& e : edges) - { + for (auto& e : edges) { int u = e[0], v = e[1]; g[u].push_back(v); g[v].push_back(u); @@ -13,19 +12,15 @@ class Solution { q.push({1, 1.0}); vector vis(n + 1); vis[1] = true; - while (!q.empty() && t >= 0) - { - for (int k = q.size(); k; --k) - { + while (!q.empty() && t >= 0) { + for (int k = q.size(); k; --k) { auto x = q.front(); q.pop(); int u = x.first; double p = x.second; vector nxt; - for (int v : g[u]) - { - if (!vis[v]) - { + for (int v : g[u]) { + if (!vis[v]) { vis[v] = true; nxt.push_back(v); } diff --git a/solution/1300-1399/1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree/Solution.cpp b/solution/1300-1399/1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree/Solution.cpp index 8d17efc239a5e..c16e908865fc3 100644 --- a/solution/1300-1399/1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree/Solution.cpp +++ b/solution/1300-1399/1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree/Solution.cpp @@ -19,8 +19,7 @@ class Solution { void dfs(TreeNode* original, TreeNode* cloned, TreeNode* target) { if (!cloned) return; - if (original == target) - { + if (original == target) { res = cloned; return; } diff --git a/solution/1300-1399/1380.Lucky Numbers in a Matrix/Solution.cpp b/solution/1300-1399/1380.Lucky Numbers in a Matrix/Solution.cpp index 8a36ec6543a5a..f327e0a9db6b3 100644 --- a/solution/1300-1399/1380.Lucky Numbers in a Matrix/Solution.cpp +++ b/solution/1300-1399/1380.Lucky Numbers in a Matrix/Solution.cpp @@ -1,13 +1,11 @@ class Solution { public: - vector luckyNumbers (vector>& matrix) { + vector luckyNumbers(vector>& matrix) { int m = matrix.size(), n = matrix[0].size(); vector rows(m, INT_MAX); vector cols(n); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { rows[i] = min(rows[i], matrix[i][j]); cols[j] = max(cols[j], matrix[i][j]); } diff --git a/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.cpp b/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.cpp index 506f408e496d9..2efa2206731f0 100644 --- a/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.cpp +++ b/solution/1300-1399/1381.Design a Stack With Increment Operation/Solution.cpp @@ -7,15 +7,15 @@ class CustomStack { s.resize(maxSize); t = 0; } - + void push(int x) { if (t < s.size()) s[t++] = x; } - + int pop() { return t == 0 ? -1 : s[--t]; } - + void increment(int k, int val) { for (int i = 0; i < min(k, t); ++i) s[i] += val; } diff --git a/solution/1300-1399/1383.Maximum Performance of a Team/Solution.cpp b/solution/1300-1399/1383.Maximum Performance of a Team/Solution.cpp index 82521fbbde11e..1b25eecff503f 100644 --- a/solution/1300-1399/1383.Maximum Performance of a Team/Solution.cpp +++ b/solution/1300-1399/1383.Maximum Performance of a Team/Solution.cpp @@ -8,18 +8,16 @@ class Solution { long long ans = 0; int mod = 1e9 + 7; long long t = 0; - for (auto& x : team) - { + for (auto& x : team) { int s = x.second, e = -x.first; t += s; ans = max(ans, e * t); q.push(s); - if (q.size() >= k) - { + if (q.size() >= k) { t -= q.top(); q.pop(); } } - return (int) (ans % mod); + return (int)(ans % mod); } }; \ No newline at end of file diff --git a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.cpp b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.cpp index 6fe25371686d6..e597b5285df09 100644 --- a/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.cpp +++ b/solution/1300-1399/1385.Find the Distance Value Between Two Arrays/Solution.cpp @@ -6,7 +6,7 @@ class Solution { ans += check(arr2, a, d); return ans; } - + bool check(vector& arr, int a, int d) { for (int& b : arr) if (abs(a - b) <= d) diff --git a/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cpp b/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cpp index 2acc403d4cb21..c6377ecf511ca 100644 --- a/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cpp +++ b/solution/1300-1399/1386.Cinema Seat Allocation/Solution.cpp @@ -2,19 +2,15 @@ class Solution { public: int maxNumberOfFamilies(int n, vector>& reservedSeats) { unordered_map m; - for (auto& e : reservedSeats) - { + for (auto& e : reservedSeats) { int i = e[0], j = 10 - e[1]; m[i] |= (1 << j); } vector masks = {0b0111100000, 0b0000011110, 0b0001111000}; int ans = (n - m.size()) << 1; - for (auto& [_, v] : m) - { - for (int& mask : masks) - { - if ((v & mask) == 0) - { + for (auto& [_, v] : m) { + for (int& mask : masks) { + if ((v & mask) == 0) { v |= mask; ++ans; } diff --git a/solution/1300-1399/1391.Check if There is a Valid Path in a Grid/Solution.cpp b/solution/1300-1399/1391.Check if There is a Valid Path in a Grid/Solution.cpp index 85e438acfda94..048e85b35a59c 100644 --- a/solution/1300-1399/1391.Check if There is a Valid Path in a Grid/Solution.cpp +++ b/solution/1300-1399/1391.Check if There is a Valid Path in a Grid/Solution.cpp @@ -7,58 +7,45 @@ class Solution { int n = grid[0].size(); p.resize(m * n); for (int i = 0; i < p.size(); ++i) p[i] = i; - auto left = [&] (int i, int j) { + auto left = [&](int i, int j) { if (j > 0 && (grid[i][j - 1] == 1 || grid[i][j - 1] == 4 || grid[i][j - 1] == 6)) { p[find(i * n + j)] = find(i * n + j - 1); } }; - auto right = [&] (int i, int j) { + auto right = [&](int i, int j) { if (j < n - 1 && (grid[i][j + 1] == 1 || grid[i][j + 1] == 3 || grid[i][j + 1] == 5)) { p[find(i * n + j)] = find(i * n + j + 1); } }; - auto up = [&] (int i, int j) { + auto up = [&](int i, int j) { if (i > 0 && (grid[i - 1][j] == 2 || grid[i - 1][j] == 3 || grid[i - 1][j] == 4)) { p[find(i * n + j)] = find((i - 1) * n + j); } }; - auto down = [&] (int i, int j) { + auto down = [&](int i, int j) { if (i < m - 1 && (grid[i + 1][j] == 2 || grid[i + 1][j] == 5 || grid[i + 1][j] == 6)) { p[find(i * n + j)] = find((i + 1) * n + j); } }; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int e = grid[i][j]; - if (e == 1) - { + if (e == 1) { left(i, j); right(i, j); - } - else if (e == 2) - { + } else if (e == 2) { up(i, j); down(i, j); - } - else if (e == 3) - { + } else if (e == 3) { left(i, j); down(i, j); - } - else if (e == 4) - { + } else if (e == 4) { right(i, j); down(i, j); - } - else if (e == 5) - { + } else if (e == 5) { left(i, j); up(i, j); - } - else - { + } else { right(i, j); up(i, j); } diff --git a/solution/1300-1399/1392.Longest Happy Prefix/Solution.cpp b/solution/1300-1399/1392.Longest Happy Prefix/Solution.cpp index c6f5070ecdbf8..1667db60e8ad7 100644 --- a/solution/1300-1399/1392.Longest Happy Prefix/Solution.cpp +++ b/solution/1300-1399/1392.Longest Happy Prefix/Solution.cpp @@ -9,13 +9,11 @@ class Solution { ULL h[n + 10]; p[0] = 1; h[0] = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { p[i + 1] = p[i] * base; h[i + 1] = h[i] * base + s[i]; } - for (int l = n - 1; l > 0; --l) - { + for (int l = n - 1; l > 0; --l) { ULL prefix = h[l]; ULL suffix = h[n] - h[n - l] * p[l]; if (prefix == suffix) return s.substr(0, l); diff --git a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp index 337d7dc2dd95f..7727ee4232564 100644 --- a/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp +++ b/solution/1300-1399/1394.Find Lucky Integer in an Array/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector counter(n); for (int e : arr) ++counter[e]; int ans = -1; - for (int i = 1; i < n; ++i) - { + for (int i = 1; i < n; ++i) { if (i == counter[i] && ans < i) ans = i; } return ans; diff --git a/solution/1300-1399/1395.Count Number of Teams/Solution.cpp b/solution/1300-1399/1395.Count Number of Teams/Solution.cpp index ab8eb0f83e521..9f3ac0ee8764b 100644 --- a/solution/1300-1399/1395.Count Number of Teams/Solution.cpp +++ b/solution/1300-1399/1395.Count Number of Teams/Solution.cpp @@ -2,18 +2,19 @@ class Solution { public: int numTeams(vector& rating) { int n = rating.size(), ans = 0; - for (int j = 1; j < n - 1; ++j) - { + for (int j = 1; j < n - 1; ++j) { int ia = 0, ib = 0, ka = 0, kb = 0; - for (int i = 0; i < j; ++i) - { - if (rating[i] < rating[j]) ++ia; - else if (rating[i] > rating[j]) ++ib; + for (int i = 0; i < j; ++i) { + if (rating[i] < rating[j]) + ++ia; + else if (rating[i] > rating[j]) + ++ib; } - for (int k = j + 1; k < n; ++k) - { - if (rating[j] < rating[k]) ++ka; - else if (rating[j] > rating[k]) ++kb; + for (int k = j + 1; k < n; ++k) { + if (rating[j] < rating[k]) + ++ka; + else if (rating[j] > rating[k]) + ++kb; } ans += ia * ka + ib * kb; } diff --git a/solution/1300-1399/1399.Count Largest Group/Solution.cpp b/solution/1300-1399/1399.Count Largest Group/Solution.cpp index d965675f52b41..a97f2c36ec36d 100644 --- a/solution/1300-1399/1399.Count Largest Group/Solution.cpp +++ b/solution/1300-1399/1399.Count Largest Group/Solution.cpp @@ -3,22 +3,19 @@ class Solution { int countLargestGroup(int n) { vector cnt(40); int mx = 0, ans = 0; - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { int t = 0; int j = i; - while (j) - { + while (j) { t += j % 10; j /= 10; } ++cnt[t]; - if (mx < cnt[t]) - { + if (mx < cnt[t]) { mx = cnt[t]; ans = 1; - } - else if (mx == cnt[t]) ++ans; + } else if (mx == cnt[t]) + ++ans; } return ans; } diff --git a/solution/1400-1499/1402.Reducing Dishes/Solution.cpp b/solution/1400-1499/1402.Reducing Dishes/Solution.cpp index 9c1e45d9f5a5d..525e477780b74 100644 --- a/solution/1400-1499/1402.Reducing Dishes/Solution.cpp +++ b/solution/1400-1499/1402.Reducing Dishes/Solution.cpp @@ -3,11 +3,12 @@ class Solution { int maxSatisfaction(vector& satisfaction) { sort(rbegin(satisfaction), rend(satisfaction)); int ans = 0, presum = 0; - for (int v : satisfaction) - { + for (int v : satisfaction) { presum += v; - if (presum > 0) ans += presum; - else break; + if (presum > 0) + ans += presum; + else + break; } return ans; } diff --git a/solution/1400-1499/1403.Minimum Subsequence in Non-Increasing Order/Solution.cpp b/solution/1400-1499/1403.Minimum Subsequence in Non-Increasing Order/Solution.cpp index 3ef044078d259..8f2c5395aa50b 100644 --- a/solution/1400-1499/1403.Minimum Subsequence in Non-Increasing Order/Solution.cpp +++ b/solution/1400-1499/1403.Minimum Subsequence in Non-Increasing Order/Solution.cpp @@ -6,8 +6,7 @@ class Solution { for (int v : nums) s += v; int t = 0; vector ans; - for (int i = nums.size() - 1; ~i; --i) - { + for (int i = nums.size() - 1; ~i; --i) { t += nums[i]; ans.push_back(nums[i]); if (t > s - t) break; diff --git a/solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/Solution.cpp b/solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/Solution.cpp index 999892afda01c..add134d1545a7 100644 --- a/solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/Solution.cpp +++ b/solution/1400-1499/1404.Number of Steps to Reduce a Number in Binary Representation to One/Solution.cpp @@ -3,20 +3,16 @@ class Solution { int numSteps(string s) { int ans = 0; bool carry = false; - for (int i = s.size() - 1; i; --i) - { + for (int i = s.size() - 1; i; --i) { char c = s[i]; - if (carry) - { - if (c == '0') - { + if (carry) { + if (c == '0') { c = '1'; carry = false; - } - else c = '0'; + } else + c = '0'; } - if (c == '1') - { + if (c == '1') { ++ans; carry = true; } diff --git a/solution/1400-1499/1408.String Matching in an Array/Solution.cpp b/solution/1400-1499/1408.String Matching in an Array/Solution.cpp index 2e687f860ba0c..7817ce6af16ad 100644 --- a/solution/1400-1499/1408.String Matching in an Array/Solution.cpp +++ b/solution/1400-1499/1408.String Matching in an Array/Solution.cpp @@ -3,12 +3,9 @@ class Solution { vector stringMatching(vector& words) { vector ans; int n = words.size(); - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (i != j && words[j].find(words[i]) != string::npos) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (i != j && words[j].find(words[i]) != string::npos) { ans.push_back(words[i]); break; } diff --git a/solution/1400-1499/1409.Queries on a Permutation With Key/Solution.cpp b/solution/1400-1499/1409.Queries on a Permutation With Key/Solution.cpp index 0fc8a02e8f86d..501bb6f5364b7 100644 --- a/solution/1400-1499/1409.Queries on a Permutation With Key/Solution.cpp +++ b/solution/1400-1499/1409.Queries on a Permutation With Key/Solution.cpp @@ -4,13 +4,10 @@ class Solution { vector p(m); iota(p.begin(), p.end(), 1); vector ans; - for (int v : queries) - { + for (int v : queries) { int j = 0; - for (int i = 0; i < m; ++i) - { - if (p[i] == v) - { + for (int i = 0; i < m; ++i) { + if (p[i] == v) { j = i; break; } diff --git a/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp b/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp index dfce1fa5e145a..280a6ee64a2c6 100644 --- a/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp +++ b/solution/1400-1499/1410.HTML Entity Parser/Solution.cpp @@ -10,17 +10,13 @@ class Solution { d["⁄"] = "/"; string ans = ""; int i = 0, n = text.size(); - while (i < n) - { + while (i < n) { bool find = false; - for (int l = 1; l < 8; ++l) - { + for (int l = 1; l < 8; ++l) { int j = i + l; - if (j <= n) - { + if (j <= n) { string t = text.substr(i, l); - if (d.count(t)) - { + if (d.count(t)) { ans += d[t]; i = j; find = true; diff --git "a/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/Solution.cpp" "b/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/Solution.cpp" index ce7b9f9c6771b..57ca48373d9b9 100644 --- "a/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/Solution.cpp" +++ "b/solution/1400-1499/1411.Number of Ways to Paint N \303\227 3 Grid/Solution.cpp" @@ -5,13 +5,12 @@ class Solution { int numOfWays(int n) { int mod = 1e9 + 7; ll f0 = 6, f1 = 6; - while (--n) - { + while (--n) { ll g0 = (f0 * 3 + f1 * 2) % mod; ll g1 = (f0 * 2 + f1 * 2) % mod; f0 = g0; f1 = g1; } - return (int) (f0 + f1) % mod; + return (int)(f0 + f1) % mod; } }; \ No newline at end of file diff --git a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.cpp b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.cpp index a760e46e4a4f0..ea8243ffd19d1 100644 --- a/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.cpp +++ b/solution/1400-1499/1413.Minimum Value to Get Positive Step by Step Sum/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int minStartValue(vector& nums) { int s = 0, t = INT_MAX; - for (int num : nums) - { + for (int num : nums) { s += num; t = min(t, s); } diff --git a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp index eb21040e1becf..ad9f584e60e8b 100644 --- a/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp +++ b/solution/1400-1499/1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int findMinFibonacciNumbers(int k) { if (k < 2) return k; int a = 1, b = 1; - while (b <= k) - { + while (b <= k) { b = a + b; a = b - a; } diff --git a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.cpp b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.cpp index 3ec446e5e4b33..33c13dad5b07a 100644 --- a/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.cpp +++ b/solution/1400-1499/1415.The k-th Lexicographical String of All Happy Strings of Length n/Solution.cpp @@ -7,13 +7,11 @@ class Solution { } void dfs(string t, int n) { - if (t.size() == n) - { + if (t.size() == n) { ans.push_back(t); return; } - for (int c = 'a'; c <= 'c'; ++c) - { + for (int c = 'a'; c <= 'c'; ++c) { if (t.size() && t.back() == c) continue; t.push_back(c); dfs(t, n); diff --git a/solution/1400-1499/1417.Reformat The String/Solution.cpp b/solution/1400-1499/1417.Reformat The String/Solution.cpp index 073a1f26c59b2..b32dbf110ec49 100644 --- a/solution/1400-1499/1417.Reformat The String/Solution.cpp +++ b/solution/1400-1499/1417.Reformat The String/Solution.cpp @@ -3,8 +3,10 @@ class Solution { string reformat(string s) { string a = "", b = ""; for (char c : s) { - if (isdigit(c)) a += c; - else b += c; + if (isdigit(c)) + a += c; + else + b += c; } int m = a.size(), n = b.size(); if (abs(m - n) > 1) return ""; diff --git a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.cpp b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.cpp index 403a9adc20964..b7f8063cd2d67 100644 --- a/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.cpp +++ b/solution/1400-1499/1418.Display Table of Food Orders in a Restaurant/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_set tables; unordered_set foods; unordered_map mp; - for (auto& order : orders) - { + for (auto& order : orders) { int table = stoi(order[1]); string food = order[2]; tables.insert(table); @@ -23,12 +22,10 @@ class Solution { title.push_back("Table"); for (auto e : f) title.push_back(e); res.push_back(title); - for (int table : t) - { + for (int table : t) { vector tmp; tmp.push_back(to_string(table)); - for (string food : f) - { + for (string food : f) { tmp.push_back(to_string(mp[to_string(table) + "." + food])); } res.push_back(tmp); diff --git a/solution/1400-1499/1419.Minimum Number of Frogs Croaking/Solution.cpp b/solution/1400-1499/1419.Minimum Number of Frogs Croaking/Solution.cpp index fa8f0ed565ba6..f11096b1ee8c0 100644 --- a/solution/1400-1499/1419.Minimum Number of Frogs Croaking/Solution.cpp +++ b/solution/1400-1499/1419.Minimum Number of Frogs Croaking/Solution.cpp @@ -2,31 +2,23 @@ class Solution { public: int minNumberOfFrogs(string croakOfFrogs) { int c = 0, r = 0, o = 0, a = 0, k = 0, ans = 0; - for (char ch : croakOfFrogs) - { - if (ch == 'c') - { + for (char ch : croakOfFrogs) { + if (ch == 'c') { ++c; - if (k > 0) --k; - else ++ans; - } - else if (ch == 'r') - { + if (k > 0) + --k; + else + ++ans; + } else if (ch == 'r') { ++r; --c; - } - else if (ch == 'o') - { + } else if (ch == 'o') { ++o; --r; - } - else if (ch == 'a') - { + } else if (ch == 'a') { ++a; --o; - } - else - { + } else { ++k; --a; } diff --git a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.cpp b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.cpp index c706d4dedc704..74b98848952ff 100644 --- a/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.cpp +++ b/solution/1400-1499/1423.Maximum Points You Can Obtain from Cards/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector s(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] + cardPoints[i]; int mi = INT_MAX; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int j = i + (n - k) - 1; if (j < n) mi = min(mi, s[j + 1] - s[i]); } diff --git a/solution/1400-1499/1425.Constrained Subsequence Sum/Solution.cpp b/solution/1400-1499/1425.Constrained Subsequence Sum/Solution.cpp index 4ad660940e6eb..ddddd9b3b97f5 100644 --- a/solution/1400-1499/1425.Constrained Subsequence Sum/Solution.cpp +++ b/solution/1400-1499/1425.Constrained Subsequence Sum/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector dp(n); int ans = INT_MIN; deque q; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (!q.empty() && i - q.front() > k) q.pop_front(); dp[i] = max(0, q.empty() ? 0 : dp[q.front()]) + nums[i]; ans = max(ans, dp[i]); diff --git a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp index 96bb2b20e81e9..feffac2a65305 100644 --- a/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp +++ b/solution/1400-1499/1428.Leftmost Column with at Least a One/Solution.cpp @@ -10,7 +10,7 @@ class Solution { public: - int leftMostColumnWithOne(BinaryMatrix &binaryMatrix) { + int leftMostColumnWithOne(BinaryMatrix& binaryMatrix) { vector scale = binaryMatrix.dimensions(); int rows = scale[0], cols = scale[1]; int res = -1; diff --git a/solution/1400-1499/1434.Number of Ways to Wear Different Hats to Each Other/Solution.cpp b/solution/1400-1499/1434.Number of Ways to Wear Different Hats to Each Other/Solution.cpp index 2ce708bada3c6..b007277056eef 100644 --- a/solution/1400-1499/1434.Number of Ways to Wear Different Hats to Each Other/Solution.cpp +++ b/solution/1400-1499/1434.Number of Ways to Wear Different Hats to Each Other/Solution.cpp @@ -6,10 +6,8 @@ class Solution { vector> d(41); int n = hats.size(); int mx = 0; - for (int i = 0; i < n; ++i) - { - for (int& h : hats[i]) - { + for (int i = 0; i < n; ++i) { + for (int& h : hats[i]) { d[h].push_back(i); mx = max(mx, h); } @@ -17,15 +15,11 @@ class Solution { vector> dp(mx + 1, vector(1 << n)); dp[0][0] = 1; int mod = 1e9 + 7; - for (int i = 1; i <= mx; ++i) - { - for (int mask = 0; mask < 1 << n; ++mask) - { + for (int i = 1; i <= mx; ++i) { + for (int mask = 0; mask < 1 << n; ++mask) { dp[i][mask] = dp[i - 1][mask]; - for (int& j : d[i]) - { - if ((mask >> j) & 1) - { + for (int& j : d[i]) { + if ((mask >> j) & 1) { dp[i][mask] = (dp[i][mask] + dp[i - 1][mask ^ (1 << j)]) % mod; } } diff --git a/solution/1400-1499/1441.Build an Array With Stack Operations/Solution.cpp b/solution/1400-1499/1441.Build an Array With Stack Operations/Solution.cpp index ceb7ae031238d..2f519e3ec7174 100644 --- a/solution/1400-1499/1441.Build an Array With Stack Operations/Solution.cpp +++ b/solution/1400-1499/1441.Build an Array With Stack Operations/Solution.cpp @@ -3,13 +3,10 @@ class Solution { vector buildArray(vector& target, int n) { vector ans; int cur = 1; - for (int t : target) - { - for (int i = cur; i <= n; ++i) - { + for (int t : target) { + for (int i = cur; i <= n; ++i) { ans.push_back("Push"); - if (t == i) - { + if (t == i) { cur = i + 1; break; } diff --git a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp index 02ea76fef8010..2fbe1327ff539 100644 --- a/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp +++ b/solution/1400-1499/1442.Count Triplets That Can Form Two Arrays of Equal XOR/Solution.cpp @@ -5,12 +5,9 @@ class Solution { vector pre(n + 1); for (int i = 0; i < n; ++i) pre[i + 1] = pre[i] ^ arr[i]; int ans = 0; - for (int i = 0; i < n - 1; ++i) - { - for (int j = i + 1; j < n; ++j) - { - for (int k = j; k < n; ++k) - { + for (int i = 0; i < n - 1; ++i) { + for (int j = i + 1; j < n; ++j) { + for (int k = j; k < n; ++k) { int a = pre[j] ^ pre[i], b = pre[k + 1] ^ pre[j]; if (a == b) ++ans; } diff --git a/solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/Solution.cpp b/solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/Solution.cpp index 14796c98366e2..2ae14aedd0c43 100644 --- a/solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/Solution.cpp +++ b/solution/1400-1499/1443.Minimum Time to Collect All Apples in a Tree/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minTime(int n, vector>& edges, vector& hasApple) { vector vis(n); vector> g(n); - for (auto& e : edges) - { + for (auto& e : edges) { int u = e[0], v = e[1]; g[u].push_back(v); g[v].push_back(u); diff --git a/solution/1400-1499/1446.Consecutive Characters/Solution.cpp b/solution/1400-1499/1446.Consecutive Characters/Solution.cpp index cd203cbb458af..d90f4d8d9278f 100644 --- a/solution/1400-1499/1446.Consecutive Characters/Solution.cpp +++ b/solution/1400-1499/1446.Consecutive Characters/Solution.cpp @@ -2,10 +2,11 @@ class Solution { public: int maxPower(string s) { int ans = 0, t = 0; - for (int i = 0; i < s.size(); ++i) - { - if (i == 0 || s[i] == s[i - 1]) ++t; - else t = 1; + for (int i = 0; i < s.size(); ++i) { + if (i == 0 || s[i] == s[i - 1]) + ++t; + else + t = 1; ans = max(ans, t); } return ans; diff --git a/solution/1400-1499/1448.Count Good Nodes in Binary Tree/Solution.cpp b/solution/1400-1499/1448.Count Good Nodes in Binary Tree/Solution.cpp index daa4e16efd58a..10a0f1729ca5f 100644 --- a/solution/1400-1499/1448.Count Good Nodes in Binary Tree/Solution.cpp +++ b/solution/1400-1499/1448.Count Good Nodes in Binary Tree/Solution.cpp @@ -21,8 +21,7 @@ class Solution { void dfs(TreeNode* root, int mx) { if (!root) return; - if (mx <= root->val) - { + if (mx <= root->val) { ++ans; mx = root->val; } diff --git a/solution/1400-1499/1457.Pseudo-Palindromic Paths in a Binary Tree/Solution.cpp b/solution/1400-1499/1457.Pseudo-Palindromic Paths in a Binary Tree/Solution.cpp index c3647eb26eb5e..000e6781476a1 100644 --- a/solution/1400-1499/1457.Pseudo-Palindromic Paths in a Binary Tree/Solution.cpp +++ b/solution/1400-1499/1457.Pseudo-Palindromic Paths in a Binary Tree/Solution.cpp @@ -14,7 +14,7 @@ class Solution { int ans; vector counter; - int pseudoPalindromicPaths (TreeNode* root) { + int pseudoPalindromicPaths(TreeNode* root) { ans = 0; counter.resize(10); dfs(root); @@ -24,16 +24,13 @@ class Solution { void dfs(TreeNode* root) { if (!root) return; ++counter[root->val]; - if (!root->left && !root->right) - { + if (!root->left && !root->right) { int n = 0; for (int i = 1; i < 10; ++i) if (counter[i] % 2 == 1) ++n; if (n < 2) ++ans; - } - else - { + } else { dfs(root->left); dfs(root->right); } diff --git a/solution/1400-1499/1462.Course Schedule IV/Solution.cpp b/solution/1400-1499/1462.Course Schedule IV/Solution.cpp index f0032a31a9968..b4ab10f40ad5a 100644 --- a/solution/1400-1499/1462.Course Schedule IV/Solution.cpp +++ b/solution/1400-1499/1462.Course Schedule IV/Solution.cpp @@ -2,14 +2,12 @@ class Solution { public: vector checkIfPrerequisite(int numCourses, vector>& prerequisites, vector>& queries) { vector> g(numCourses, vector(numCourses, -1)); - for (auto& e : prerequisites) - { + for (auto& e : prerequisites) { int a = e[0], b = e[1]; g[a][b] = 1; } vector ans; - for (auto& e : queries) - { + for (auto& e : queries) { int a = e[0], b = e[1]; ans.push_back(dfs(a, b, g)); } @@ -18,15 +16,12 @@ class Solution { bool dfs(int a, int b, vector>& g) { if (g[a][b] != -1) return g[a][b] == 1; - if (a == b) - { + if (a == b) { g[a][b] = 1; return true; } - for (int i = 0; i < g[a].size(); ++i) - { - if (g[a][i] == 1 && dfs(i, b, g)) - { + for (int i = 0; i < g[a].size(); ++i) { + if (g[a][i] == 1 && dfs(i, b, g)) { g[a][b] = 1; return true; } diff --git a/solution/1400-1499/1463.Cherry Pickup II/Solution.cpp b/solution/1400-1499/1463.Cherry Pickup II/Solution.cpp index 026d86adc2316..a46c95af78b6c 100644 --- a/solution/1400-1499/1463.Cherry Pickup II/Solution.cpp +++ b/solution/1400-1499/1463.Cherry Pickup II/Solution.cpp @@ -6,19 +6,15 @@ class Solution { vector>> valid(m, vector>(n, vector(n))); dp[0][0][n - 1] = grid[0][0] + grid[0][n - 1]; valid[0][0][n - 1] = true; - for (int i = 1; i < m; ++i) - { - for (int j1 = 0; j1 < n; ++j1) - { - for (int j2 = 0; j2 < n; ++j2) - { + for (int i = 1; i < m; ++i) { + for (int j1 = 0; j1 < n; ++j1) { + for (int j2 = 0; j2 < n; ++j2) { int t = grid[i][j1]; if (j1 != j2) t += grid[i][j2]; bool ok = false; for (int y1 = j1 - 1; y1 <= j1 + 1; ++y1) for (int y2 = j2 - 1; y2 <= j2 + 1; ++y2) - if (y1 >= 0 && y1 < n && y2 >= 0 && y2 < n && valid[i - 1][y1][y2]) - { + if (y1 >= 0 && y1 < n && y2 >= 0 && y2 < n && valid[i - 1][y1][y2]) { dp[i][j1][j2] = max(dp[i][j1][j2], dp[i - 1][y1][y2] + t); ok = true; } diff --git a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp index 2b2989f7c5090..d08fcbcecfaa9 100644 --- a/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp +++ b/solution/1400-1499/1466.Reorder Routes to Make All Paths Lead to the City Zero/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int minReorder(int n, vector>& connections) { unordered_map>> g; - for (auto& e : connections) - { + for (auto& e : connections) { int u = e[0], v = e[1]; g[u].push_back({v, true}); g[v].push_back({u, false}); @@ -15,12 +14,10 @@ class Solution { int dfs(int u, unordered_map>>& g, vector& vis) { vis[u] = true; int ans = 0; - for (auto& p : g[u]) - { + for (auto& p : g[u]) { int v = p.first; bool exist = p.second; - if (!vis[v]) - { + if (!vis[v]) { if (exist) ++ans; ans += dfs(v, g, vis); } diff --git a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp index f23e66f24ded9..c3ffc6b6269e6 100644 --- a/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp +++ b/solution/1400-1499/1475.Final Prices With a Special Discount in a Shop/Solution.cpp @@ -3,10 +3,8 @@ class Solution { vector finalPrices(vector& prices) { stack stk; vector ans = prices; - for (int i = 0; i < prices.size(); ++i) - { - while (!stk.empty() && prices[stk.top()] >= prices[i]) - { + for (int i = 0; i < prices.size(); ++i) { + while (!stk.empty() && prices[stk.top()] >= prices[i]) { ans[stk.top()] -= prices[i]; stk.pop(); } diff --git a/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/Solution.cpp b/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/Solution.cpp index 5f330ecb5b554..a29b31547ccad 100644 --- a/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/Solution.cpp +++ b/solution/1400-1499/1481.Least Number of Unique Integers after K Removals/Solution.cpp @@ -4,15 +4,13 @@ class Solution { unordered_map counter; for (int v : arr) ++counter[v]; vector> t(counter.begin(), counter.end()); - sort(t.begin(), t.end(), [](const auto& a, const auto& b) {return a.second < b.second;}); - for (auto [v, cnt] : t) - { - if (k >= cnt) - { + sort(t.begin(), t.end(), [](const auto& a, const auto& b) { return a.second < b.second; }); + for (auto [v, cnt] : t) { + if (k >= cnt) { k -= cnt; counter.erase(v); - } - else break; + } else + break; } return counter.size(); } diff --git a/solution/1400-1499/1489.Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree/Solution.cpp b/solution/1400-1499/1489.Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree/Solution.cpp index 657616a907ae1..24150de4364c5 100644 --- a/solution/1400-1499/1489.Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree/Solution.cpp +++ b/solution/1400-1499/1489.Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree/Solution.cpp @@ -3,7 +3,9 @@ class UnionFind { vector p; int n; - UnionFind(int _n): n(_n), p(_n) { + UnionFind(int _n) + : n(_n) + , p(_n) { iota(p.begin(), p.end(), 0); } @@ -24,27 +26,23 @@ class Solution { public: vector> findCriticalAndPseudoCriticalEdges(int n, vector>& edges) { for (int i = 0; i < edges.size(); ++i) edges[i].push_back(i); - sort(edges.begin(), edges.end(), [](auto& a, auto& b) {return a[2] < b[2];}); + sort(edges.begin(), edges.end(), [](auto& a, auto& b) { return a[2] < b[2]; }); int v = 0; UnionFind uf(n); - for (auto& e : edges) - { + for (auto& e : edges) { int f = e[0], t = e[1], w = e[2]; if (uf.unite(f, t)) v += w; } vector> ans(2); - for (auto& e : edges) - { + for (auto& e : edges) { int f = e[0], t = e[1], w = e[2], i = e[3]; UnionFind ufa(n); int k = 0; - for (auto& ne : edges) - { + for (auto& ne : edges) { int x = ne[0], y = ne[1], z = ne[2], j = ne[3]; if (j != i && ufa.unite(x, y)) k += z; } - if (ufa.n > 1 || (ufa.n == 1 && k > v)) - { + if (ufa.n > 1 || (ufa.n == 1 && k > v)) { ans[0].push_back(i); continue; } @@ -52,8 +50,7 @@ class Solution { UnionFind ufb(n); ufb.unite(f, t); k = w; - for (auto& ne : edges) - { + for (auto& ne : edges) { int x = ne[0], y = ne[1], z = ne[2], j = ne[3]; if (j != i && ufb.unite(x, y)) k += z; } diff --git a/solution/1400-1499/1496.Path Crossing/Solution.cpp b/solution/1400-1499/1496.Path Crossing/Solution.cpp index cb68ab04dd319..a2b9fdbea4889 100644 --- a/solution/1400-1499/1496.Path Crossing/Solution.cpp +++ b/solution/1400-1499/1496.Path Crossing/Solution.cpp @@ -2,13 +2,16 @@ class Solution { public: bool isPathCrossing(string path) { int x = 0, y = 0; - unordered_set vis{{0}}; - for (char c : path) - { - if (c == 'N') ++y; - else if (c == 'S') --y; - else if (c == 'E') ++x; - else --x; + unordered_set vis {{0}}; + for (char c : path) { + if (c == 'N') + ++y; + else if (c == 'S') + --y; + else if (c == 'E') + ++x; + else + --x; int t = x * 20000 + y; if (vis.count(t)) return 1; vis.insert(t); diff --git a/solution/1400-1499/1499.Max Value of Equation/Solution.cpp b/solution/1400-1499/1499.Max Value of Equation/Solution.cpp index 6919e746161fd..07b99cc43646d 100644 --- a/solution/1400-1499/1499.Max Value of Equation/Solution.cpp +++ b/solution/1400-1499/1499.Max Value of Equation/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int findMaxValueOfEquation(vector>& points, int k) { deque> q; int ans = INT_MIN; - for (auto& p : points) - { + for (auto& p : points) { int x = p[0], y = p[1]; while (!q.empty() && x - q.front()[0] > k) q.pop_front(); if (!q.empty()) ans = max(ans, y + x + q.front()[1] - q.front()[0]); diff --git a/solution/1500-1599/1505.Minimum Possible Integer After at Most K Adjacent Swaps On Digits/Solution.cpp b/solution/1500-1599/1505.Minimum Possible Integer After at Most K Adjacent Swaps On Digits/Solution.cpp index df21158b58471..94eec7201e60b 100644 --- a/solution/1500-1599/1505.Minimum Possible Integer After at Most K Adjacent Swaps On Digits/Solution.cpp +++ b/solution/1500-1599/1505.Minimum Possible Integer After at Most K Adjacent Swaps On Digits/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -36,17 +36,13 @@ class Solution { for (int i = 0; i < n; ++i) pos[num[i] - '0'].push(i + 1); BinaryIndexedTree* tree = new BinaryIndexedTree(n); string ans = ""; - for (int i = 1; i <= n; ++i) - { - for (int v = 0; v < 10; ++v) - { + for (int i = 1; i <= n; ++i) { + for (int v = 0; v < 10; ++v) { auto& q = pos[v]; - if (!q.empty()) - { + if (!q.empty()) { int j = q.front(); int dist = tree->query(n) - tree->query(j) + j - i; - if (dist <= k) - { + if (dist <= k) { k -= dist; q.pop(); ans += (v + '0'); diff --git a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.cpp b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.cpp index 413a95726bd77..f1c3a7245d4e2 100644 --- a/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.cpp +++ b/solution/1500-1599/1508.Range Sum of Sorted Subarray Sums/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int rangeSum(vector& nums, int n, int left, int right) { const int mod = 1e9 + 7; vector arr; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int s = 0; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { s += nums[j]; arr.push_back(s); } diff --git a/solution/1500-1599/1512.Number of Good Pairs/Solution.cpp b/solution/1500-1599/1512.Number of Good Pairs/Solution.cpp index 37c77913edd16..94d51ea8f7dbe 100644 --- a/solution/1500-1599/1512.Number of Good Pairs/Solution.cpp +++ b/solution/1500-1599/1512.Number of Good Pairs/Solution.cpp @@ -1,12 +1,12 @@ class Solution { public: int numIdenticalPairs(vector& nums) { - unordered_map counter; + unordered_map counter; for (int num : nums) { ++counter[num]; } int res = 0; - for (auto &[num, n] : counter) { + for (auto& [num, n] : counter) { res += n * (n - 1); } return res >> 1; diff --git a/solution/1500-1599/1514.Path with Maximum Probability/Solution.cpp b/solution/1500-1599/1514.Path with Maximum Probability/Solution.cpp index 980ede2930202..fb0047bfdd733 100644 --- a/solution/1500-1599/1514.Path with Maximum Probability/Solution.cpp +++ b/solution/1500-1599/1514.Path with Maximum Probability/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: double maxProbability(int n, vector>& edges, vector& succProb, int start, int end) { vector>> g(n); - for (int i = 0; i < edges.size(); ++i) - { + for (int i = 0; i < edges.size(); ++i) { int a = edges[i][0], b = edges[i][1]; double s = succProb[i]; g[a].push_back({b, s}); @@ -13,19 +12,16 @@ class Solution { d[start] = 1.0; queue> q; q.push({1.0, start}); - while (!q.empty()) - { + while (!q.empty()) { auto p = q.front(); q.pop(); double w = p.first; int u = p.second; if (d[u] > w) continue; - for (auto& e : g[u]) - { + for (auto& e : g[u]) { int v = e.first; double t = e.second; - if (d[v] < d[u] * t) - { + if (d[v] < d[u] * t) { d[v] = d[u] * t; q.push({d[v], v}); } diff --git a/solution/1500-1599/1518.Water Bottles/Solution.cpp b/solution/1500-1599/1518.Water Bottles/Solution.cpp index f8cb6dc2b114d..0da95016721b2 100644 --- a/solution/1500-1599/1518.Water Bottles/Solution.cpp +++ b/solution/1500-1599/1518.Water Bottles/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int numWaterBottles(int numBottles, int numExchange) { int ans = numBottles; - while (numBottles >= numExchange) - { + while (numBottles >= numExchange) { numBottles -= (numExchange - 1); ++ans; } diff --git a/solution/1500-1599/1522.Diameter of N-Ary Tree/Solution.cpp b/solution/1500-1599/1522.Diameter of N-Ary Tree/Solution.cpp index b2968b1d7dea9..1907284f5fd3d 100644 --- a/solution/1500-1599/1522.Diameter of N-Ary Tree/Solution.cpp +++ b/solution/1500-1599/1522.Diameter of N-Ary Tree/Solution.cpp @@ -25,21 +25,19 @@ class Solution { int diameter(Node* root) { ans = 0; dfs(root); - return ans; + return ans; } - + int dfs(Node* root) { if (!root) return 0; int m1 = 0, m2 = 0; - for (Node* child : root->children) - { + for (Node* child : root->children) { int t = dfs(child); - if (t > m1) - { + if (t > m1) { m2 = m1; m1 = t; - } - else if (t > m2) m2 = t; + } else if (t > m2) + m2 = t; } ans = max(ans, m1 + m2); return 1 + m1; diff --git a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.cpp b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.cpp index dab305ca1c4d2..175d1eb9575ce 100644 --- a/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.cpp +++ b/solution/1500-1599/1524.Number of Sub-arrays With Odd Sum/Solution.cpp @@ -4,12 +4,13 @@ class Solution { const int MOD = 1e9 + 7; vector counter(2); int s = 0, ans = 0; - for (int& v : arr) - { + for (int& v : arr) { s += v; ++counter[s % 2]; - if (s % 2 == 1) ans = (ans + 1 + counter[0]) % MOD; - else ans = (ans + counter[1]) % MOD; + if (s % 2 == 1) + ans = (ans + 1 + counter[0]) % MOD; + else + ans = (ans + counter[1]) % MOD; } return ans; } diff --git a/solution/1500-1599/1533.Find the Index of the Large Integer/Solution.cpp b/solution/1500-1599/1533.Find the Index of the Large Integer/Solution.cpp index a1cbe236a8244..7ca7e64cf7ee5 100644 --- a/solution/1500-1599/1533.Find the Index of the Large Integer/Solution.cpp +++ b/solution/1500-1599/1533.Find the Index of the Large Integer/Solution.cpp @@ -16,7 +16,7 @@ class Solution { public: - int getIndex(ArrayReader &reader) { + int getIndex(ArrayReader& reader) { int left = 0, right = reader.length() - 1; while (left < right) { int t1 = left, t2 = left + (right - left) / 3, t3 = left + (right - left) / 3 * 2 + 1; diff --git a/solution/1500-1599/1534.Count Good Triplets/Solution.cpp b/solution/1500-1599/1534.Count Good Triplets/Solution.cpp index 7dd9091c70328..e851b0bf745ef 100644 --- a/solution/1500-1599/1534.Count Good Triplets/Solution.cpp +++ b/solution/1500-1599/1534.Count Good Triplets/Solution.cpp @@ -3,12 +3,9 @@ class Solution { int countGoodTriplets(vector& arr, int a, int b, int c) { int n = arr.size(); int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = i + 1; j < n; ++j) - { - for (int k = j + 1; k < n; ++k) - { + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { + for (int k = j + 1; k < n; ++k) { ans += abs(arr[i] - arr[j]) <= a && abs(arr[j] - arr[k]) <= b && abs(arr[i] - arr[k]) <= c; } } diff --git a/solution/1500-1599/1539.Kth Missing Positive Number/Solution.cpp b/solution/1500-1599/1539.Kth Missing Positive Number/Solution.cpp index ee3101598c2d3..ed887d321a004 100644 --- a/solution/1500-1599/1539.Kth Missing Positive Number/Solution.cpp +++ b/solution/1500-1599/1539.Kth Missing Positive Number/Solution.cpp @@ -3,11 +3,12 @@ class Solution { int findKthPositive(vector& arr, int k) { if (arr[0] > k) return k; int left = 0, right = arr.size(); - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (arr[mid] - mid - 1 >= k) right = mid; - else left = mid + 1; + if (arr[mid] - mid - 1 >= k) + right = mid; + else + left = mid + 1; } return arr[left - 1] + k - (arr[left - 1] - (left - 1) - 1); } diff --git a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp index 332611c1f06d9..80e4c1a9cc9ab 100644 --- a/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp +++ b/solution/1500-1599/1546.Maximum Number of Non-Overlapping Subarrays With Sum Equals Target/Solution.cpp @@ -3,16 +3,13 @@ class Solution { int maxNonOverlapping(vector& nums, int target) { int i = 0, n = nums.size(); int ans = 0; - while (i < n) - { + while (i < n) { int s = 0; unordered_set seen; seen.insert(0); - while (i < n) - { + while (i < n) { s += nums[i]; - if (seen.count(s - target)) - { + if (seen.count(s - target)) { ++ans; break; } diff --git a/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp b/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp index 7bf6eb55fb9f2..a12dba095bf27 100644 --- a/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp +++ b/solution/1500-1599/1552.Magnetic Force Between Two Balls/Solution.cpp @@ -3,11 +3,12 @@ class Solution { int maxDistance(vector& position, int m) { sort(position.begin(), position.end()); int left = 1, right = position[position.size() - 1]; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; - if (check(position, mid, m)) left = mid; - else right = mid - 1; + if (check(position, mid, m)) + left = mid; + else + right = mid - 1; } return left; } @@ -15,11 +16,9 @@ class Solution { bool check(vector& position, int f, int m) { int prev = position[0]; int cnt = 1; - for (int i = 1; i < position.size(); ++i) - { + for (int i = 1; i < position.size(); ++i) { int curr = position[i]; - if (curr - prev >= f) - { + if (curr - prev >= f) { prev = curr; ++cnt; } diff --git a/solution/1500-1599/1554.Strings Differ by One Character/Solution.cpp b/solution/1500-1599/1554.Strings Differ by One Character/Solution.cpp index 494cb472321a4..760034dcaa63e 100644 --- a/solution/1500-1599/1554.Strings Differ by One Character/Solution.cpp +++ b/solution/1500-1599/1554.Strings Differ by One Character/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: bool differByOne(vector& dict) { unordered_set s; - for (auto word : dict) - { - for (int i = 0; i < word.size(); ++i) - { + for (auto word : dict) { + for (int i = 0; i < word.size(); ++i) { auto t = word; t[i] = '*'; if (s.count(t)) return true; diff --git a/solution/1500-1599/1557.Minimum Number of Vertices to Reach All Nodes/Solution.cpp b/solution/1500-1599/1557.Minimum Number of Vertices to Reach All Nodes/Solution.cpp index 8ab2b66ed61fc..e8c78fd9da6d3 100644 --- a/solution/1500-1599/1557.Minimum Number of Vertices to Reach All Nodes/Solution.cpp +++ b/solution/1500-1599/1557.Minimum Number of Vertices to Reach All Nodes/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_set s; for (auto& e : edges) s.insert(e[1]); vector ans; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (!s.count(i)) ans.push_back(i); } return ans; diff --git a/solution/1500-1599/1558.Minimum Numbers of Function Calls to Make Target Array/Solution.cpp b/solution/1500-1599/1558.Minimum Numbers of Function Calls to Make Target Array/Solution.cpp index f5408e326742b..8b319f78541b3 100644 --- a/solution/1500-1599/1558.Minimum Numbers of Function Calls to Make Target Array/Solution.cpp +++ b/solution/1500-1599/1558.Minimum Numbers of Function Calls to Make Target Array/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minOperations(vector& nums) { int ans = 0; int mx = 0; - for (int v : nums) - { + for (int v : nums) { mx = max(mx, v); ans += __builtin_popcount(v); } diff --git a/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.cpp b/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.cpp index e010df51d5902..b56f469eb205f 100644 --- a/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.cpp +++ b/solution/1500-1599/1559.Detect Cycles in 2D Grid/Solution.cpp @@ -7,15 +7,11 @@ class Solution { p.resize(m * n); for (int i = 0; i < p.size(); ++i) p[i] = i; vector dirs = {0, 1, 0}; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - for (int k = 0; k < 2; ++k) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + for (int k = 0; k < 2; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; - if (x < m && y < n && grid[x][y] == grid[i][j]) - { + if (x < m && y < n && grid[x][y] == grid[i][j]) { if (find(x * n + y) == find(i * n + j)) return 1; p[find(x * n + y)] = find(i * n + j); } diff --git a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp index 0e4234a862615..32cea27587cd8 100644 --- a/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp +++ b/solution/1500-1599/1560.Most Visited Sector in a Circular Track/Solution.cpp @@ -3,12 +3,9 @@ class Solution { vector mostVisited(int n, vector& rounds) { int m = rounds.size() - 1; vector ans; - if (rounds[0] <= rounds[m]) - { + if (rounds[0] <= rounds[m]) { for (int i = rounds[0]; i <= rounds[m]; ++i) ans.push_back(i); - } - else - { + } else { for (int i = 1; i <= rounds[m]; ++i) ans.push_back(i); for (int i = rounds[0]; i <= n; ++i) ans.push_back(i); } diff --git a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp index 4da1c57716f10..e39249c9937ae 100644 --- a/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp +++ b/solution/1500-1599/1561.Maximum Number of Coins You Can Get/Solution.cpp @@ -3,7 +3,7 @@ class Solution { int maxCoins(vector& piles) { sort(piles.begin(), piles.end()); int ans = 0; - for (int i = piles.size() - 2; i >= (int) piles.size() / 3; i -= 2) ans += piles[i]; + for (int i = piles.size() - 2; i >= (int)piles.size() / 3; i -= 2) ans += piles[i]; return ans; } }; \ No newline at end of file diff --git a/solution/1500-1599/1562.Find Latest Group of Size M/Solution.cpp b/solution/1500-1599/1562.Find Latest Group of Size M/Solution.cpp index 2b8b3108ca84f..31d9a0f6812df 100644 --- a/solution/1500-1599/1562.Find Latest Group of Size M/Solution.cpp +++ b/solution/1500-1599/1562.Find Latest Group of Size M/Solution.cpp @@ -5,8 +5,7 @@ class Solution { if (m == n) return n; vector cnt(n + 2); int ans = -1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = arr[i]; int l = cnt[v - 1], r = cnt[v + 1]; if (l == m || r == m) ans = i; diff --git a/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp b/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp index 04db2cc9ddf63..c9b2b0e485f91 100644 --- a/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp +++ b/solution/1500-1599/1575.Count All Possible Routes/Solution.cpp @@ -12,8 +12,7 @@ class Solution { if (f[i][t] != -1) return f[i][t]; if (abs(locations[i] - locations[target]) > t) return 0; int res = i == target; - for (int j = 0; j < locations.size(); ++j) - { + for (int j = 0; j < locations.size(); ++j) { if (j == i) continue; int cost = abs(locations[i] - locations[j]); if (cost <= t) res = (res + dfs(j, t - cost, locations, target, f)) % mod; diff --git a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.cpp b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.cpp index f57c3acf6513a..d28f7e2ec8f0f 100644 --- a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.cpp +++ b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.cpp @@ -1,12 +1,9 @@ class Solution { public: string modifyString(string s) { - for (int i = 0; i < s.size(); ++i) - { - if (s[i] == '?') - { - for (char cc : "abc") - { + for (int i = 0; i < s.size(); ++i) { + if (s[i] == '?') { + for (char cc : "abc") { if (i > 0 && s[i - 1] == cc) continue; if (i < s.size() - 1 && s[i + 1] == cc) continue; s[i] = cc; diff --git a/solution/1500-1599/1579.Remove Max Number of Edges to Keep Graph Fully Traversable/Solution.cpp b/solution/1500-1599/1579.Remove Max Number of Edges to Keep Graph Fully Traversable/Solution.cpp index a6d3ab1248f78..55a7f3f9b741c 100644 --- a/solution/1500-1599/1579.Remove Max Number of Edges to Keep Graph Fully Traversable/Solution.cpp +++ b/solution/1500-1599/1579.Remove Max Number of Edges to Keep Graph Fully Traversable/Solution.cpp @@ -3,7 +3,9 @@ class UnionFind { vector p; int n; - UnionFind(int _n): n(_n), p(_n) { + UnionFind(int _n) + : n(_n) + , p(_n) { iota(p.begin(), p.end(), 0); } @@ -26,12 +28,12 @@ class Solution { int maxNumEdgesToRemove(int n, vector>& edges) { UnionFind ufa(n), ufb(n); int ans = 0; - for (auto& e : edges) - { - if (e[0] == 3) - { - if (ufa.unite(e[1], e[2])) ufb.unite(e[1], e[2]); - else ++ans; + for (auto& e : edges) { + if (e[0] == 3) { + if (ufa.unite(e[1], e[2])) + ufb.unite(e[1], e[2]); + else + ++ans; } } for (auto& e : edges) diff --git a/solution/1500-1599/1584.Min Cost to Connect All Points/Solution.cpp b/solution/1500-1599/1584.Min Cost to Connect All Points/Solution.cpp index 2ad29af88f0e6..43314650db204 100644 --- a/solution/1500-1599/1584.Min Cost to Connect All Points/Solution.cpp +++ b/solution/1500-1599/1584.Min Cost to Connect All Points/Solution.cpp @@ -5,11 +5,9 @@ class Solution { int minCostConnectPoints(vector>& points) { int n = points.size(); vector> g; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int x1 = points[i][0], y1 = points[i][1]; - for (int j = i + 1; j < n; ++j) - { + for (int j = i + 1; j < n; ++j) { int x2 = points[j][0], y2 = points[j][1]; g.push_back({abs(x1 - x2) + abs(y1 - y2), i, j}); } @@ -18,8 +16,7 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; int ans = 0; - for (auto& e : g) - { + for (auto& e : g) { int cost = e[0], i = e[1], j = e[2]; if (find(i) == find(j)) continue; p[find(i)] = find(j); diff --git a/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.cpp b/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.cpp index a6933cf0e92fe..18b3907f50823 100644 --- a/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.cpp +++ b/solution/1500-1599/1588.Sum of All Odd Length Subarrays/Solution.cpp @@ -5,10 +5,8 @@ class Solution { int presum[n + 1]; for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + arr[i]; int res = 0; - for (int i = 0; i < n; ++i) - { - for (int j = 0; i + j < n; j += 2) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; i + j < n; j += 2) { res += presum[i + j + 1] - presum[i]; } } diff --git a/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/Solution.cpp b/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/Solution.cpp index 29bd7e1e41031..4436eb154d3ae 100644 --- a/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/Solution.cpp +++ b/solution/1500-1599/1589.Maximum Sum Obtained of Any Permutation/Solution.cpp @@ -13,9 +13,9 @@ class Solution { sort(nums.begin(), nums.end()); sort(delta.begin(), delta.end()); long long res = 0; - for (int i = n - 1, j = nums.size() - 1; i >= 0 && delta[i] > 0; --i, --j) { - res += (long long) delta[i] * nums[j]; + for (int i = n - 1, j = nums.size() - 1; i >= 0 && delta[i] > 0; --i, --j) { + res += (long long)delta[i] * nums[j]; } - return (int) (res % 1000000007); + return (int)(res % 1000000007); } }; \ No newline at end of file diff --git a/solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/Solution.cpp b/solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/Solution.cpp index e3c0d06e46bb7..082d044665ffa 100644 --- a/solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/Solution.cpp +++ b/solution/1600-1699/1601.Maximum Number of Achievable Transfer Requests/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int maximumRequests(int n, vector>& requests) { int ans = 0, m = requests.size(); - for (int mask = 0; mask < 1 << m; ++mask) - { + for (int mask = 0; mask < 1 << m; ++mask) { int cnt = __builtin_popcount(mask); if (ans < cnt && check(mask, requests)) ans = cnt; } @@ -12,10 +11,8 @@ class Solution { bool check(int x, vector>& requests) { vector d(21); - for (int i = 0; i < requests.size(); ++i) - { - if ((x >> i) & 1) - { + for (int i = 0; i < requests.size(); ++i) { + if ((x >> i) & 1) { --d[requests[i][0]]; ++d[requests[i][1]]; } diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp index 21cce2013b345..217f3e23bc24d 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp @@ -14,10 +14,8 @@ class Solution { TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) { queue q; q.push(root); - while (!q.empty()) - { - for (int i = 0, n = q.size(); i < n; ++i) - { + while (!q.empty()) { + for (int i = 0, n = q.size(); i < n; ++i) { TreeNode* node = q.front(); q.pop(); if (node == u) return i == n - 1 ? nullptr : q.front(); diff --git a/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/Solution.cpp b/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/Solution.cpp index 88b3de5b34983..8148ac3a6d08d 100644 --- a/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/Solution.cpp +++ b/solution/1600-1699/1605.Find Valid Matrix Given Row and Column Sums/Solution.cpp @@ -3,10 +3,8 @@ class Solution { vector> restoreMatrix(vector& rowSum, vector& colSum) { int m = rowSum.size(), n = colSum.size(); vector> ans(m, vector(n)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { int x = min(rowSum[i], colSum[j]); ans[i][j] = x; rowSum[i] -= x; diff --git a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.cpp b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.cpp index d7ea960c2f6ea..a903bd745f928 100644 --- a/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.cpp +++ b/solution/1600-1699/1606.Find Servers That Handled Most Number of Requests/Solution.cpp @@ -5,11 +5,9 @@ class Solution { for (int i = 0; i < k; ++i) free.insert(i); priority_queue, vector>, greater<>> busy; vector cnt(k); - for (int i = 0; i < arrival.size(); ++i) - { + for (int i = 0; i < arrival.size(); ++i) { int start = arrival[i], end = start + load[i]; - while (!busy.empty() && busy.top().first <= start) - { + while (!busy.empty() && busy.top().first <= start) { free.insert(busy.top().second); busy.pop(); } diff --git a/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/Solution.cpp b/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/Solution.cpp index 8d010ba49072a..323f10c3152c5 100644 --- a/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/Solution.cpp +++ b/solution/1600-1699/1608.Special Array With X Elements Greater Than or Equal X/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int specialArray(vector& nums) { int n = nums.size(); sort(nums.begin(), nums.end()); - for (int x = 0; x <= n; ++x) - { + for (int x = 0; x <= n; ++x) { int idx = lower_bound(nums.begin(), nums.end(), x) - nums.begin(); int cnt = n - 1 - idx + 1; if (cnt == x) return x; diff --git a/solution/1600-1699/1609.Even Odd Tree/Solution.cpp b/solution/1600-1699/1609.Even Odd Tree/Solution.cpp index 3dd4a962b43a5..cb6fe80563543 100644 --- a/solution/1600-1699/1609.Even Odd Tree/Solution.cpp +++ b/solution/1600-1699/1609.Even Odd Tree/Solution.cpp @@ -15,11 +15,9 @@ class Solution { bool even = true; queue q; q.push(root); - while (!q.empty()) - { + while (!q.empty()) { int prev = even ? 0 : 1000000; - for (int i = 0, n = q.size(); i < n; ++i) - { + for (int i = 0, n = q.size(); i < n; ++i) { auto node = q.front(); q.pop(); if (even && (prev >= node->val || node->val % 2 == 0)) return false; diff --git a/solution/1600-1699/1610.Maximum Number of Visible Points/Solution.cpp b/solution/1600-1699/1610.Maximum Number of Visible Points/Solution.cpp index b5b40cf6fab0d..2ea31b2764e1c 100644 --- a/solution/1600-1699/1610.Maximum Number of Visible Points/Solution.cpp +++ b/solution/1600-1699/1610.Maximum Number of Visible Points/Solution.cpp @@ -4,20 +4,20 @@ class Solution { vector v; int x = location[0], y = location[1]; int same = 0; - for (auto& p : points) - { + for (auto& p : points) { int xi = p[0], yi = p[1]; - if (xi == x && yi == y) ++same; - else v.emplace_back(atan2(yi - y, xi - x)); + if (xi == x && yi == y) + ++same; + else + v.emplace_back(atan2(yi - y, xi - x)); } sort(v.begin(), v.end()); int n = v.size(); for (int i = 0; i < n; ++i) v.emplace_back(v[i] + 2 * M_PI); - int mx = 0; + int mx = 0; double t = angle * M_PI / 180; - for (int i = 0, j = 0; j < 2 * n; ++j) - { + for (int i = 0, j = 0; j < 2 * n; ++j) { while (i < j && v[j] - v[i] > t) ++i; mx = max(mx, j - i + 1); } diff --git a/solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.cpp b/solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.cpp index 9ff1fb4f68526..df7fa391d832b 100644 --- a/solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.cpp +++ b/solution/1600-1699/1612.Check If Two Expression Trees are Equivalent/Solution.cpp @@ -18,8 +18,7 @@ class Solution { vector dfs(Node* root) { vector ans(26); if (!root) return ans; - if (root->val == '+' || root->val == '-') - { + if (root->val == '+' || root->val == '-') { auto left = dfs(root->left); auto right = dfs(root->right); calc(ans, left, right, root->val); diff --git a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp index aea742836e2ff..06f86d5d3701d 100644 --- a/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp +++ b/solution/1600-1699/1614.Maximum Nesting Depth of the Parentheses/Solution.cpp @@ -2,10 +2,11 @@ class Solution { public: int maxDepth(string s) { int n = 0, ans = 0; - for (char c : s) - { - if (c == '(') ans = max(ans, ++n); - else if (c == ')') --n; + for (char c : s) { + if (c == '(') + ans = max(ans, ++n); + else if (c == ')') + --n; } return ans; } diff --git a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.cpp b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.cpp index 22eb3ab3e5ef4..e6836c8b1e2bb 100644 --- a/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.cpp +++ b/solution/1600-1699/1619.Mean of Array After Removing Some Elements/Solution.cpp @@ -4,7 +4,7 @@ class Solution { sort(arr.begin(), arr.end()); int n = arr.size(); double s = 0; - for (int start = (int) (n * 0.05), i = start; i < n - start; ++i) + for (int start = (int)(n * 0.05), i = start; i < n - start; ++i) s += arr[i]; return s / (n * 0.9); } diff --git a/solution/1600-1699/1622.Fancy Sequence/Solution.cpp b/solution/1600-1699/1622.Fancy Sequence/Solution.cpp index c562644b8b545..8b32e0a4d6fa2 100644 --- a/solution/1600-1699/1622.Fancy Sequence/Solution.cpp +++ b/solution/1600-1699/1622.Fancy Sequence/Solution.cpp @@ -36,8 +36,7 @@ class SegmentTree { void modifyAdd(int l, int r, int inc, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v = (node->v + (node->r - node->l + 1) * inc) % MOD; node->add = (node->add + inc) % MOD; return; @@ -54,8 +53,7 @@ class SegmentTree { void modifyMul(int l, int r, int m, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v = (node->v * m) % MOD; node->add = (node->add * m) % MOD; node->mul = (node->mul * m) % MOD; @@ -88,8 +86,7 @@ class SegmentTree { void pushdown(Node* node) { if (!node->left) node->left = new Node(node->l, node->mid); if (!node->right) node->right = new Node(node->mid + 1, node->r); - if (node->add || node->mul != 1) - { + if (node->add || node->mul != 1) { long add = node->add, mul = node->mul; Node* left = node->left; Node* right = node->right; @@ -105,7 +102,6 @@ class SegmentTree { } }; - class Fancy { public: int n; @@ -115,20 +111,20 @@ class Fancy { n = 0; tree = new SegmentTree(); } - + void append(int val) { ++n; tree->modifyAdd(n, n, val); } - + void addAll(int inc) { tree->modifyAdd(1, n, inc); } - + void multAll(int m) { tree->modifyMul(1, n, m); } - + int getIndex(int idx) { return idx >= n ? -1 : tree->query(idx + 1, idx + 1); } diff --git a/solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/Solution.cpp b/solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/Solution.cpp index b09fbe7ffbf95..50c39bf6c22e9 100644 --- a/solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/Solution.cpp +++ b/solution/1600-1699/1625.Lexicographically Smallest String After Applying Operations/Solution.cpp @@ -1,22 +1,19 @@ class Solution { public: string findLexSmallestString(string s, int a, int b) { - unordered_set vis{{s}}; - queue q{{s}}; + unordered_set vis {{s}}; + queue q {{s}}; string ans = s; int n = s.size(); - while (!q.empty()) - { + while (!q.empty()) { s = q.front(); q.pop(); if (s < ans) ans = s; string nxt1 = s; for (int i = 1; i < n; i += 2) nxt1[i] = ((nxt1[i] - '0' + a) % 10) + '0'; string nxt2 = s.substr(n - b) + s.substr(0, n - b); - for (string nxt : {nxt1, nxt2}) - { - if (!vis.count(nxt)) - { + for (string nxt : {nxt1, nxt2}) { + if (!vis.count(nxt)) { vis.insert(nxt); q.push(nxt); } diff --git a/solution/1600-1699/1626.Best Team With No Conflicts/Solution.cpp b/solution/1600-1699/1626.Best Team With No Conflicts/Solution.cpp index 3e2c78794ecd8..ef2c7ebbf0018 100644 --- a/solution/1600-1699/1626.Best Team With No Conflicts/Solution.cpp +++ b/solution/1600-1699/1626.Best Team With No Conflicts/Solution.cpp @@ -6,11 +6,9 @@ class Solution { for (int i = 0; i < n; ++i) nums[i] = {ages[i], scores[i]}; sort(nums.begin(), nums.end()); vector dp(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { dp[i] = nums[i][1]; - for (int j = 0; j < i; ++j) - { + for (int j = 0; j < i; ++j) { if (nums[i][1] >= nums[j][1]) dp[i] = max(dp[i], dp[j] + nums[i][1]); } diff --git a/solution/1600-1699/1628.Design an Expression Tree With Evaluate Function/Solution.cpp b/solution/1600-1699/1628.Design an Expression Tree With Evaluate Function/Solution.cpp index 1821e691eeb9b..8e3c48136dda3 100644 --- a/solution/1600-1699/1628.Design an Expression Tree With Evaluate Function/Solution.cpp +++ b/solution/1600-1699/1628.Design an Expression Tree With Evaluate Function/Solution.cpp @@ -5,8 +5,9 @@ class Node { public: - virtual ~Node () {}; + virtual ~Node() {}; virtual int evaluate() const = 0; + protected: // define your fields here string val; @@ -37,7 +38,6 @@ class MyNode : public Node { } }; - /** * This is the TreeBuilder class. * You can treat it as the driver code that takes the postinfix input @@ -48,19 +48,15 @@ class TreeBuilder { public: Node* buildTree(vector& postfix) { stack stk; - for (auto s : postfix) - { + for (auto s : postfix) { MyNode* node; - if (s == "+" || s == "-" || s == "*" || s == "/") - { + if (s == "+" || s == "-" || s == "*" || s == "/") { auto right = stk.top(); stk.pop(); auto left = stk.top(); stk.pop(); node = new MyNode(s, left, right); - } - else - { + } else { node = new MyNode(s); } stk.push(node); @@ -69,7 +65,6 @@ class TreeBuilder { } }; - /** * Your TreeBuilder object will be instantiated and called as such: * TreeBuilder* obj = new TreeBuilder(); diff --git a/solution/1600-1699/1629.Slowest Key/Solution.cpp b/solution/1600-1699/1629.Slowest Key/Solution.cpp index 08e2a6268c171..e3fbbde83d8b6 100644 --- a/solution/1600-1699/1629.Slowest Key/Solution.cpp +++ b/solution/1600-1699/1629.Slowest Key/Solution.cpp @@ -3,11 +3,9 @@ class Solution { char slowestKey(vector& releaseTimes, string keysPressed) { char ans = keysPressed[0]; int mx = releaseTimes[0]; - for (int i = 1, n = releaseTimes.size(); i < n; ++i) - { + for (int i = 1, n = releaseTimes.size(); i < n; ++i) { int d = releaseTimes[i] - releaseTimes[i - 1]; - if (d > mx || (d == mx && keysPressed[i] > ans)) - { + if (d > mx || (d == mx && keysPressed[i] > ans)) { mx = d; ans = keysPressed[i]; } diff --git a/solution/1600-1699/1631.Path With Minimum Effort/Solution.cpp b/solution/1600-1699/1631.Path With Minimum Effort/Solution.cpp index 4e55257095d37..b7338bf17b329 100644 --- a/solution/1600-1699/1631.Path With Minimum Effort/Solution.cpp +++ b/solution/1600-1699/1631.Path With Minimum Effort/Solution.cpp @@ -7,18 +7,14 @@ class Solution { priority_queue, vector>, greater>> q; q.push({0, 0, 0}); vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto [t, i, j] = q.top(); q.pop(); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { + if (x >= 0 && x < m && y >= 0 && y < n) { int nd = max(t, abs(heights[x][y] - heights[i][j])); - if (nd < dist[x][y]) - { + if (nd < dist[x][y]) { dist[x][y] = nd; q.push({nd, x, y}); } diff --git a/solution/1600-1699/1638.Count Substrings That Differ by One Character/Solution.cpp b/solution/1600-1699/1638.Count Substrings That Differ by One Character/Solution.cpp index 43ee8d0cade09..5e3f357634d66 100644 --- a/solution/1600-1699/1638.Count Substrings That Differ by One Character/Solution.cpp +++ b/solution/1600-1699/1638.Count Substrings That Differ by One Character/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int countSubstrings(string s, string t) { int m = s.size(), n = t.size(); int ans = 0; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (s[i] == t[j]) continue; int l = 1, r = 1; while (i - l >= 0 && j - l >= 0 && s[i - l] == t[j - l]) ++l; diff --git a/solution/1600-1699/1649.Create Sorted Array through Instructions/Solution.cpp b/solution/1600-1699/1649.Create Sorted Array through Instructions/Solution.cpp index 218a03202a83f..ae4159328f019 100644 --- a/solution/1600-1699/1649.Create Sorted Array through Instructions/Solution.cpp +++ b/solution/1600-1699/1649.Create Sorted Array through Instructions/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -35,8 +35,7 @@ class Solution { int mod = 1e9 + 7; BinaryIndexedTree* tree = new BinaryIndexedTree(n); int ans = 0; - for (int num : instructions) - { + for (int num : instructions) { int a = tree->query(num - 1); int b = tree->query(n) - tree->query(num); ans += min(a, b); diff --git a/solution/1600-1699/1650.Lowest Common Ancestor of a Binary Tree III/Solution.cpp b/solution/1600-1699/1650.Lowest Common Ancestor of a Binary Tree III/Solution.cpp index 01144fe436aae..4e3b3541ff6e6 100644 --- a/solution/1600-1699/1650.Lowest Common Ancestor of a Binary Tree III/Solution.cpp +++ b/solution/1600-1699/1650.Lowest Common Ancestor of a Binary Tree III/Solution.cpp @@ -11,11 +11,10 @@ class Node { class Solution { public: - Node* lowestCommonAncestor(Node* p, Node * q) { + Node* lowestCommonAncestor(Node* p, Node* q) { Node* a = p; Node* b = q; - while (a != b) - { + while (a != b) { a = a->parent ? a->parent : q; b = b->parent ? b->parent : p; } diff --git a/solution/1600-1699/1654.Minimum Jumps to Reach Home/Solution.cpp b/solution/1600-1699/1654.Minimum Jumps to Reach Home/Solution.cpp index 430fdd666aa51..a32cffd5661d0 100644 --- a/solution/1600-1699/1654.Minimum Jumps to Reach Home/Solution.cpp +++ b/solution/1600-1699/1654.Minimum Jumps to Reach Home/Solution.cpp @@ -10,10 +10,8 @@ class Solution { vis[0][0] = true; vis[0][1] = true; int ans = 0; - while (!q.empty()) - { - for (int t = q.size(); t; --t) - { + while (!q.empty()) { + for (int t = q.size(); t; --t) { auto p = q.front(); q.pop(); int i = p[0], dir = p[1]; @@ -21,12 +19,10 @@ class Solution { vector> nxt; nxt.push_back({i + a, 1}); if (dir) nxt.push_back({i - b, 0}); - for (auto& e : nxt) - { + for (auto& e : nxt) { int j = e[0]; dir = e[1]; - if (j >= 0 && j < N && !s.count(j) && !vis[j][dir]) - { + if (j >= 0 && j < N && !s.count(j) && !vis[j][dir]) { vis[j][dir] = true; q.push({j, dir}); } diff --git a/solution/1600-1699/1656.Design an Ordered Stream/Solution.cpp b/solution/1600-1699/1656.Design an Ordered Stream/Solution.cpp index c2dd22b36cddf..60f63ec99bd6d 100644 --- a/solution/1600-1699/1656.Design an Ordered Stream/Solution.cpp +++ b/solution/1600-1699/1656.Design an Ordered Stream/Solution.cpp @@ -6,7 +6,7 @@ class OrderedStream { OrderedStream(int n) { data.resize(n, ""); } - + vector insert(int idKey, string value) { data[idKey - 1] = value; vector ans; diff --git a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp index 5faa937b951a6..2fe6511dc6554 100644 --- a/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp +++ b/solution/1600-1699/1658.Minimum Operations to Reduce X to Zero/Solution.cpp @@ -7,12 +7,10 @@ class Solution { unordered_map seen; seen[0] = -1; int ans = INT_MAX; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { s += nums[i]; if (!seen.count(s)) seen[s] = i; - if (seen.count(s - x)) - { + if (seen.count(s - x)) { int j = seen[s - x]; ans = min(ans, n - (i - j)); } diff --git a/solution/1600-1699/1660.Correct a Binary Tree/Solution.cpp b/solution/1600-1699/1660.Correct a Binary Tree/Solution.cpp index b57b211941ae8..ec28009467ca2 100644 --- a/solution/1600-1699/1660.Correct a Binary Tree/Solution.cpp +++ b/solution/1600-1699/1660.Correct a Binary Tree/Solution.cpp @@ -15,27 +15,21 @@ class Solution { queue q; q.push(root); unordered_set s; - while (!q.empty()) - { + while (!q.empty()) { int n = q.size(); - while (n--) - { + while (n--) { TreeNode* node = q.front(); q.pop(); - if (node->right) - { - if (s.count(node->right->right)) - { + if (node->right) { + if (s.count(node->right->right)) { node->right = nullptr; return root; } q.push(node->right); s.insert(node->right); } - if (node->left) - { - if (s.count(node->left->right)) - { + if (node->left) { + if (s.count(node->left->right)) { node->left = nullptr; return root; } diff --git a/solution/1600-1699/1668.Maximum Repeating Substring/Solution.cpp b/solution/1600-1699/1668.Maximum Repeating Substring/Solution.cpp index b3f28f961fc78..77f7652e96638 100644 --- a/solution/1600-1699/1668.Maximum Repeating Substring/Solution.cpp +++ b/solution/1600-1699/1668.Maximum Repeating Substring/Solution.cpp @@ -3,7 +3,7 @@ class Solution { int minOperations(vector& nums, int x) { int n = nums.size(); int sum = 0; - for (int num: nums) { + for (int num : nums) { sum += num; } diff --git a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp index 05eeb9290fb5e..ea5a74c1149c8 100644 --- a/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp +++ b/solution/1600-1699/1676.Lowest Common Ancestor of a Binary Tree IV/Solution.cpp @@ -13,9 +13,9 @@ class Solution { public: unordered_set s; - TreeNode* lowestCommonAncestor(TreeNode* root, vector &nodes) { + TreeNode* lowestCommonAncestor(TreeNode* root, vector& nodes) { for (auto node : nodes) s.insert(node->val); - return dfs(root); + return dfs(root); } TreeNode* dfs(TreeNode* root) { diff --git a/solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.cpp b/solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.cpp index 7220771c920a4..cb307c6014ad8 100644 --- a/solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.cpp +++ b/solution/1600-1699/1691.Maximum Height by Stacking Cuboids/Solution.cpp @@ -6,12 +6,9 @@ class Solution { int n = cuboids.size(); vector dp(n); int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < i; ++j) - { - if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < i; ++j) { + if (cuboids[j][1] <= cuboids[i][1] && cuboids[j][2] <= cuboids[i][2]) { dp[i] = max(dp[i], dp[j]); } } diff --git a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.cpp b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.cpp index c6fa233353f39..7d9742b492c8b 100644 --- a/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.cpp +++ b/solution/1600-1699/1697.Checking Existence of Edge Length Limited Paths/Solution.cpp @@ -14,14 +14,12 @@ class Solution { sort(indexes.begin(), indexes.end(), [&](int i, int j) { return queries[i][2] < queries[j][2]; }); - + vector ans(m, false); int i = 0; - for (int j : indexes) - { + for (int j : indexes) { int pj = queries[j][0], qj = queries[j][1], limit = queries[j][2]; - while (i < edgeList.size() && edgeList[i][2] < limit) - { + while (i < edgeList.size() && edgeList[i][2] < limit) { int u = edgeList[i][0], v = edgeList[i][1]; p[find(u)] = find(v); ++i; diff --git a/solution/1600-1699/1698.Number of Distinct Substrings in a String/Solution.cpp b/solution/1600-1699/1698.Number of Distinct Substrings in a String/Solution.cpp index 055b0a9c1a05d..3f6a007f366fc 100644 --- a/solution/1600-1699/1698.Number of Distinct Substrings in a String/Solution.cpp +++ b/solution/1600-1699/1698.Number of Distinct Substrings in a String/Solution.cpp @@ -4,10 +4,8 @@ class Solution { unordered_set ss; int n = s.size(); string_view t, v = s; - for (int i = 0; i < n; ++i) - { - for (int j = i + 1; j <= n; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j <= n; ++j) { t = v.substr(i, j - i); ss.insert(t); } diff --git a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp index 8acf46aea3bef..b6099390939c6 100644 --- a/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp +++ b/solution/1700-1799/1705.Maximum Number of Eaten Apples/Solution.cpp @@ -5,12 +5,10 @@ class Solution { int eatenApples(vector& apples, vector& days) { priority_queue, greater> q; int i = 0, n = apples.size(), ans = 0; - while (i < n || !q.empty()) - { + while (i < n || !q.empty()) { if (i < n && apples[i] > 0) q.emplace(i + days[i] - 1, apples[i]); while (!q.empty() && q.top().first < i) q.pop(); - if (!q.empty()) - { + if (!q.empty()) { PII t = q.top(); q.pop(); --t.second; diff --git a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.cpp b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.cpp index d57d84cb6ce83..8eb576c1e2940 100644 --- a/solution/1700-1799/1706.Where Will the Ball Fall/Solution.cpp +++ b/solution/1700-1799/1706.Where Will the Ball Fall/Solution.cpp @@ -15,7 +15,7 @@ class Solution { int dfs(int i, int j) { if (i == m) return j; if (j == 0 && grid[i][j] == -1) return -1; - if (j == n - 1 && grid[i][j] == 1) return -1; + if (j == n - 1 && grid[i][j] == 1) return -1; if (grid[i][j] == 1 && grid[i][j + 1] == -1) return -1; if (grid[i][j] == -1 && grid[i][j - 1] == 1) return -1; return grid[i][j] == 1 ? dfs(i + 1, j + 1) : dfs(i + 1, j - 1); diff --git a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cpp b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cpp index b1d2548091631..06b0a72a47abc 100644 --- a/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cpp +++ b/solution/1700-1799/1717.Maximum Score From Removing Substrings/Solution.cpp @@ -1,40 +1,35 @@ class Solution { public: int maximumGain(string s, int x, int y) { - if (x < y) - { + if (x < y) { reverse(s.begin(), s.end()); return maximumGain(s, y, x); } int ans = 0; stack stk1; stack stk2; - for (char c : s) - { - if (c != 'b') stk1.push(c); - else - { - if (!stk1.empty() && stk1.top() == 'a') - { + for (char c : s) { + if (c != 'b') + stk1.push(c); + else { + if (!stk1.empty() && stk1.top() == 'a') { stk1.pop(); ans += x; - } - else stk1.push(c); + } else + stk1.push(c); } } - while (!stk1.empty()) - { + while (!stk1.empty()) { char c = stk1.top(); stk1.pop(); - if (c != 'b') stk2.push(c); - else - { - if (!stk2.empty() && stk2.top() == 'a') - { + if (c != 'b') + stk2.push(c); + else { + if (!stk2.empty() && stk2.top() == 'a') { stk2.pop(); ans += y; - } - else stk2.push(c); + } else + stk2.push(c); } } return ans; diff --git a/solution/1700-1799/1718.Construct the Lexicographically Largest Valid Sequence/Solution.cpp b/solution/1700-1799/1718.Construct the Lexicographically Largest Valid Sequence/Solution.cpp index 680a509ace442..c6850b39dba6c 100644 --- a/solution/1700-1799/1718.Construct the Lexicographically Largest Valid Sequence/Solution.cpp +++ b/solution/1700-1799/1718.Construct the Lexicographically Largest Valid Sequence/Solution.cpp @@ -17,10 +17,8 @@ class Solution { bool dfs(int u) { if (u == n * 2) return 1; if (path[u]) return dfs(u + 1); - for (int i = n; i > 1; --i) - { - if (cnt[i] && u + i < n * 2 && !path[u + i]) - { + for (int i = n; i > 1; --i) { + if (cnt[i] && u + i < n * 2 && !path[u + i]) { path[u] = path[u + i] = i; cnt[i] = 0; if (dfs(u + 1)) return 1; @@ -28,8 +26,7 @@ class Solution { path[u] = path[u + i] = 0; } } - if (cnt[1]) - { + if (cnt[1]) { path[u] = 1; cnt[1] = 0; if (dfs(u + 1)) return 1; diff --git a/solution/1700-1799/1719.Number Of Ways To Reconstruct A Tree/Solution.cpp b/solution/1700-1799/1719.Number Of Ways To Reconstruct A Tree/Solution.cpp index 3b7a577cf1962..2b78b9541f441 100644 --- a/solution/1700-1799/1719.Number Of Ways To Reconstruct A Tree/Solution.cpp +++ b/solution/1700-1799/1719.Number Of Ways To Reconstruct A Tree/Solution.cpp @@ -3,39 +3,35 @@ class Solution { int checkWays(vector>& pairs) { vector> g(510, vector(510)); vector> v(510); - for (auto& p : pairs) - { + for (auto& p : pairs) { int x = p[0], y = p[1]; g[x][y] = g[y][x] = 1; v[x].push_back(y); v[y].push_back(x); } vector nodes; - for (int i = 1; i <= 500; ++i) - { - if (v[i].size()) - { + for (int i = 1; i <= 500; ++i) { + if (v[i].size()) { nodes.push_back(i); g[i][i] = 1; } } - sort(nodes.begin(), nodes.end(), [&](int x, int y)->bool{return v[x].size() < v[y].size();}); + sort(nodes.begin(), nodes.end(), [&](int x, int y) -> bool { return v[x].size() < v[y].size(); }); bool equal = 0; int root = 0; - for (int i = 0; i < nodes.size(); ++i) - { + for (int i = 0; i < nodes.size(); ++i) { int x = nodes[i]; int j = i + 1; - for (; j < nodes.size() && !g[x][nodes[j]]; ++j); - if (j < nodes.size()) - { + for (; j < nodes.size() && !g[x][nodes[j]]; ++j) + ; + if (j < nodes.size()) { int y = nodes[j]; if (v[x].size() == v[y].size()) equal = 1; for (int z : v[x]) if (!g[y][z]) return 0; - } - else ++root; + } else + ++root; } if (root > 1) return 0; if (equal) return 2; diff --git a/solution/1700-1799/1720.Decode XORed Array/Solution.cpp b/solution/1700-1799/1720.Decode XORed Array/Solution.cpp index ada1ef0c83176..e312df467ea50 100644 --- a/solution/1700-1799/1720.Decode XORed Array/Solution.cpp +++ b/solution/1700-1799/1720.Decode XORed Array/Solution.cpp @@ -1,7 +1,7 @@ class Solution { public: vector decode(vector& encoded, int first) { - vector ans{{first}}; + vector ans {{first}}; for (int i = 0; i < encoded.size(); ++i) ans.push_back(ans[i] ^ encoded[i]); return ans; diff --git a/solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.cpp b/solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.cpp index f73d3f3846853..11f0284814bb8 100644 --- a/solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.cpp +++ b/solution/1700-1799/1721.Swapping Nodes in a Linked List/Solution.cpp @@ -11,11 +11,11 @@ class Solution { public: ListNode* swapNodes(ListNode* head, int k) { - ListNode *p1 = head; + ListNode* p1 = head; for (int i = 1; i < k; i++) p1 = p1->next; - ListNode* slow = head, *fast = p1->next; - + ListNode *slow = head, *fast = p1->next; + while (fast) { fast = fast->next; slow = slow->next; diff --git a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.cpp b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.cpp index ed73e492a31cd..1fc9aeb7ad077 100644 --- a/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.cpp +++ b/solution/1700-1799/1722.Minimize Hamming Distance After Swap Operations/Solution.cpp @@ -10,10 +10,11 @@ class Solution { unordered_map> mp; for (int i = 0; i < n; ++i) ++mp[find(i)][source[i]]; int res = 0; - for (int i = 0; i < n; ++i) - { - if (mp[find(i)][target[i]] > 0) --mp[find(i)][target[i]]; - else ++res; + for (int i = 0; i < n; ++i) { + if (mp[find(i)][target[i]] > 0) + --mp[find(i)][target[i]]; + else + ++res; } return res; } diff --git a/solution/1700-1799/1723.Find Minimum Time to Finish All Jobs/Solution.cpp b/solution/1700-1799/1723.Find Minimum Time to Finish All Jobs/Solution.cpp index 9902fe9706615..4ca42720a21cb 100644 --- a/solution/1700-1799/1723.Find Minimum Time to Finish All Jobs/Solution.cpp +++ b/solution/1700-1799/1723.Find Minimum Time to Finish All Jobs/Solution.cpp @@ -15,8 +15,7 @@ class Solution { ans = min(ans, *max_element(cnt.begin(), cnt.end())); return; } - for (int j = 0; j < k; ++j) - { + for (int j = 0; j < k; ++j) { if (cnt[j] + jobs[i] >= ans) continue; cnt[j] += jobs[i]; dfs(i + 1, k, jobs, cnt); diff --git a/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/Solution.cpp b/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/Solution.cpp index e6655d45ae6bd..8644b9b962724 100644 --- a/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/Solution.cpp +++ b/solution/1700-1799/1725.Number Of Rectangles That Can Form The Largest Square/Solution.cpp @@ -2,15 +2,13 @@ class Solution { public: int countGoodRectangles(vector>& rectangles) { int ans = 0, mx = 0; - for (auto& r : rectangles) - { + for (auto& r : rectangles) { int t = min(r[0], r[1]); - if (mx < t) - { + if (mx < t) { mx = t; ans = 1; - } - else if (mx == t) ++ans; + } else if (mx == t) + ++ans; } return ans; } diff --git a/solution/1700-1799/1730.Shortest Path to Get Food/Solution.cpp b/solution/1700-1799/1730.Shortest Path to Get Food/Solution.cpp index f72dff5534f80..69ad1c3e18801 100644 --- a/solution/1700-1799/1730.Shortest Path to Get Food/Solution.cpp +++ b/solution/1700-1799/1730.Shortest Path to Get Food/Solution.cpp @@ -4,25 +4,20 @@ class Solution { public: int getFood(vector>& grid) { int m = grid.size(), n = grid[0].size(); - queue q{{pos(grid)}}; + queue q {{pos(grid)}}; int ans = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { pii p = q.front(); q.pop(); - for (int j = 0; j < 4; ++j) - { + for (int j = 0; j < 4; ++j) { int x = p.first + dirs[j]; int y = p.second + dirs[j + 1]; - if (x >= 0 && x < m && y >= 0 && y < n) - { + if (x >= 0 && x < m && y >= 0 && y < n) { if (grid[x][y] == '#') return ans; - if (grid[x][y] == 'O') - { + if (grid[x][y] == 'O') { grid[x][y] = 'X'; q.push({x, y}); } diff --git a/solution/1700-1799/1732.Find the Highest Altitude/Solution.cpp b/solution/1700-1799/1732.Find the Highest Altitude/Solution.cpp index 25578adf20098..6e25e30128b01 100644 --- a/solution/1700-1799/1732.Find the Highest Altitude/Solution.cpp +++ b/solution/1700-1799/1732.Find the Highest Altitude/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int largestAltitude(vector& gain) { int res = 0, t = 0; - for (int h : gain) - { + for (int h : gain) { t += h; res = max(res, t); } diff --git a/solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/Solution.cpp b/solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/Solution.cpp index ea2713023ea26..7e08f83fd137f 100644 --- a/solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/Solution.cpp +++ b/solution/1700-1799/1738.Find Kth Largest XOR Coordinate Value/Solution.cpp @@ -4,10 +4,8 @@ class Solution { int m = matrix.size(), n = matrix[0].size(); vector> s(m + 1, vector(n + 1)); vector ans; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { s[i + 1][j + 1] = s[i + 1][j] ^ s[i][j + 1] ^ s[i][j] ^ matrix[i][j]; ans.push_back(s[i + 1][j + 1]); } diff --git a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cpp b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cpp index e2f44e2dc2275..25e88332cd681 100644 --- a/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cpp +++ b/solution/1700-1799/1742.Maximum Number of Balls in a Box/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int countBalls(int lowLimit, int highLimit) { vector counter(60); int ans = 0; - for (int i = lowLimit; i <= highLimit; ++i) - { + for (int i = lowLimit; i <= highLimit; ++i) { int s = 0, j = i; - while (j) - { + while (j) { s += (j % 10); j /= 10; } diff --git a/solution/1700-1799/1748.Sum of Unique Elements/Solution.cpp b/solution/1700-1799/1748.Sum of Unique Elements/Solution.cpp index 46ce9a8936f59..30db0be66fdb8 100644 --- a/solution/1700-1799/1748.Sum of Unique Elements/Solution.cpp +++ b/solution/1700-1799/1748.Sum of Unique Elements/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: int sumOfUnique(vector& nums) { vector counter(101); - for (int num : nums) ++ counter[num]; + for (int num : nums) ++counter[num]; int ans = 0; for (int i = 0; i < 101; ++i) if (counter[i] == 1) diff --git a/solution/1700-1799/1756.Design Most Recently Used Queue/Solution.cpp b/solution/1700-1799/1756.Design Most Recently Used Queue/Solution.cpp index 23d4cce89ab2e..42f6cc3d20a70 100644 --- a/solution/1700-1799/1756.Design Most Recently Used Queue/Solution.cpp +++ b/solution/1700-1799/1756.Design Most Recently Used Queue/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -40,14 +40,15 @@ class MRUQueue { for (int i = 1; i <= n; ++i) data[i] = i; tree = new BinaryIndexedTree(n + 2010); } - + int fetch(int k) { int left = 1, right = data.size(); - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (mid - tree->query(mid) >= k) right = mid; - else left = mid + 1; + if (mid - tree->query(mid) >= k) + right = mid; + else + left = mid + 1; } data.push_back(data[left]); tree->update(left, 1); diff --git a/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.cpp b/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.cpp index 64784b12d05cc..99de084aaf2a1 100644 --- a/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.cpp +++ b/solution/1700-1799/1760.Minimum Limit of Balls in a Bag/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: int minimumSize(vector& nums, int maxOperations) { int left = 1, right = 1e9; - while (left < right) - { + while (left < right) { int mid = left + ((right - left) >> 1); long long ops = 0; for (int num : nums) ops += (num - 1) / mid; - if (ops <= maxOperations) right = mid; - else left = mid + 1; + if (ops <= maxOperations) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/1700-1799/1763.Longest Nice Substring/Solution.cpp b/solution/1700-1799/1763.Longest Nice Substring/Solution.cpp index 4e0a72c372523..cdd5544044fa0 100644 --- a/solution/1700-1799/1763.Longest Nice Substring/Solution.cpp +++ b/solution/1700-1799/1763.Longest Nice Substring/Solution.cpp @@ -3,13 +3,13 @@ class Solution { string longestNiceSubstring(string s) { int n = s.size(); string ans = ""; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int lower = 0, upper = 0; - for (int j = i; j < n; ++j) - { - if (islower(s[j])) lower |= 1 << (s[j] - 'a'); - else upper |= 1 << (s[j] - 'A'); + for (int j = i; j < n; ++j) { + if (islower(s[j])) + lower |= 1 << (s[j] - 'a'); + else + upper |= 1 << (s[j] - 'A'); if (lower == upper && j - i + 1 > ans.size()) ans = s.substr(i, j - i + 1); } } diff --git a/solution/1700-1799/1765.Map of Highest Peak/Solution.cpp b/solution/1700-1799/1765.Map of Highest Peak/Solution.cpp index 5b04c8cae9ca7..4a20787876c47 100644 --- a/solution/1700-1799/1765.Map of Highest Peak/Solution.cpp +++ b/solution/1700-1799/1765.Map of Highest Peak/Solution.cpp @@ -8,27 +8,21 @@ class Solution { int m = isWater.size(), n = isWater[0].size(); vector> ans(m, vector(n, -1)); queue q; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (isWater[i][j] == 1) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (isWater[i][j] == 1) { ans[i][j] = 0; q.push({i, j}); } } } - while (!q.empty()) - { + while (!q.empty()) { PII p = q.front(); q.pop(); int i = p.first, j = p.second; - for (auto& dir : dirs) - { + for (auto& dir : dirs) { int x = i + dir[0], y = j + dir[1]; - if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) - { + if (x >= 0 && x < m && y >= 0 && y < n && ans[x][y] == -1) { ans[x][y] = ans[i][j] + 1; q.push({x, y}); } diff --git a/solution/1700-1799/1775.Equal Sum Arrays With Minimum Number of Operations/Solution.cpp b/solution/1700-1799/1775.Equal Sum Arrays With Minimum Number of Operations/Solution.cpp index ea0961baaa60a..6d3eb7d5303e1 100644 --- a/solution/1700-1799/1775.Equal Sum Arrays With Minimum Number of Operations/Solution.cpp +++ b/solution/1700-1799/1775.Equal Sum Arrays With Minimum Number of Operations/Solution.cpp @@ -10,10 +10,8 @@ class Solution { for (int x : nums2) ++freq[x - 1]; int diff = s2 - s1; int ans = 0; - for (int i = 5; i > 0 && diff > 0; --i) - { - while (freq[i] && diff > 0) - { + for (int i = 5; i > 0 && diff > 0; --i) { + while (freq[i] && diff > 0) { diff -= i; --freq[i]; ++ans; diff --git a/solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.cpp b/solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.cpp index a07f13397e769..afa8a8b38a3a1 100644 --- a/solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.cpp +++ b/solution/1700-1799/1781.Sum of Beauty of All Substrings/Solution.cpp @@ -3,18 +3,14 @@ class Solution { int beautySum(string s) { int ans = 0; int n = s.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { vector counter(26); - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { ++counter[s[j] - 'a']; int mi = 1000; int mx = 0; - for (int v : counter) - { - if (v) - { + for (int v : counter) { + if (v) { mi = min(mi, v); mx = max(mx, v); } diff --git a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.cpp b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.cpp index 4800f03655dda..52399b9856e0c 100644 --- a/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.cpp +++ b/solution/1700-1799/1786.Number of Restricted Paths From First to Last Node/Solution.cpp @@ -15,22 +15,18 @@ class Solution { dist.assign(n + 1, inf); f.assign(n + 1, -1); dist[n] = 0; - for (auto& e : edges) - { + for (auto& e : edges) { int u = e[0], v = e[1], w = e[2]; g[u].emplace_back(v, w); g[v].emplace_back(u, w); } priority_queue, greater> q; q.emplace(0, n); - while (!q.empty()) - { + while (!q.empty()) { auto [_, u] = q.top(); q.pop(); - for (auto [v, w] : g[u]) - { - if (dist[v] > dist[u] + w) - { + for (auto [v, w] : g[u]) { + if (dist[v] > dist[u] + w) { dist[v] = dist[u] + w; q.emplace(dist[v], v); } @@ -43,10 +39,8 @@ class Solution { if (f[i] != -1) return f[i]; if (i == n) return 1; int ans = 0; - for (auto [j, _] : g[i]) - { - if (dist[i] > dist[j]) - { + for (auto [j, _] : g[i]) { + if (dist[i] > dist[j]) { ans = (ans + dfs(j)) % mod; } } diff --git a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.cpp b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.cpp index 8b46a0cb82b7f..34d887d4b92e2 100644 --- a/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.cpp +++ b/solution/1700-1799/1790.Check if One String Swap Can Make Strings Equal/Solution.cpp @@ -4,10 +4,8 @@ class Solution { char c1 = 0, c2 = 0; int n = s1.size(); int cnt = 0; - for (int i = 0; i < n; ++i) - { - if (s1[i] != s2[i]) - { + for (int i = 0; i < n; ++i) { + if (s1[i] != s2[i]) { ++cnt; if ((cnt == 2 && (c1 != s2[i] || c2 != s1[i])) || cnt > 2) return false; c1 = s1[i]; diff --git a/solution/1800-1899/1813.Sentence Similarity III/Solution.cpp b/solution/1800-1899/1813.Sentence Similarity III/Solution.cpp index 9186597072186..a1f18ec3f14c4 100644 --- a/solution/1800-1899/1813.Sentence Similarity III/Solution.cpp +++ b/solution/1800-1899/1813.Sentence Similarity III/Solution.cpp @@ -18,7 +18,7 @@ class Solution { return j == n2 || i + j == n2; } - vector split(const string &str) { + vector split(const string& str) { vector words; int i = str.find_first_not_of(' '); int j = str.find_first_of(' ', i); diff --git a/solution/1800-1899/1818.Minimum Absolute Sum Difference/Solution.cpp b/solution/1800-1899/1818.Minimum Absolute Sum Difference/Solution.cpp index e975fdfd30956..1ab22d43da45d 100644 --- a/solution/1800-1899/1818.Minimum Absolute Sum Difference/Solution.cpp +++ b/solution/1800-1899/1818.Minimum Absolute Sum Difference/Solution.cpp @@ -5,16 +5,14 @@ class Solution { int n = nums1.size(); vector diff(n); int s = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { diff[i] = abs(nums1[i] - nums2[i]); s = (s + diff[i]) % mod; } if (s == 0) return 0; sort(nums1.begin(), nums1.end()); int mx = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int d = diff[i]; if (d == 0) continue; int b = nums2[i]; diff --git a/solution/1800-1899/1822.Sign of the Product of an Array/Solution.cpp b/solution/1800-1899/1822.Sign of the Product of an Array/Solution.cpp index ea68e21c17a6a..a33d64e85bae5 100644 --- a/solution/1800-1899/1822.Sign of the Product of an Array/Solution.cpp +++ b/solution/1800-1899/1822.Sign of the Product of an Array/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: int arraySign(vector& nums) { int res = 1; - for (auto &num : nums) { + for (auto& num : nums) { if (num == 0) { return 0; } diff --git a/solution/1800-1899/1826.Faulty Sensor/Solution.cpp b/solution/1800-1899/1826.Faulty Sensor/Solution.cpp index 0363743142f7f..847f461c74d45 100644 --- a/solution/1800-1899/1826.Faulty Sensor/Solution.cpp +++ b/solution/1800-1899/1826.Faulty Sensor/Solution.cpp @@ -3,9 +3,8 @@ class Solution { int badSensor(vector& sensor1, vector& sensor2) { int i = 0; int n = sensor1.size(); - for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) {} - for (; i < n - 1; ++i) - { + for (; i < n - 1 && sensor1[i] == sensor2[i]; ++i) { } + for (; i < n - 1; ++i) { if (sensor1[i + 1] != sensor2[i]) return 1; if (sensor1[i] != sensor2[i + 1]) return 2; } diff --git a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.cpp b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.cpp index 466c20785399d..247a942a3d887 100644 --- a/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.cpp +++ b/solution/1800-1899/1827.Minimum Operations to Make the Array Increasing/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minOperations(vector& nums) { int ans = 0; int mx = 0; - for (int& v : nums) - { + for (int& v : nums) { ans += max(0, mx + 1 - v); mx = max(mx + 1, v); } diff --git a/solution/1800-1899/1829.Maximum XOR for Each Query/Solution.cpp b/solution/1800-1899/1829.Maximum XOR for Each Query/Solution.cpp index c018ea66ca9f3..7894bb5a6c32d 100644 --- a/solution/1800-1899/1829.Maximum XOR for Each Query/Solution.cpp +++ b/solution/1800-1899/1829.Maximum XOR for Each Query/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector s(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] ^ nums[i]; vector ans; - for (int i = n; i > 0; --i) - { + for (int i = n; i > 0; --i) { int t = 0, x = s[i]; for (int j = 0; j < maximumBit; ++j) { if (((x >> j) & 1) == 0) t |= (1 << j); diff --git a/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.cpp b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.cpp index 300c54713bf43..f9479dc4937b1 100644 --- a/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.cpp +++ b/solution/1800-1899/1832.Check if the Sentence Is Pangram/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool checkIfPangram(string sentence) { int res = 0; - for (char c : sentence) - { + for (char c : sentence) { res |= (1 << (c - 'a')); if (res == 0x3ffffff) return true; } diff --git a/solution/1800-1899/1833.Maximum Ice Cream Bars/Solution.cpp b/solution/1800-1899/1833.Maximum Ice Cream Bars/Solution.cpp index 02142c69e02b1..626a8c5833802 100644 --- a/solution/1800-1899/1833.Maximum Ice Cream Bars/Solution.cpp +++ b/solution/1800-1899/1833.Maximum Ice Cream Bars/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int maxIceCream(vector& costs, int coins) { sort(costs.begin(), costs.end()); int ans = 0; - for (int i = 0; i < costs.size() && coins >= costs[i]; ++i) - { + for (int i = 0; i < costs.size() && coins >= costs[i]; ++i) { ++ans; coins -= costs[i]; } diff --git a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.cpp b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.cpp index 7efe2aa1ad806..3dbab7620e789 100644 --- a/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.cpp +++ b/solution/1800-1899/1836.Remove Duplicates From an Unsorted Linked List/Solution.cpp @@ -17,7 +17,7 @@ class Solution { } ListNode* dummy = new ListNode(0, head); - for (ListNode* pre = dummy, *cur = head; cur != nullptr; cur = cur->next) { + for (ListNode *pre = dummy, *cur = head; cur != nullptr; cur = cur->next) { if (counter[cur->val] > 1) { pre->next = cur->next; } else { diff --git a/solution/1800-1899/1838.Frequency of the Most Frequent Element/Solution.cpp b/solution/1800-1899/1838.Frequency of the Most Frequent Element/Solution.cpp index f75ab3e091bc5..c6ce24b533f65 100644 --- a/solution/1800-1899/1838.Frequency of the Most Frequent Element/Solution.cpp +++ b/solution/1800-1899/1838.Frequency of the Most Frequent Element/Solution.cpp @@ -17,8 +17,10 @@ class Solution { int left = 1, right = n; while (left < right) { int mid = left + right + 1 >> 1; - if (check(mid)) left = mid; - else right = mid - 1; + if (check(mid)) + left = mid; + else + right = mid - 1; } return left; } @@ -26,7 +28,7 @@ class Solution { bool check(int count) { for (int i = 0; i < n - count + 1; ++i) { int j = i + count - 1; - if ((long long) nums[j] * count - (presum[j + 1] - presum[i]) <= k) return true; + if ((long long)nums[j] * count - (presum[j + 1] - presum[i]) <= k) return true; } return false; } diff --git a/solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/Solution.cpp b/solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/Solution.cpp index a0d84d1f2be37..950a1a87f0c88 100644 --- a/solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/Solution.cpp +++ b/solution/1800-1899/1846.Maximum Element After Decreasing and Rearranging/Solution.cpp @@ -4,8 +4,7 @@ class Solution { sort(arr.begin(), arr.end()); arr[0] = 1; int ans = 1; - for (int i = 1; i < arr.size(); ++i) - { + for (int i = 1; i < arr.size(); ++i) { int d = max(0, arr[i] - arr[i - 1] - 1); arr[i] -= d; ans = max(ans, arr[i]); diff --git a/solution/1800-1899/1856.Maximum Subarray Min-Product/Solution.cpp b/solution/1800-1899/1856.Maximum Subarray Min-Product/Solution.cpp index 5d3daa1d93ad9..dc97ce1882652 100644 --- a/solution/1800-1899/1856.Maximum Subarray Min-Product/Solution.cpp +++ b/solution/1800-1899/1856.Maximum Subarray Min-Product/Solution.cpp @@ -5,15 +5,13 @@ class Solution { vector left(n, -1); vector right(n, n); stack stk; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { while (!stk.empty() && nums[stk.top()] >= nums[i]) stk.pop(); if (!stk.empty()) left[i] = stk.top(); stk.push(i); } stk = stack(); - for (int i = n - 1; i >= 0; --i) - { + for (int i = n - 1; i >= 0; --i) { while (!stk.empty() && nums[stk.top()] > nums[i]) stk.pop(); if (!stk.empty()) right[i] = stk.top(); stk.push(i); @@ -22,11 +20,10 @@ class Solution { for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; long long ans = 0; const int mod = 1e9 + 7; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { long long t = nums[i] * (s[right[i]] - s[left[i] + 1]); ans = max(ans, t); } - return (int) (ans % mod); + return (int)(ans % mod); } }; \ No newline at end of file diff --git a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.cpp b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.cpp index 37c533a7e4e40..97b4d5f63d44c 100644 --- a/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.cpp +++ b/solution/1800-1899/1857.Largest Color Value in a Directed Graph/Solution.cpp @@ -4,18 +4,15 @@ class Solution { int n = colors.size(); vector> g(n); vector indeg(n); - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; g[a].push_back(b); ++indeg[b]; } queue q; vector> dp(n, vector(26)); - for (int i = 0; i < n; ++i) - { - if (indeg[i] == 0) - { + for (int i = 0; i < n; ++i) { + if (indeg[i] == 0) { q.push(i); int c = colors[i] - 'a'; dp[i][c]++; @@ -23,17 +20,14 @@ class Solution { } int cnt = 0; int ans = 1; - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); ++cnt; - for (int j : g[i]) - { + for (int j : g[i]) { if (--indeg[j] == 0) q.push(j); int c = colors[j] - 'a'; - for (int k = 0; k < 26; ++k) - { + for (int k = 0; k < 26; ++k) { dp[j][k] = max(dp[j][k], dp[i][k] + (c == k)); ans = max(ans, dp[j][k]); } diff --git a/solution/1800-1899/1858.Longest Word With All Prefixes/Solution.cpp b/solution/1800-1899/1858.Longest Word With All Prefixes/Solution.cpp index 072ea1075d062..4f0e2554e15c6 100644 --- a/solution/1800-1899/1858.Longest Word With All Prefixes/Solution.cpp +++ b/solution/1800-1899/1858.Longest Word With All Prefixes/Solution.cpp @@ -2,24 +2,25 @@ class Trie { private: vector children; bool isEnd; + public: - Trie() : children(26), isEnd(false) {} - + Trie() + : children(26) + , isEnd(false) { } + void insert(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; if (!node->children[c]) node->children[c] = new Trie(); node = node->children[c]; } node->isEnd = true; } - + bool search(string word) { Trie* node = this; - for (char c : word) - { + for (char c : word) { c -= 'a'; node = node->children[c]; if (!node->isEnd) return false; @@ -34,8 +35,7 @@ class Solution { Trie* trie = new Trie(); for (auto w : words) trie->insert(w); string ans = ""; - for (auto w : words) - { + for (auto w : words) { if (ans != "" && (ans.size() > w.size() || (ans.size() == w.size() && ans < w))) continue; if (trie->search(w)) ans = w; } diff --git a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.cpp b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.cpp index a4995ead0e976..d708fbffce5b3 100644 --- a/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.cpp +++ b/solution/1800-1899/1869.Longer Contiguous Segments of Ones than Zeros/Solution.cpp @@ -3,15 +3,11 @@ class Solution { bool checkZeroOnes(string s) { int n0 = 0, n1 = 0; int t0 = 0, t1 = 0; - for (auto c : s) - { - if (c == '0') - { + for (auto c : s) { + if (c == '0') { ++t0; t1 = 0; - } - else - { + } else { ++t1; t0 = 0; } diff --git a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp index 37a7786cb8204..741a185b3a2fe 100644 --- a/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp +++ b/solution/1800-1899/1881.Maximum Value after Insertion/Solution.cpp @@ -2,8 +2,12 @@ class Solution { public: string maxValue(string n, int x) { int i = 0; - if (n[0] != '-') for (; i < n.size() && n[i] - '0' >= x; ++i); - else for (i = 1; i < n.size() && n[i] - '0' <= x; ++i); + if (n[0] != '-') + for (; i < n.size() && n[i] - '0' >= x; ++i) + ; + else + for (i = 1; i < n.size() && n[i] - '0' <= x; ++i) + ; return n.substr(0, i) + to_string(x) + n.substr(i); } }; \ No newline at end of file diff --git a/solution/1800-1899/1885.Count Pairs in Two Arrays/Solution.cpp b/solution/1800-1899/1885.Count Pairs in Two Arrays/Solution.cpp index f8d5d6712722f..7e0651959409f 100644 --- a/solution/1800-1899/1885.Count Pairs in Two Arrays/Solution.cpp +++ b/solution/1800-1899/1885.Count Pairs in Two Arrays/Solution.cpp @@ -6,8 +6,7 @@ class Solution { for (int i = 0; i < n; ++i) d[i] = nums1[i] - nums2[i]; sort(d.begin(), d.end()); long long ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int j = upper_bound(d.begin() + i + 1, d.end(), -d[i]) - d.begin(); ans += n - j; } diff --git a/solution/1800-1899/1886.Determine Whether Matrix Can Be Obtained By Rotation/Solution.cpp b/solution/1800-1899/1886.Determine Whether Matrix Can Be Obtained By Rotation/Solution.cpp index 19b1261a521e5..1ce36d2b25311 100644 --- a/solution/1800-1899/1886.Determine Whether Matrix Can Be Obtained By Rotation/Solution.cpp +++ b/solution/1800-1899/1886.Determine Whether Matrix Can Be Obtained By Rotation/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool findRotation(vector>& mat, vector>& target) { int n = mat.size(); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { vector> g(n, vector(n)); for (int i = 0; i < n; ++i) for (int j = 0; j < n; ++j) diff --git a/solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/Solution.cpp b/solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/Solution.cpp index 94febdc83de1e..a14a27668bc36 100644 --- a/solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/Solution.cpp +++ b/solution/1800-1899/1887.Reduction Operations to Make the Array Elements Equal/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int reductionOperations(vector& nums) { sort(nums.begin(), nums.end()); int ans = 0, cnt = 0; - for (int i = 1; i < nums.size(); ++i) - { + for (int i = 1; i < nums.size(); ++i) { cnt += nums[i] != nums[i - 1]; ans += cnt; } diff --git a/solution/1800-1899/1893.Check if All the Integers in a Range Are Covered/Solution.cpp b/solution/1800-1899/1893.Check if All the Integers in a Range Are Covered/Solution.cpp index 41d035ba6fc31..278ef58bbacd5 100644 --- a/solution/1800-1899/1893.Check if All the Integers in a Range Are Covered/Solution.cpp +++ b/solution/1800-1899/1893.Check if All the Integers in a Range Are Covered/Solution.cpp @@ -2,14 +2,12 @@ class Solution { public: bool isCovered(vector>& ranges, int left, int right) { vector d(52); - for (auto& e : ranges) - { + for (auto& e : ranges) { ++d[e[0]]; --d[e[1] + 1]; } int s = 0; - for (int i = 0; i < d.size(); ++i) - { + for (int i = 0; i < d.size(); ++i) { s += d[i]; if (left <= i && i <= right && s == 0) return false; } diff --git a/solution/1800-1899/1895.Largest Magic Square/Solution.cpp b/solution/1800-1899/1895.Largest Magic Square/Solution.cpp index 3474fe70ead22..8d9b15d394831 100644 --- a/solution/1800-1899/1895.Largest Magic Square/Solution.cpp +++ b/solution/1800-1899/1895.Largest Magic Square/Solution.cpp @@ -1,23 +1,18 @@ class Solution { public: - int largestMagicSquare(vector> &grid) { + int largestMagicSquare(vector>& grid) { int m = grid.size(), n = grid.size(); vector> rowsum(m + 1, vector(n + 1)); vector> colsum(m + 1, vector(n + 1)); - for (int i = 1; i <= m; ++i) - { - for (int j = 1; j <= n; ++j) - { + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { rowsum[i][j] = rowsum[i][j - 1] + grid[i - 1][j - 1]; colsum[i][j] = colsum[i - 1][j] + grid[i - 1][j - 1]; } } - for (int k = min(m, n); k > 1; --k) - { - for (int i = 0; i + k - 1 < m; ++i) - { - for (int j = 0; j + k - 1 < n; ++j) - { + for (int k = min(m, n); k > 1; --k) { + for (int i = 0; i + k - 1 < m; ++i) { + for (int j = 0; j + k - 1 < n; ++j) { int i2 = i + k - 1, j2 = j + k - 1; if (check(grid, rowsum, colsum, i, j, i2, j2)) return k; @@ -27,8 +22,7 @@ class Solution { return 1; } - bool check(vector> &grid, vector> &rowsum, vector> &colsum, int x1, int y1, int x2, int y2) - { + bool check(vector>& grid, vector>& rowsum, vector>& colsum, int x1, int y1, int x2, int y2) { int val = rowsum[x1 + 1][y2 + 1] - rowsum[x1 + 1][y1]; for (int i = x1 + 1; i <= x2; ++i) if (rowsum[i + 1][y2 + 1] - rowsum[i + 1][y1] != val) diff --git a/solution/1900-1999/1905.Count Sub Islands/Solution.cpp b/solution/1900-1999/1905.Count Sub Islands/Solution.cpp index db3ca2e1dcaca..b6bcc763de745 100644 --- a/solution/1900-1999/1905.Count Sub Islands/Solution.cpp +++ b/solution/1900-1999/1905.Count Sub Islands/Solution.cpp @@ -15,8 +15,7 @@ class Solution { bool ans = grid1[i][j]; grid2[i][j] = 0; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < m && y >= 0 && y < n && grid2[x][y] && !dfs(x, y, m, n, grid1, grid2)) ans = false; diff --git a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp index 687c51ec18187..d556f8519c213 100644 --- a/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp +++ b/solution/1900-1999/1909.Remove One Element to Make the Array Strictly Increasing/Solution.cpp @@ -2,7 +2,8 @@ class Solution { public: bool canBeIncreasing(vector& nums) { int i = 1, n = nums.size(); - for (; i < n && nums[i - 1] < nums[i]; ++i); + for (; i < n && nums[i - 1] < nums[i]; ++i) + ; return check(nums, i - 1) || check(nums, i); } diff --git a/solution/1900-1999/1915.Number of Wonderful Substrings/Solution.cpp b/solution/1900-1999/1915.Number of Wonderful Substrings/Solution.cpp index 63b7bdc265b05..b385292d20acc 100644 --- a/solution/1900-1999/1915.Number of Wonderful Substrings/Solution.cpp +++ b/solution/1900-1999/1915.Number of Wonderful Substrings/Solution.cpp @@ -5,8 +5,7 @@ class Solution { counter[0] = 1; long long ans = 0; int state = 0; - for (char c : word) - { + for (char c : word) { state ^= (1 << (c - 'a')); ans += counter[state]; for (int i = 0; i < 10; ++i) ans += counter[state ^ (1 << i)]; diff --git a/solution/1900-1999/1922.Count Good Numbers/Solution.cpp b/solution/1900-1999/1922.Count Good Numbers/Solution.cpp index 51edff29da420..c1e7b0e6ca3da 100644 --- a/solution/1900-1999/1922.Count Good Numbers/Solution.cpp +++ b/solution/1900-1999/1922.Count Good Numbers/Solution.cpp @@ -3,7 +3,7 @@ int MOD = 1000000007; class Solution { public: int countGoodNumbers(long long n) { - return (int) (myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD); + return (int)(myPow(5, (n + 1) >> 1) * myPow(4, n >> 1) % MOD); } private: diff --git a/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp b/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp index 89e0a390ffca4..978b69059566b 100644 --- a/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp +++ b/solution/1900-1999/1925.Count Square Sum Triples/Solution.cpp @@ -5,7 +5,7 @@ class Solution { for (int a = 1; a <= n; ++a) { for (int b = 1; b <= n; ++b) { int t = a * a + b * b; - int c = (int) sqrt(t); + int c = (int)sqrt(t); if (c <= n && c * c == t) { ++res; } diff --git a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp index 8eb6423cef00e..0b6eaebb28110 100644 --- a/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp +++ b/solution/1900-1999/1926.Nearest Exit from Entrance in Maze/Solution.cpp @@ -2,22 +2,18 @@ class Solution { public: int nearestExit(vector>& maze, vector& entrance) { int m = maze.size(), n = maze[0].size(); - queue> q{{entrance}}; + queue> q {{entrance}}; maze[entrance[0]][entrance[1]] = '+'; int ans = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { ++ans; - for (int k = q.size(); k > 0; --k) - { + for (int k = q.size(); k > 0; --k) { auto p = q.front(); q.pop(); - for (int l = 0; l < 4; ++l) - { + for (int l = 0; l < 4; ++l) { int x = p[0] + dirs[l], y = p[1] + dirs[l + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') - { + if (x >= 0 && x < m && y >= 0 && y < n && maze[x][y] == '.') { if (x == 0 || x == m - 1 || y == 0 || y == n - 1) return ans; q.push({x, y}); maze[x][y] = '+'; diff --git a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp index 84a9899645687..468038f394452 100644 --- a/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp +++ b/solution/1900-1999/1941.Check if All Characters Have Equal Number of Occurrences/Solution.cpp @@ -4,7 +4,8 @@ class Solution { vector cnt(26); for (char& c : s) ++cnt[c - 'a']; unordered_set ss; - for (int& v : cnt) if (v) ss.insert(v); + for (int& v : cnt) + if (v) ss.insert(v); return ss.size() == 1; } }; \ No newline at end of file diff --git a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp index 68fd591b48a35..9d8e503af4952 100644 --- a/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp +++ b/solution/1900-1999/1942.The Number of the Smallest Unoccupied Chair/Solution.cpp @@ -6,17 +6,14 @@ class Solution { priority_queue, greater> q; priority_queue, greater> busy; int n = times.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { times[i].push_back(i); q.push(i); } sort(times.begin(), times.end()); - for (auto& t : times) - { + for (auto& t : times) { int a = t[0], b = t[1], i = t[2]; - while (!busy.empty() && busy.top().first <= a) - { + while (!busy.empty() && busy.top().first <= a) { q.push(busy.top().second); busy.pop(); } diff --git a/solution/1900-1999/1943.Describe the Painting/Solution.cpp b/solution/1900-1999/1943.Describe the Painting/Solution.cpp index 7e81ac1a8d878..6214f331a1c6c 100644 --- a/solution/1900-1999/1943.Describe the Painting/Solution.cpp +++ b/solution/1900-1999/1943.Describe the Painting/Solution.cpp @@ -2,19 +2,17 @@ class Solution { public: vector> splitPainting(vector>& segments) { map d; - for (auto& e : segments) - { + for (auto& e : segments) { int l = e[0], r = e[1], c = e[2]; d[l] += c; d[r] -= c; } vector> ans; long long i, j, cur = 0; - for (auto& it : d) - { - if (it == *d.begin()) i = it.first; - else - { + for (auto& it : d) { + if (it == *d.begin()) + i = it.first; + else { j = it.first; if (cur > 0) ans.push_back({i, j, cur}); i = j; diff --git a/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp index 864140c0aff27..66fad52f4af1f 100644 --- a/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp +++ b/solution/1900-1999/1944.Number of Visible People in a Queue/Solution.cpp @@ -4,10 +4,8 @@ class Solution { int n = heights.size(); vector ans(n); stack stk; - for (int i = n - 1; i >= 0; --i) - { - while (!stk.empty()) - { + for (int i = n - 1; i >= 0; --i) { + while (!stk.empty()) { ans[i]++; if (heights[i] <= stk.top()) break; stk.pop(); diff --git a/solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.cpp b/solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.cpp index ff91e6a88eb25..50ea680f32aec 100644 --- a/solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.cpp +++ b/solution/1900-1999/1957.Delete Characters to Make Fancy String/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: string makeFancyString(string s) { string ans = ""; - for (char& c : s) - { + for (char& c : s) { int n = ans.size(); if (n > 1 && ans[n - 1] == c && ans[n - 2] == c) continue; ans.push_back(c); diff --git a/solution/1900-1999/1958.Check if Move is Legal/Solution.cpp b/solution/1900-1999/1958.Check if Move is Legal/Solution.cpp index 8de7e4377779a..412387039dbdd 100644 --- a/solution/1900-1999/1958.Check if Move is Legal/Solution.cpp +++ b/solution/1900-1999/1958.Check if Move is Legal/Solution.cpp @@ -4,13 +4,11 @@ class Solution { int n = 8; bool checkMove(vector>& board, int rMove, int cMove, char color) { - for (auto& d : dirs) - { + for (auto& d : dirs) { int a = d[0], b = d[1]; int i = rMove, j = cMove; int t = 0; - while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n) - { + while (0 <= i + a && i + a < n && 0 <= j + b && j + b < n) { ++t; i += a; j += b; diff --git a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.cpp b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.cpp index cbb9a4b299cad..87f11a53a74ac 100644 --- a/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.cpp +++ b/solution/1900-1999/1959.Minimum Total Space Wasted With K Resizing Operations/Solution.cpp @@ -4,11 +4,9 @@ class Solution { ++k; int n = nums.size(); vector> g(n, vector(n)); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int s = 0, mx = 0; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { mx = max(mx, nums[j]); s += nums[j]; g[i][j] = mx * (j - i + 1) - s; @@ -17,12 +15,9 @@ class Solution { int inf = 0x3f3f3f3f; vector> f(n + 1, vector(k + 1, inf)); f[0][0] = 0; - for (int i = 1; i <= n; ++i) - { - for (int j = 1; j <= k; ++j) - { - for (int h = 0; h < i; ++h) - { + for (int i = 1; i <= n; ++i) { + for (int j = 1; j <= k; ++j) { + for (int h = 0; h < i; ++h) { f[i][j] = min(f[i][j], f[h][j - 1] + g[h][i - 1]); } } diff --git a/solution/1900-1999/1961.Check If String Is a Prefix of Array/Solution.cpp b/solution/1900-1999/1961.Check If String Is a Prefix of Array/Solution.cpp index bc6a73214b65e..b800754597fb8 100644 --- a/solution/1900-1999/1961.Check If String Is a Prefix of Array/Solution.cpp +++ b/solution/1900-1999/1961.Check If String Is a Prefix of Array/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: bool isPrefixString(string s, vector& words) { string t = ""; - for (string& w : words) - { + for (string& w : words) { t += w; if (t.size() == s.size()) return t == s; } diff --git a/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.cpp b/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.cpp index 10b8c935be22b..a45fecd3a2219 100644 --- a/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.cpp +++ b/solution/1900-1999/1962.Remove Stones to Minimize the Total/Solution.cpp @@ -3,15 +3,13 @@ class Solution { int minStoneSum(vector& piles, int k) { priority_queue q; for (int& p : piles) q.push(p); - while (k--) - { + while (k--) { int p = q.top(); q.pop(); q.push((p + 1) >> 1); } int ans = 0; - while (!q.empty()) - { + while (!q.empty()) { ans += q.top(); q.pop(); } diff --git a/solution/1900-1999/1963.Minimum Number of Swaps to Make the String Balanced/Solution.cpp b/solution/1900-1999/1963.Minimum Number of Swaps to Make the String Balanced/Solution.cpp index ef4e693fc38b1..1cc6cccaa07e0 100644 --- a/solution/1900-1999/1963.Minimum Number of Swaps to Make the String Balanced/Solution.cpp +++ b/solution/1900-1999/1963.Minimum Number of Swaps to Make the String Balanced/Solution.cpp @@ -2,10 +2,11 @@ class Solution { public: int minSwaps(string s) { int ans = 0; - for (char& c : s) - { - if (c == '[') ++ans; - else if (ans) --ans; + for (char& c : s) { + if (c == '[') + ++ans; + else if (ans) + --ans; } return (ans + 1) >> 1; } diff --git a/solution/1900-1999/1964.Find the Longest Valid Obstacle Course at Each Position/Solution.cpp b/solution/1900-1999/1964.Find the Longest Valid Obstacle Course at Each Position/Solution.cpp index ef6fe91022ea4..4336385b74929 100644 --- a/solution/1900-1999/1964.Find the Longest Valid Obstacle Course at Each Position/Solution.cpp +++ b/solution/1900-1999/1964.Find the Longest Valid Obstacle Course at Each Position/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int val) { - while (x <= n) - { + while (x <= n) { c[x] = max(c[x], val); x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s = max(s, c[x]); x -= lowbit(x); } @@ -28,7 +28,6 @@ class BinaryIndexedTree { } }; - class Solution { public: vector longestObstacleCourseAtEachPosition(vector& obstacles) { @@ -39,8 +38,7 @@ class Solution { BinaryIndexedTree* tree = new BinaryIndexedTree(m.size()); int n = obstacles.size(); vector ans(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = obstacles[i]; int x = m[v]; ans[i] = 1 + tree->query(x); diff --git a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.cpp b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.cpp index 8d0eefeeae4cd..988d31072fba7 100644 --- a/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.cpp +++ b/solution/1900-1999/1967.Number of Strings That Appear as Substrings in Word/Solution.cpp @@ -1,6 +1,6 @@ class Solution { public: - int numOfStrings(vector &patterns, string word) { + int numOfStrings(vector& patterns, string word) { int res = 0; for (auto p : patterns) if (word.find(p) != string::npos) diff --git a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp index b2c3f9c4e2130..cfce7a330831d 100644 --- a/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp +++ b/solution/1900-1999/1968.Array With Elements Not Equal to Average of Neighbors/Solution.cpp @@ -5,8 +5,7 @@ class Solution { vector ans; int n = nums.size(); int m = (n + 1) >> 1; - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { ans.push_back(nums[i]); if (i + m < n) ans.push_back(nums[i + m]); } diff --git a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp index 978f6cb8002a4..0d26dfbf279f3 100644 --- a/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp +++ b/solution/1900-1999/1970.Last Day Where You Can Still Cross/Solution.cpp @@ -12,14 +12,11 @@ class Solution { for (int i = 0; i < p.size(); ++i) p[i] = i; vector> grid(row, vector(col, false)); int top = n, bottom = n + 1; - for (int k = cells.size() - 1; k >= 0; --k) - { + for (int k = cells.size() - 1; k >= 0; --k) { int i = cells[k][0] - 1, j = cells[k][1] - 1; grid[i][j] = true; - for (auto e : dirs) - { - if (check(i + e[0], j + e[1], grid)) - { + for (auto e : dirs) { + if (check(i + e[0], j + e[1], grid)) { p[find(i * col + j)] = find((i + e[0]) * col + j + e[1]); } } diff --git a/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.cpp b/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.cpp index 026450f955878..aeac22b853990 100644 --- a/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.cpp +++ b/solution/1900-1999/1971.Find if Path Exists in Graph/Solution.cpp @@ -6,7 +6,7 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; for (auto& e : edges) p[find(e[0])] = find(e[1]); - return find(source) == find(destination); + return find(source) == find(destination); } int find(int x) { diff --git a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp index 40d860fead8b8..3965a90f69e05 100644 --- a/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp +++ b/solution/1900-1999/1974.Minimum Time to Type Word Using Special Typewriter/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minTimeToType(string word) { int ans = 0; int prev = 0; - for (char& c : word) - { + for (char& c : word) { int curr = c - 'a'; int t = abs(prev - curr); t = min(t, 26 - t); diff --git a/solution/1900-1999/1976.Number of Ways to Arrive at Destination/Solution.cpp b/solution/1900-1999/1976.Number of Ways to Arrive at Destination/Solution.cpp index 2dc9c4d3d826c..992670d72eec2 100644 --- a/solution/1900-1999/1976.Number of Ways to Arrive at Destination/Solution.cpp +++ b/solution/1900-1999/1976.Number of Ways to Arrive at Destination/Solution.cpp @@ -10,8 +10,7 @@ class Solution { vector dist(n, INF); vector w(n); vector vis(n); - for (auto& r : roads) - { + for (auto& r : roads) { int u = r[0], v = r[1], t = r[2]; g[u][v] = t; g[v][u] = t; @@ -19,24 +18,20 @@ class Solution { g[0][0] = 0; dist[0] = 0; w[0] = 1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int t = -1; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { if (!vis[j] && (t == -1 || dist[t] > dist[j])) t = j; } vis[t] = true; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { if (t == j) continue; ll ne = dist[t] + g[t][j]; - if (dist[j] > ne) - { + if (dist[j] > ne) { dist[j] = ne; w[j] = w[t]; - } - else if (dist[j] == ne) w[j] = (w[j] + w[t]) % MOD; + } else if (dist[j] == ne) + w[j] = (w[j] + w[t]) % MOD; } } return w[n - 1]; diff --git a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp index 49146636ddc8a..efc42553a1939 100644 --- a/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp +++ b/solution/1900-1999/1979.Find Greatest Common Divisor of Array/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int findGCD(vector& nums) { int a = 0, b = 1000; - for (int num : nums) - { + for (int num : nums) { a = max(a, num); b = min(b, num); } diff --git a/solution/1900-1999/1980.Find Unique Binary String/Solution.cpp b/solution/1900-1999/1980.Find Unique Binary String/Solution.cpp index bff798e8fc05c..f0b027e19ac61 100644 --- a/solution/1900-1999/1980.Find Unique Binary String/Solution.cpp +++ b/solution/1900-1999/1980.Find Unique Binary String/Solution.cpp @@ -1,22 +1,19 @@ class Solution { public: - string findDifferentBinaryString(vector &nums) { + string findDifferentBinaryString(vector& nums) { auto s = count(nums); - for (int i = 0, n = nums.size(); i < n + 1; ++i) - { + for (int i = 0, n = nums.size(); i < n + 1; ++i) { if (!s.count(i)) return repeat("1", i) + repeat("0", n - i); } return ""; } - unordered_set count(vector &nums) { + unordered_set count(vector& nums) { unordered_set s; - for (auto &num : nums) - { + for (auto& num : nums) { int t = 0; - for (char c : num) - { + for (char c : num) { if (c == '1') ++t; } @@ -27,8 +24,7 @@ class Solution { string repeat(string s, int n) { string res = ""; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { res += s; } return res; diff --git a/solution/1900-1999/1991.Find the Middle Index in Array/Solution.cpp b/solution/1900-1999/1991.Find the Middle Index in Array/Solution.cpp index e9a93ebd52f87..2cd9615648c4c 100644 --- a/solution/1900-1999/1991.Find the Middle Index in Array/Solution.cpp +++ b/solution/1900-1999/1991.Find the Middle Index in Array/Solution.cpp @@ -3,7 +3,7 @@ class Solution { int findMiddleIndex(vector& nums) { int sum = 0; int total = 0; - for (int num: nums) + for (int num : nums) sum += num; for (int i = 0; i < nums.size(); i++) { diff --git a/solution/1900-1999/1992.Find All Groups of Farmland/Solution.cpp b/solution/1900-1999/1992.Find All Groups of Farmland/Solution.cpp index bc7d9249de15d..b80daf002d742 100644 --- a/solution/1900-1999/1992.Find All Groups of Farmland/Solution.cpp +++ b/solution/1900-1999/1992.Find All Groups of Farmland/Solution.cpp @@ -4,15 +4,15 @@ class Solution { vector> ans; int m = land.size(); int n = land[0].size(); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (land[i][j] == 0 || (j > 0 && land[i][j-1] == 1) || (i > 0 && land[i-1][j] == 1)) continue; + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (land[i][j] == 0 || (j > 0 && land[i][j - 1] == 1) || (i > 0 && land[i - 1][j] == 1)) continue; int x = i; int y = j; - for (; x + 1 < m && land[x + 1][j] == 1; ++x); - for (; y + 1 < n && land[x][y + 1] == 1; ++y); + for (; x + 1 < m && land[x + 1][j] == 1; ++x) + ; + for (; y + 1 < n && land[x][y + 1] == 1; ++y) + ; ans.push_back({i, j, x, y}); } } diff --git a/solution/1900-1999/1993.Operations on Tree/Solution.cpp b/solution/1900-1999/1993.Operations on Tree/Solution.cpp index 9cbd5b9928ab2..8f7f4dd9f1b62 100644 --- a/solution/1900-1999/1993.Operations on Tree/Solution.cpp +++ b/solution/1900-1999/1993.Operations on Tree/Solution.cpp @@ -12,19 +12,19 @@ class LockingTree { if (parent[i] != -1) children[parent[i]].push_back(i); } - + bool lock(int num, int user) { if (nums.count(num)) return false; nums[num] = user; return true; } - + bool unlock(int num, int user) { if (!nums.count(num) || nums[num] != user) return false; nums.erase(num); return true; } - + bool upgrade(int num, int user) { for (int t = num; t != -1; t = parent[t]) if (nums.count(t)) @@ -37,10 +37,8 @@ class LockingTree { } void dfs(int num, bool& find) { - for (int child : children[num]) - { - if (nums.count(child)) - { + for (int child : children[num]) { + if (nums.count(child)) { nums.erase(child); find = true; } diff --git a/solution/1900-1999/1994.The Number of Good Subsets/Solution.cpp b/solution/1900-1999/1994.The Number of Good Subsets/Solution.cpp index 9444361b5a6bb..cf5df3198491b 100644 --- a/solution/1900-1999/1994.The Number of Good Subsets/Solution.cpp +++ b/solution/1900-1999/1994.The Number of Good Subsets/Solution.cpp @@ -8,15 +8,13 @@ class Solution { int n = prime.size(); vector dp(1 << n); dp[0] = 1; - for (int x = 2; x <= 30; ++x) - { + for (int x = 2; x <= 30; ++x) { if (counter[x] == 0 || x % 4 == 0 || x % 9 == 0 || x % 25 == 0) continue; int mask = 0; for (int i = 0; i < n; ++i) if (x % prime[i] == 0) mask |= (1 << i); - for (int state = 0; state < 1 << n; ++state) - { + for (int state = 0; state < 1 << n; ++state) { if ((mask & state) > 0) continue; dp[mask | state] = (dp[mask | state] + counter[x] * dp[state]) % MOD; } @@ -24,6 +22,6 @@ class Solution { long long ans = 0; for (int i = 1; i < 1 << n; ++i) ans = (ans + dp[i]) % MOD; while (counter[1]--) ans = (ans << 1) % MOD; - return (int) ans; + return (int)ans; } }; \ No newline at end of file diff --git a/solution/1900-1999/1995.Count Special Quadruplets/Solution.cpp b/solution/1900-1999/1995.Count Special Quadruplets/Solution.cpp index e0d01e6f0f44f..1e682d8fecbd5 100644 --- a/solution/1900-1999/1995.Count Special Quadruplets/Solution.cpp +++ b/solution/1900-1999/1995.Count Special Quadruplets/Solution.cpp @@ -3,13 +3,10 @@ class Solution { int countQuadruplets(vector& nums) { int ans = 0, n = nums.size(); vector counter(310); - for (int b = n - 3; b > 0; --b) - { + for (int b = n - 3; b > 0; --b) { int c = b + 1; - for (int d = c + 1; d < n; ++d) - { - if (nums[d] - nums[c] >= 0) - { + for (int d = c + 1; d < n; ++d) { + if (nums[d] - nums[c] >= 0) { ++counter[nums[d] - nums[c]]; } } diff --git a/solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.cpp b/solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.cpp index 17940398b95cd..44347d95edc69 100644 --- a/solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.cpp +++ b/solution/1900-1999/1996.The Number of Weak Characters in the Game/Solution.cpp @@ -1,13 +1,13 @@ class Solution { public: - int numberOfWeakCharacters(vector> &properties) { - sort(properties.begin(), properties.end(), [&](vector &a, vector &b) - { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; }); + int numberOfWeakCharacters(vector>& properties) { + sort(properties.begin(), properties.end(), [&](vector& a, vector& b) { return a[0] == b[0] ? a[1] < b[1] : a[0] > b[0]; }); int ans = 0, mx = 0; - for (auto &p : properties) - { - if (mx > p[1]) ++ans; - else mx = p[1]; + for (auto& p : properties) { + if (mx > p[1]) + ++ans; + else + mx = p[1]; } return ans; } diff --git a/solution/1900-1999/1998.GCD Sort of an Array/Solution.cpp b/solution/1900-1999/1998.GCD Sort of an Array/Solution.cpp index fb9e10f67f648..cb70406e24966 100644 --- a/solution/1900-1999/1998.GCD Sort of an Array/Solution.cpp +++ b/solution/1900-1999/1998.GCD Sort of an Array/Solution.cpp @@ -9,19 +9,16 @@ class Solution { int mx = 0; for (auto num : nums) mx = max(mx, num); unordered_map> f; - for (int i = 2; i <= mx; ++i) - { + for (int i = 2; i <= mx; ++i) { if (!f[i].empty()) continue; for (int j = i; j <= mx; j += i) f[j].push_back(i); } - for (int i : nums) - { + for (int i : nums) { for (int j : f[i]) p[find(i)] = find(j); } vector s = nums; sort(s.begin(), s.end()); - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { if (s[i] != nums[i] && find(s[i]) != find(nums[i])) return false; } return true; diff --git a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.cpp b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.cpp index 91cada7e8284f..738c73d4ea294 100644 --- a/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.cpp +++ b/solution/2000-2099/2006.Count Number of Pairs With Absolute Difference K/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int countKDifference(vector& nums, int k) { int ans = 0; vector counter(110); - for (int num : nums) - { + for (int num : nums) { if (num >= k) ans += counter[num - k]; if (num + k <= 100) ans += counter[num + k]; ++counter[num]; diff --git a/solution/2000-2099/2007.Find Original Array From Doubled Array/Solution.cpp b/solution/2000-2099/2007.Find Original Array From Doubled Array/Solution.cpp index bf6e43f4f05db..1d7286b1ae1bc 100644 --- a/solution/2000-2099/2007.Find Original Array From Doubled Array/Solution.cpp +++ b/solution/2000-2099/2007.Find Original Array From Doubled Array/Solution.cpp @@ -8,8 +8,7 @@ class Solution { if (counter[0] % 2 != 0) return {}; vector res(changed.size() / 2); int j = counter[0] / 2; - for (int i = 1; i < n; ++i) - { + for (int i = 1; i < n; ++i) { if (counter[i] == 0) continue; if (i * 2 >= n || counter[i] > counter[i * 2]) return {}; counter[i * 2] -= counter[i]; diff --git a/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.cpp b/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.cpp index fd4d9963f16a0..db2f340c793c3 100644 --- a/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.cpp +++ b/solution/2000-2099/2012.Sum of Beauty in the Array/Solution.cpp @@ -7,10 +7,11 @@ class Solution { for (int i = 1; i < n; ++i) lmx[i] = max(lmx[i - 1], nums[i - 1]); for (int i = n - 2; i >= 0; --i) rmi[i] = min(rmi[i + 1], nums[i + 1]); int ans = 0; - for (int i = 1; i < n - 1; ++i) - { - if (lmx[i] < nums[i] && nums[i] < rmi[i]) ans += 2; - else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) ans += 1; + for (int i = 1; i < n - 1; ++i) { + if (lmx[i] < nums[i] && nums[i] < rmi[i]) + ans += 2; + else if (nums[i - 1] < nums[i] && nums[i] < nums[i + 1]) + ans += 1; } return ans; } diff --git a/solution/2000-2099/2013.Detect Squares/Solution.cpp b/solution/2000-2099/2013.Detect Squares/Solution.cpp index 96f9256189a39..f4d51900e9204 100644 --- a/solution/2000-2099/2013.Detect Squares/Solution.cpp +++ b/solution/2000-2099/2013.Detect Squares/Solution.cpp @@ -3,25 +3,22 @@ class DetectSquares { unordered_map> mp; DetectSquares() { - } - + void add(vector point) { int x = point[0], y = point[1]; ++mp[x][y]; } - + int count(vector point) { int x = point[0], y = point[1]; int ans = 0; if (!mp.count(x)) return ans; auto xcnt = mp[x]; - for (auto e : mp) - { + for (auto e : mp) { int x1 = e.first; auto counter = e.second; - if (x1 != x) - { + if (x1 != x) { int d = x1 - x; ans += xcnt[y + d] * counter[y] * counter[y + d]; ans += xcnt[y - d] * counter[y] * counter[y - d]; diff --git a/solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.cpp b/solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.cpp index 60ff69b2f3b46..64afe12249446 100644 --- a/solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.cpp +++ b/solution/2000-2099/2016.Maximum Difference Between Increasing Elements/Solution.cpp @@ -3,10 +3,11 @@ class Solution { int maximumDifference(vector& nums) { int mi = nums[0]; int ans = -1; - for (int i = 1, n = nums.size(); i < n; ++i) - { - if (nums[i] > mi) ans = max(ans, nums[i] - mi); - else mi = nums[i]; + for (int i = 1, n = nums.size(); i < n; ++i) { + if (nums[i] > mi) + ans = max(ans, nums[i] - mi); + else + mi = nums[i]; } return ans; } diff --git a/solution/2000-2099/2017.Grid Game/Solution.cpp b/solution/2000-2099/2017.Grid Game/Solution.cpp index 7eddd90013cd7..33cf6baf1a96c 100644 --- a/solution/2000-2099/2017.Grid Game/Solution.cpp +++ b/solution/2000-2099/2017.Grid Game/Solution.cpp @@ -7,8 +7,7 @@ class Solution { int n = grid[0].size(); ll s1 = 0, s2 = 0; for (int& v : grid[0]) s1 += v; - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { s1 -= grid[0][j]; ans = min(ans, max(s1, s2)); s2 += grid[1][j]; diff --git a/solution/2000-2099/2021.Brightest Position on Street/Solution.cpp b/solution/2000-2099/2021.Brightest Position on Street/Solution.cpp index 66591fc893eab..116f6d8eab518 100644 --- a/solution/2000-2099/2021.Brightest Position on Street/Solution.cpp +++ b/solution/2000-2099/2021.Brightest Position on Street/Solution.cpp @@ -2,18 +2,15 @@ class Solution { public: int brightestPosition(vector>& lights) { map d; - for (auto& e : lights) - { + for (auto& e : lights) { int l = e[0] - e[1], r = e[0] + e[1]; ++d[l]; --d[r + 1]; } int s = 0, mx = 0, ans = 0; - for (auto& e : d) - { + for (auto& e : d) { s += e.second; - if (s > mx) - { + if (s > mx) { mx = s; ans = e.first; } diff --git a/solution/2000-2099/2022.Convert 1D Array Into 2D Array/Solution.cpp b/solution/2000-2099/2022.Convert 1D Array Into 2D Array/Solution.cpp index 0056319c12508..e71c0a9b71c05 100644 --- a/solution/2000-2099/2022.Convert 1D Array Into 2D Array/Solution.cpp +++ b/solution/2000-2099/2022.Convert 1D Array Into 2D Array/Solution.cpp @@ -3,10 +3,8 @@ class Solution { vector> construct2DArray(vector& original, int m, int n) { if (m * n != original.size()) return {}; vector> ans(m, vector(n, 0)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { ans[i][j] = original[i * n + j]; } } diff --git a/solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/Solution.cpp b/solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/Solution.cpp index edfa0664d6e72..1a36f0e37081e 100644 --- a/solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/Solution.cpp +++ b/solution/2000-2099/2023.Number of Pairs of Strings With Concatenation Equal to Target/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int numOfPairs(vector& nums, string target) { int n = nums.size(); int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { if (i != j && nums[i] + nums[j] == target) ++ans; } } diff --git a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp index 9111e8bb8f762..8658ef1a080d2 100644 --- a/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp +++ b/solution/2000-2099/2024.Maximize the Confusion of an Exam/Solution.cpp @@ -6,8 +6,7 @@ class Solution { int get(char c, int k, string& answerKey) { int l = 0, r = 0; - while (r < answerKey.size()) - { + while (r < answerKey.size()) { if (answerKey[r++] == c) --k; if (k < 0 && answerKey[l++] == c) ++k; } diff --git a/solution/2000-2099/2029.Stone Game IX/Solution.cpp b/solution/2000-2099/2029.Stone Game IX/Solution.cpp index 3f3d1b166d29f..e79b87c2d530d 100644 --- a/solution/2000-2099/2029.Stone Game IX/Solution.cpp +++ b/solution/2000-2099/2029.Stone Game IX/Solution.cpp @@ -11,8 +11,7 @@ class Solution { if (c[1] == 0) return false; --c[1]; int turn = 1 + min(c[1], c[2]) * 2 + c[0]; - if (c[1] > c[2]) - { + if (c[1] > c[2]) { --c[1]; ++turn; } diff --git a/solution/2000-2099/2031.Count Subarrays With More Ones Than Zeros/Solution.cpp b/solution/2000-2099/2031.Count Subarrays With More Ones Than Zeros/Solution.cpp index a0c5984f5e857..1a083e33227ae 100644 --- a/solution/2000-2099/2031.Count Subarrays With More Ones Than Zeros/Solution.cpp +++ b/solution/2000-2099/2031.Count Subarrays With More Ones Than Zeros/Solution.cpp @@ -3,12 +3,13 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n + 1e5 + 1), c(_n + 1 + 1e5 + 1){} + BinaryIndexedTree(int _n) + : n(_n + 1e5 + 1) + , c(_n + 1 + 1e5 + 1) { } void update(int x, int delta) { x += 1e5 + 1; - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -17,8 +18,7 @@ class BinaryIndexedTree { int query(int x) { x += 1e5 + 1; int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -40,8 +40,7 @@ class Solution { BinaryIndexedTree* tree = new BinaryIndexedTree(n + 1); int ans = 0; const int MOD = 1e9 + 7; - for (int v : s) - { + for (int v : s) { ans = (ans + tree->query(v - 1)) % MOD; tree->update(v, 1); } diff --git a/solution/2000-2099/2032.Two Out of Three/Solution.cpp b/solution/2000-2099/2032.Two Out of Three/Solution.cpp index 29fe9f258f11f..b32685c317875 100644 --- a/solution/2000-2099/2032.Two Out of Three/Solution.cpp +++ b/solution/2000-2099/2032.Two Out of Three/Solution.cpp @@ -4,7 +4,7 @@ class Solution { auto s1 = get(nums1), s2 = get(nums2), s3 = get(nums3); vector ans; for (int i = 1; i <= 100; ++i) - if (s1[i] + s2[i] + s3[i] > 1) + if (s1[i] + s2[i] + s3[i] > 1) ans.push_back(i); return ans; } diff --git a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.cpp b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.cpp index fdef2dae3f0e9..091a9e8efd538 100644 --- a/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.cpp +++ b/solution/2000-2099/2033.Minimum Operations to Make a Uni-Value Grid/Solution.cpp @@ -4,10 +4,8 @@ class Solution { vector nums; int m = grid.size(), n = grid[0].size(); int base = grid[0][0]; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (abs(grid[i][j] - base) % x != 0) return -1; nums.push_back(grid[i][j]); } diff --git a/solution/2000-2099/2034.Stock Price Fluctuation/Solution.cpp b/solution/2000-2099/2034.Stock Price Fluctuation/Solution.cpp index 61a905026715c..be492bc4ddb33 100644 --- a/solution/2000-2099/2034.Stock Price Fluctuation/Solution.cpp +++ b/solution/2000-2099/2034.Stock Price Fluctuation/Solution.cpp @@ -5,12 +5,10 @@ class StockPrice { map counter; StockPrice() { - } - + void update(int timestamp, int price) { - if (mp.count(timestamp)) - { + if (mp.count(timestamp)) { int oldPrice = mp[timestamp]; --counter[oldPrice]; if (counter[oldPrice] == 0) counter.erase(oldPrice); @@ -19,15 +17,15 @@ class StockPrice { ++counter[price]; lastTs = max(lastTs, timestamp); } - + int current() { return mp[lastTs]; } - + int maximum() { return counter.rbegin()->first; } - + int minimum() { return counter.begin()->first; } diff --git a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.cpp b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.cpp index e71fc2277688e..dd091b5a94bd1 100644 --- a/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.cpp +++ b/solution/2000-2099/2035.Partition Array Into Two Arrays to Minimize Sum Difference/Solution.cpp @@ -3,21 +3,16 @@ class Solution { int minimumDifference(vector& nums) { int n = nums.size() >> 1; vector> f(n + 1), g(n + 1); - for (int i = 0; i < (1 << n); ++i) - { + for (int i = 0; i < (1 << n); ++i) { int s = 0, cnt = 0; int s1 = 0, cnt1 = 0; - for (int j = 0; j < n; ++j) - { - if (i & (1 << j)) - { + for (int j = 0; j < n; ++j) { + if (i & (1 << j)) { s += nums[j]; ++cnt; s1 += nums[n + j]; ++cnt1; - } - else - { + } else { s -= nums[j]; s1 -= nums[n + j]; } @@ -25,23 +20,21 @@ class Solution { f[cnt].push_back(s); g[cnt1].push_back(s1); } - for (int i = 0; i <= n; ++i) - { + for (int i = 0; i <= n; ++i) { sort(f[i].begin(), f[i].end()); sort(g[i].begin(), g[i].end()); } int ans = INT_MAX; - for (int i = 0; i <= n; ++i) - { - for (int a : f[i]) - { + for (int i = 0; i <= n; ++i) { + for (int a : f[i]) { int left = 0, right = g[n - i].size() - 1; int b = -a; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (g[n - i][mid] >= b) right = mid; - else left = mid + 1; + if (g[n - i][mid] >= b) + right = mid; + else + left = mid + 1; } ans = min(ans, abs(a + g[n - i][left])); if (left > 0) ans = min(ans, abs(a + g[n - i][left - 1])); diff --git a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.cpp b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.cpp index 25f1ae465858a..a0607c0c8aac3 100644 --- a/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.cpp +++ b/solution/2000-2099/2038.Remove Colored Pieces if Both Neighbors are the Same Color/Solution.cpp @@ -3,16 +3,12 @@ class Solution { bool winnerOfGame(string colors) { int a = 0, b = 0; int cnt1 = 0, cnt2 = 0; - for (char& c : colors) - { - if (c == 'A') - { + for (char& c : colors) { + if (c == 'A') { ++a; if (a > 2) ++cnt1; b = 0; - } - else - { + } else { ++b; if (b > 2) ++cnt2; a = 0; diff --git a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp index c961b3beac2cb..8f6ae2e647cd2 100644 --- a/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp +++ b/solution/2000-2099/2039.The Time When the Network Becomes Idle/Solution.cpp @@ -4,24 +4,20 @@ class Solution { int n = patience.size(); vector> g(n); vector vis(n); - for (auto& e : edges) - { + for (auto& e : edges) { int u = e[0], v = e[1]; g[u].push_back(v); g[v].push_back(u); } - queue q{{0}}; + queue q {{0}}; vis[0] = true; int ans = 0, step = 0; - while (!q.empty()) - { + while (!q.empty()) { ++step; - for (int i = q.size(); i > 0; --i) - { + for (int i = q.size(); i > 0; --i) { int u = q.front(); q.pop(); - for (int v : g[u]) - { + for (int v : g[u]) { if (vis[v]) continue; vis[v] = true; q.push(v); diff --git a/solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.cpp b/solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.cpp index 0fd1a5b0f69a5..7e4c0a79acbd0 100644 --- a/solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.cpp +++ b/solution/2000-2099/2042.Check if Numbers Are Ascending in a Sentence/Solution.cpp @@ -4,10 +4,8 @@ class Solution { int curr = 0; istringstream is(s); string t; - while (is >> t) - { - if (isdigit(t[0])) - { + while (is >> t) { + if (isdigit(t[0])) { int x = stoi(t); if (curr >= x) return false; curr = x; diff --git a/solution/2000-2099/2043.Simple Bank System/Solution.cpp b/solution/2000-2099/2043.Simple Bank System/Solution.cpp index ef1dc0988499b..640887e266d1c 100644 --- a/solution/2000-2099/2043.Simple Bank System/Solution.cpp +++ b/solution/2000-2099/2043.Simple Bank System/Solution.cpp @@ -7,20 +7,20 @@ class Bank { this->balance = balance; n = balance.size(); } - + bool transfer(int account1, int account2, long long money) { if (account1 > n || account2 > n || balance[account1 - 1] < money) return false; balance[account1 - 1] -= money; balance[account2 - 1] += money; return true; } - + bool deposit(int account, long long money) { if (account > n) return false; balance[account - 1] += money; return true; } - + bool withdraw(int account, long long money) { if (account > n || balance[account - 1] < money) return false; balance[account - 1] -= money; diff --git a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp index 2d65e4e246ebe..c8939eb26024f 100644 --- a/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp +++ b/solution/2000-2099/2044.Count Number of Maximum Bitwise-OR Subsets/Solution.cpp @@ -14,8 +14,7 @@ class Solution { } void dfs(int i, int t) { - if (i == nums.size()) - { + if (i == nums.size()) { if (t == mx) ++ans; return; } diff --git a/solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/Solution.cpp b/solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/Solution.cpp index 93330fd7e9dc5..9f40a723ed528 100644 --- a/solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/Solution.cpp +++ b/solution/2000-2099/2046.Sort Linked List Already Sorted Using Absolute Values/Solution.cpp @@ -13,18 +13,14 @@ class Solution { ListNode* sortLinkedList(ListNode* head) { ListNode* prev = head; ListNode* curr = head->next; - while (curr) - { - if (curr->val < 0) - { + while (curr) { + if (curr->val < 0) { auto t = curr->next; prev->next = t; curr->next = head; head = curr; curr = t; - } - else - { + } else { prev = curr; curr = curr->next; } diff --git a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.cpp b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.cpp index f1a2d8d9cd5a8..21f2ef955d6c7 100644 --- a/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.cpp +++ b/solution/2000-2099/2048.Next Greater Numerically Balanced Number/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: int nextBeautifulNumber(int n) { - for (int i = n + 1; i < 10000000; ++i) - { + for (int i = n + 1; i < 10000000; ++i) { if (check(i)) return i; } return -1; @@ -12,8 +11,7 @@ class Solution { string s = to_string(num); vector counter(10); for (char c : s) ++counter[c - '0']; - for (char c : s) - { + for (char c : s) { if (counter[c - '0'] != c - '0') return false; } return true; diff --git a/solution/2000-2099/2049.Count Nodes With the Highest Score/Solution.cpp b/solution/2000-2099/2049.Count Nodes With the Highest Score/Solution.cpp index e6e3947afac07..c46e392a81307 100644 --- a/solution/2000-2099/2049.Count Nodes With the Highest Score/Solution.cpp +++ b/solution/2000-2099/2049.Count Nodes With the Highest Score/Solution.cpp @@ -17,19 +17,17 @@ class Solution { int dfs(int u, unordered_map>& g) { int size = 1; long long score = 1; - for (int v : g[u]) - { + for (int v : g[u]) { int t = dfs(v, g); size += t; score *= t; } if (u > 0) score *= (n - size); - if (score > maxScore) - { + if (score > maxScore) { maxScore = score; ans = 1; - } - else if (score == maxScore) ++ans; + } else if (score == maxScore) + ++ans; return size; } }; \ No newline at end of file diff --git a/solution/2000-2099/2050.Parallel Courses III/Solution.cpp b/solution/2000-2099/2050.Parallel Courses III/Solution.cpp index 75ba0c659d147..8f2d8c4fdd3c9 100644 --- a/solution/2000-2099/2050.Parallel Courses III/Solution.cpp +++ b/solution/2000-2099/2050.Parallel Courses III/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minimumTime(int n, vector>& relations, vector& time) { vector> g(n); vector indeg(n); - for (auto& e : relations) - { + for (auto& e : relations) { int a = e[0] - 1, b = e[1] - 1; g[a].push_back(b); ++indeg[b]; @@ -12,22 +11,18 @@ class Solution { queue q; vector dp(n); int ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = indeg[i], t = time[i]; - if (v == 0) - { + if (v == 0) { q.push(i); dp[i] = t; ans = max(ans, t); } } - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); - for (int j : g[i]) - { + for (int j : g[i]) { if (--indeg[j] == 0) q.push(j); dp[j] = max(dp[j], dp[i] + time[j]); ans = max(ans, dp[j]); diff --git a/solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/Solution.cpp b/solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/Solution.cpp index e030efe38983c..54dd9c745a0d0 100644 --- a/solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/Solution.cpp +++ b/solution/2000-2099/2052.Minimum Cost to Separate Sentence Into Rows/Solution.cpp @@ -17,14 +17,12 @@ class Solution { int dfs(int i, int k, vector& s, vector& memo) { if (memo[i] != inf) return memo[i]; - if (s[n] - s[i] + n - i - 1 <= k) - { + if (s[n] - s[i] + n - i - 1 <= k) { memo[i] = 0; return 0; } int ans = inf; - for (int j = i + 1; j < n; ++j) - { + for (int j = i + 1; j < n; ++j) { int t = s[j] - s[i] + j - i - 1; if (t <= k) ans = min(ans, (k - t) * (k - t) + dfs(j, k, s, memo)); } diff --git a/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.cpp b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.cpp index fe3ab9065a841..b31ef8e13ea43 100644 --- a/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.cpp +++ b/solution/2000-2099/2053.Kth Distinct String in an Array/Solution.cpp @@ -3,10 +3,8 @@ class Solution { string kthDistinct(vector& arr, int k) { unordered_map counter; for (auto& v : arr) ++counter[v]; - for (auto& v : arr) - { - if (counter[v] == 1) - { + for (auto& v : arr) { + if (counter[v] == 1) { --k; if (k == 0) return v; } diff --git a/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp b/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp index 5e19d1da45145..8e9ee1c17e6aa 100644 --- a/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp +++ b/solution/2000-2099/2054.Two Best Non-Overlapping Events/Solution.cpp @@ -6,15 +6,15 @@ class Solution { vector f(n + 1); for (int i = n - 1; ~i; --i) f[i] = max(f[i + 1], events[i][2]); int ans = 0; - for (auto& e : events) - { + for (auto& e : events) { int v = e[2]; int left = 0, right = n; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (events[mid][0] > e[1]) right = mid; - else left = mid + 1; + if (events[mid][0] > e[1]) + right = mid; + else + left = mid + 1; } if (left < n) v += f[left]; ans = max(ans, v); diff --git a/solution/2000-2099/2055.Plates Between Candles/Solution.cpp b/solution/2000-2099/2055.Plates Between Candles/Solution.cpp index d199f23d18aa8..2a6bfcea2804a 100644 --- a/solution/2000-2099/2055.Plates Between Candles/Solution.cpp +++ b/solution/2000-2099/2055.Plates Between Candles/Solution.cpp @@ -6,19 +6,16 @@ class Solution { for (int i = 0; i < n; ++i) presum[i + 1] = presum[i] + (s[i] == '*'); vector left(n); vector right(n); - for (int i = 0, l = -1; i < n; ++i) - { + for (int i = 0, l = -1; i < n; ++i) { if (s[i] == '|') l = i; left[i] = l; } - for (int i = n - 1, r = -1; i >= 0; --i) - { + for (int i = n - 1, r = -1; i >= 0; --i) { if (s[i] == '|') r = i; right[i] = r; } vector ans(queries.size()); - for (int k = 0; k < queries.size(); ++k) - { + for (int k = 0; k < queries.size(); ++k) { int i = right[queries[k][0]]; int j = left[queries[k][1]]; if (i >= 0 && j >= 0 && i < j) ans[k] = presum[j] - presum[i + 1]; diff --git a/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/Solution.cpp b/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/Solution.cpp index d9e296852d1f6..382138ff846be 100644 --- a/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/Solution.cpp +++ b/solution/2000-2099/2058.Find the Minimum and Maximum Number of Nodes Between Critical Points/Solution.cpp @@ -16,13 +16,11 @@ class Solution { int first = 0, last = 0; int i = 1; vector ans(2, INT_MAX); - while (curr->next) - { - if (curr->val < min(prev->val, curr->next->val) || curr->val > max(prev->val, curr->next->val)) - { - if (last == 0) first = i; - else - { + while (curr->next) { + if (curr->val < min(prev->val, curr->next->val) || curr->val > max(prev->val, curr->next->val)) { + if (last == 0) + first = i; + else { ans[0] = min(ans[0], i - last); ans[1] = i - first; } diff --git a/solution/2000-2099/2059.Minimum Operations to Convert Number/Solution.cpp b/solution/2000-2099/2059.Minimum Operations to Convert Number/Solution.cpp index 21c9d09ea2595..d5be7fe26605a 100644 --- a/solution/2000-2099/2059.Minimum Operations to Convert Number/Solution.cpp +++ b/solution/2000-2099/2059.Minimum Operations to Convert Number/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: int minimumOperations(vector& nums, int start, int goal) { using pii = pair; - vector> ops{ + vector> ops { [](int x, int y) { return x + y; }, [](int x, int y) { return x - y; }, [](int x, int y) { return x ^ y; }, diff --git a/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.cpp b/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.cpp index 21868eb7061ce..307b109c41d8f 100644 --- a/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.cpp +++ b/solution/2000-2099/2062.Count Vowel Substrings of a String/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int countVowelSubstrings(string word) { int ans = 0; int n = word.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { unordered_set t; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { char c = word[j]; if (!isVowel(c)) break; t.insert(c); diff --git a/solution/2000-2099/2063.Vowels of All Substrings/Solution.cpp b/solution/2000-2099/2063.Vowels of All Substrings/Solution.cpp index 4686301c6452b..e0df1464f8d9c 100644 --- a/solution/2000-2099/2063.Vowels of All Substrings/Solution.cpp +++ b/solution/2000-2099/2063.Vowels of All Substrings/Solution.cpp @@ -2,10 +2,9 @@ class Solution { public: long long countVowels(string word) { long long ans = 0; - for (int i = 0, n = word.size(); i < n; ++i) - { + for (int i = 0, n = word.size(); i < n; ++i) { char c = word[i]; - if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long) (i + 1) * (n - i); + if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') ans += (long long)(i + 1) * (n - i); } return ans; } diff --git a/solution/2000-2099/2064.Minimized Maximum of Products Distributed to Any Store/Solution.cpp b/solution/2000-2099/2064.Minimized Maximum of Products Distributed to Any Store/Solution.cpp index 1a8c78a327490..f394257452752 100644 --- a/solution/2000-2099/2064.Minimized Maximum of Products Distributed to Any Store/Solution.cpp +++ b/solution/2000-2099/2064.Minimized Maximum of Products Distributed to Any Store/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: int minimizedMaximum(int n, vector& quantities) { int left = 1, right = 1e5; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; int s = 0; for (int& q : quantities) s += (q + mid - 1) / mid; - if (s <= n) right = mid; - else left = mid + 1; + if (s <= n) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp index 1c8d1762ba10b..5154e7749aece 100644 --- a/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp +++ b/solution/2000-2099/2067.Number of Equal Count Substrings/Solution.cpp @@ -5,14 +5,12 @@ class Solution { if (count > n) return 0; vector> counter(n + 1, vector(26)); int ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int idx = s[i] - 'a'; for (int j = 0; j < 26; ++j) counter[i + 1][j] = counter[i][j]; counter[i + 1][idx] = counter[i][idx] + 1; int l = 0; - for (int k = 0; k < 26; ++k) - { + for (int k = 0; k < 26; ++k) { l += count; int j = i - l + 1; if (j < 0) break; @@ -25,8 +23,7 @@ class Solution { bool check(int i, int j, int count, vector>& counter) { auto& c1 = counter[i]; auto& c2 = counter[j + 1]; - for (int k = 0; k < 26; ++k) - { + for (int k = 0; k < 26; ++k) { if (c2[k] == 0 || c1[k] == c2[k]) continue; if (c2[k] - c1[k] != count) return false; } diff --git a/solution/2000-2099/2070.Most Beautiful Item for Each Query/Solution.cpp b/solution/2000-2099/2070.Most Beautiful Item for Each Query/Solution.cpp index 090dcf3b211ae..3441ebec8f574 100644 --- a/solution/2000-2099/2070.Most Beautiful Item for Each Query/Solution.cpp +++ b/solution/2000-2099/2070.Most Beautiful Item for Each Query/Solution.cpp @@ -5,14 +5,14 @@ class Solution { for (int i = 1; i < items.size(); ++i) items[i][1] = max(items[i - 1][1], items[i][1]); int n = queries.size(); vector ans(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int left = 0, right = items.size(); - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (items[mid][0] > queries[i]) right = mid; - else left = mid + 1; + if (items[mid][0] > queries[i]) + right = mid; + else + left = mid + 1; } if (left) ans[i] = items[left - 1][1]; } diff --git a/solution/2000-2099/2076.Process Restricted Friend Requests/Solution.cpp b/solution/2000-2099/2076.Process Restricted Friend Requests/Solution.cpp index 027d8474abb7c..6d4cff3edbe5c 100644 --- a/solution/2000-2099/2076.Process Restricted Friend Requests/Solution.cpp +++ b/solution/2000-2099/2076.Process Restricted Friend Requests/Solution.cpp @@ -6,18 +6,15 @@ class Solution { p.resize(n); for (int i = 0; i < n; ++i) p[i] = i; vector ans; - for (auto& req : requests) - { + for (auto& req : requests) { int u = req[0], v = req[1]; - if (find(u) == find(v)) ans.push_back(true); - else - { + if (find(u) == find(v)) + ans.push_back(true); + else { bool valid = true; - for (auto& res : restrictions) - { + for (auto& res : restrictions) { int x = res[0], y = res[1]; - if ((find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x))) - { + if ((find(u) == find(x) && find(v) == find(y)) || (find(u) == find(y) && find(v) == find(x))) { valid = false; break; } diff --git a/solution/2000-2099/2078.Two Furthest Houses With Different Colors/Solution.cpp b/solution/2000-2099/2078.Two Furthest Houses With Different Colors/Solution.cpp index 346aa170be98d..ea7332169f602 100644 --- a/solution/2000-2099/2078.Two Furthest Houses With Different Colors/Solution.cpp +++ b/solution/2000-2099/2078.Two Furthest Houses With Different Colors/Solution.cpp @@ -4,8 +4,10 @@ class Solution { int n = colors.size(); if (colors[0] != colors[n - 1]) return n - 1; int i = 0, j = n; - while (colors[++i] == colors[0]); - while (colors[--j] == colors[0]); + while (colors[++i] == colors[0]) + ; + while (colors[--j] == colors[0]) + ; return max(n - i - 1, j); } }; \ No newline at end of file diff --git a/solution/2000-2099/2079.Watering Plants/Solution.cpp b/solution/2000-2099/2079.Watering Plants/Solution.cpp index b71cffa589d3c..4fc7c114d3bd2 100644 --- a/solution/2000-2099/2079.Watering Plants/Solution.cpp +++ b/solution/2000-2099/2079.Watering Plants/Solution.cpp @@ -2,15 +2,11 @@ class Solution { public: int wateringPlants(vector& plants, int capacity) { int ans = 0, cap = capacity; - for (int i = 0; i < plants.size(); ++i) - { - if (cap >= plants[i]) - { + for (int i = 0; i < plants.size(); ++i) { + if (cap >= plants[i]) { cap -= plants[i]; ++ans; - } - else - { + } else { cap = capacity - plants[i]; ans += i * 2 + 1; } diff --git a/solution/2000-2099/2080.Range Frequency Queries/Solution.cpp b/solution/2000-2099/2080.Range Frequency Queries/Solution.cpp index f9938c180ed06..6b634f5d448d6 100644 --- a/solution/2000-2099/2080.Range Frequency Queries/Solution.cpp +++ b/solution/2000-2099/2080.Range Frequency Queries/Solution.cpp @@ -5,7 +5,7 @@ class RangeFreqQuery { for (int i = 0; i < arr.size(); ++i) mp[arr[i]].push_back(i); } - + int query(int left, int right, int value) { if (!mp.count(value)) return 0; auto& arr = mp[value]; diff --git a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp index 319357968be61..1f10961f4a5d4 100644 --- a/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp +++ b/solution/2000-2099/2083.Substrings That Begin and End With the Same Letter/Solution.cpp @@ -3,8 +3,7 @@ class Solution { long long numberOfSubstrings(string s) { vector counter(26); long long ans = 0; - for (char c : s) - { + for (char c : s) { int i = c - 'a'; ++counter[i]; ans += counter[i]; diff --git a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.cpp b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.cpp index a3e80fe0ae478..bbb412be56c7d 100644 --- a/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.cpp +++ b/solution/2000-2099/2091.Removing Minimum and Maximum From Array/Solution.cpp @@ -2,13 +2,11 @@ class Solution { public: int minimumDeletions(vector& nums) { int mi = 0, mx = 0, n = nums.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (nums[i] < nums[mi]) mi = i; if (nums[i] > nums[mx]) mx = i; } - if (mi > mx) - { + if (mi > mx) { int t = mi; mi = mx; mx = t; diff --git a/solution/2000-2099/2092.Find All People With Secret/Solution.cpp b/solution/2000-2099/2092.Find All People With Secret/Solution.cpp index 544a1a35372f7..562884498f622 100644 --- a/solution/2000-2099/2092.Find All People With Secret/Solution.cpp +++ b/solution/2000-2099/2092.Find All People With Secret/Solution.cpp @@ -6,14 +6,13 @@ class Solution { sort(meetings.begin(), meetings.end(), [&](const auto& x, const auto& y) { return x[2] < y[2]; }); - for (int i = 0, m = meetings.size(); i < m;) - { + for (int i = 0, m = meetings.size(); i < m;) { int j = i; - for (; j + 1 < m && meetings[j + 1][2] == meetings[i][2]; ++j); + for (; j + 1 < m && meetings[j + 1][2] == meetings[i][2]; ++j) + ; unordered_map> g; unordered_set s; - for (int k = i; k <= j; ++k) - { + for (int k = i; k <= j; ++k) { int x = meetings[k][0], y = meetings[k][1]; g[x].push_back(y); g[y].push_back(x); @@ -24,14 +23,11 @@ class Solution { for (int u : s) if (vis[u]) q.push(u); - while (!q.empty()) - { + while (!q.empty()) { int u = q.front(); q.pop(); - for (int v : g[u]) - { - if (!vis[v]) - { + for (int v : g[u]) { + if (!vis[v]) { vis[v] = true; q.push(v); } diff --git a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp index 3e50eddc9406a..eb62909788538 100644 --- a/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp +++ b/solution/2000-2099/2094.Finding 3-Digit Even Numbers/Solution.cpp @@ -3,11 +3,9 @@ class Solution { vector findEvenNumbers(vector& digits) { vector counter = count(digits); vector ans; - for (int i = 100; i < 1000; i += 2) - { + for (int i = 100; i < 1000; i += 2) { vector t(3); - for (int j = 0, k = i; k > 0; ++j) - { + for (int j = 0, k = i; k > 0; ++j) { t[j] = k % 10; k /= 10; } diff --git a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.cpp b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.cpp index 0095dee88c55e..2b5aafb7d0518 100644 --- a/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.cpp +++ b/solution/2000-2099/2095.Delete the Middle Node of a Linked List/Solution.cpp @@ -14,8 +14,7 @@ class Solution { ListNode* dummy = new ListNode(0, head); ListNode* slow = dummy; ListNode* fast = head; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } diff --git a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.cpp b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.cpp index 88691b6fb27e4..8b85567ffb3c9 100644 --- a/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.cpp +++ b/solution/2000-2099/2096.Step-By-Step Directions From a Binary Tree Node to Another/Solution.cpp @@ -25,13 +25,11 @@ class Solution { void traverse(TreeNode* root) { if (!root) return; - if (root->left) - { + if (root->left) { edges[root->val].push_back({root->left->val, 'L'}); edges[root->left->val].push_back({root->val, 'U'}); } - if (root->right) - { + if (root->right) { edges[root->val].push_back({root->right->val, 'R'}); edges[root->right->val].push_back({root->val, 'U'}); } @@ -41,16 +39,13 @@ class Solution { void dfs(int start, int dest, string& t) { if (visited.count(start)) return; - if (start == dest) - { + if (start == dest) { if (ans == "" || ans.size() > t.size()) ans = t; return; } visited.insert(start); - if (edges.count(start)) - { - for (auto& item : edges[start]) - { + if (edges.count(start)) { + for (auto& item : edges[start]) { t += item.second; dfs(item.first, dest, t); t.pop_back(); diff --git a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp index fe1643e3610e5..65cad805bde67 100644 --- a/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp +++ b/solution/2100-2199/2101.Detonate the Maximum Bombs/Solution.cpp @@ -7,21 +7,17 @@ class Solution { for (int j = 0; j < n; ++j) g[i][j] = check(i, j, bombs); int ans = 0; - for (int k = 0; k < n; ++k) - { - queue q{{k}}; + for (int k = 0; k < n; ++k) { + queue q {{k}}; vector vis(n); vis[k] = true; int cnt = 0; - while (!q.empty()) - { + while (!q.empty()) { int i = q.front(); q.pop(); ++cnt; - for (int j = 0; j < n; ++j) - { - if (g[i][j] && !vis[j]) - { + for (int j = 0; j < n; ++j) { + if (g[i][j] && !vis[j]) { vis[j] = true; q.push(j); } diff --git a/solution/2100-2199/2103.Rings and Rods/Solution.cpp b/solution/2100-2199/2103.Rings and Rods/Solution.cpp index 5f99aca2df4d1..56e01d2b9512c 100644 --- a/solution/2100-2199/2103.Rings and Rods/Solution.cpp +++ b/solution/2100-2199/2103.Rings and Rods/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int countPoints(string rings) { unordered_map> mp; - for (int i = 1; i < rings.size(); i += 2) - { + for (int i = 1; i < rings.size(); i += 2) { int c = rings[i] - '0'; mp[c].insert(rings[i - 1]); } diff --git a/solution/2100-2199/2104.Sum of Subarray Ranges/Solution.cpp b/solution/2100-2199/2104.Sum of Subarray Ranges/Solution.cpp index aaeee5c1bb7bf..53c89f1621efd 100644 --- a/solution/2100-2199/2104.Sum of Subarray Ranges/Solution.cpp +++ b/solution/2100-2199/2104.Sum of Subarray Ranges/Solution.cpp @@ -12,23 +12,20 @@ class Solution { int n = nums.size(); vector left(n, -1); vector right(n, n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { while (!stk.empty() && nums[stk.top()] <= nums[i]) stk.pop(); if (!stk.empty()) left[i] = stk.top(); stk.push(i); } stk = stack(); - for (int i = n - 1; i >= 0; --i) - { + for (int i = n - 1; i >= 0; --i) { while (!stk.empty() && nums[stk.top()] < nums[i]) stk.pop(); if (!stk.empty()) right[i] = stk.top(); stk.push(i); } long long ans = 0; - for (int i = 0; i < n; ++i) - { - ans += (long long) (i - left[i]) * (right[i] - i) * nums[i]; + for (int i = 0; i < n; ++i) { + ans += (long long)(i - left[i]) * (right[i] - i) * nums[i]; } return ans; } diff --git a/solution/2100-2199/2105.Watering Plants II/Solution.cpp b/solution/2100-2199/2105.Watering Plants II/Solution.cpp index 7ceaac8595ffe..f5b6e8daa9d6f 100644 --- a/solution/2100-2199/2105.Watering Plants II/Solution.cpp +++ b/solution/2100-2199/2105.Watering Plants II/Solution.cpp @@ -3,26 +3,22 @@ class Solution { int minimumRefill(vector& plants, int capacityA, int capacityB) { int i = 0, j = plants.size() - 1; int ans = 0, a = capacityA, b = capacityB; - while (i <= j) - { - if (i == j) - { + while (i <= j) { + if (i == j) { if (max(capacityA, capacityB) < plants[i]) ++ans; break; } - if (capacityA < plants[i]) - { + if (capacityA < plants[i]) { capacityA = a - plants[i]; ++ans; - } - else capacityA -= plants[i]; + } else + capacityA -= plants[i]; - if (capacityB < plants[j]) - { + if (capacityB < plants[j]) { capacityB = b - plants[j]; ++ans; - } - else capacityB -= plants[j]; + } else + capacityB -= plants[j]; ++i; --j; } diff --git a/solution/2100-2199/2106.Maximum Fruits Harvested After at Most K Steps/Solution.cpp b/solution/2100-2199/2106.Maximum Fruits Harvested After at Most K Steps/Solution.cpp index fbadf0d85949f..9d92fd5e70d4a 100644 --- a/solution/2100-2199/2106.Maximum Fruits Harvested After at Most K Steps/Solution.cpp +++ b/solution/2100-2199/2106.Maximum Fruits Harvested After at Most K Steps/Solution.cpp @@ -4,20 +4,16 @@ class Solution { queue> q; int i = 0, n = fruits.size(); int ans = 0; - while (i < n && fruits[i][0] <= startPos) - { - if (startPos - fruits[i][0] <= k) - { + while (i < n && fruits[i][0] <= startPos) { + if (startPos - fruits[i][0] <= k) { ans += fruits[i][1]; q.push(fruits[i]); } ++i; } int t = ans; - while (i < n && fruits[i][0] - startPos <= k) - { - while (!q.empty() && q.front()[0] < startPos && fruits[i][0] - q.front()[0] + min(startPos - q.front()[0], fruits[i][0] - startPos) > k) - { + while (i < n && fruits[i][0] - startPos <= k) { + while (!q.empty() && q.front()[0] < startPos && fruits[i][0] - q.front()[0] + min(startPos - q.front()[0], fruits[i][0] - startPos) > k) { t -= q.front()[1]; q.pop(); } diff --git a/solution/2100-2199/2109.Adding Spaces to a String/Solution.cpp b/solution/2100-2199/2109.Adding Spaces to a String/Solution.cpp index ae09179f35033..df5029d9fb343 100644 --- a/solution/2100-2199/2109.Adding Spaces to a String/Solution.cpp +++ b/solution/2100-2199/2109.Adding Spaces to a String/Solution.cpp @@ -2,10 +2,8 @@ class Solution { public: string addSpaces(string s, vector& spaces) { string ans = ""; - for (int i = 0, j = 0; i < s.size(); ++i) - { - if (j < spaces.size() && i == spaces[j]) - { + for (int i = 0, j = 0; i < s.size(); ++i) { + if (j < spaces.size() && i == spaces[j]) { ans += ' '; ++j; } diff --git a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cpp b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cpp index bcb815bc1adb0..3ed176ffa8674 100644 --- a/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cpp +++ b/solution/2100-2199/2110.Number of Smooth Descent Periods of a Stock/Solution.cpp @@ -2,12 +2,12 @@ class Solution { public: long long getDescentPeriods(vector& prices) { long long ans = 0; - for (int i = 0, n = prices.size(); i < n;) - { + for (int i = 0, n = prices.size(); i < n;) { int j = i; - for (; j + 1 < n && prices[j] - prices[j + 1] == 1; ++j); + for (; j + 1 < n && prices[j] - prices[j + 1] == 1; ++j) + ; int t = j - i + 1; - ans += (long long) t * (t + 1) / 2; + ans += (long long)t * (t + 1) / 2; i = j + 1; } return ans; diff --git a/solution/2100-2199/2111.Minimum Operations to Make the Array K-Increasing/Solution.cpp b/solution/2100-2199/2111.Minimum Operations to Make the Array K-Increasing/Solution.cpp index d08b4bb57fe55..42e48384a90c2 100644 --- a/solution/2100-2199/2111.Minimum Operations to Make the Array K-Increasing/Solution.cpp +++ b/solution/2100-2199/2111.Minimum Operations to Make the Array K-Increasing/Solution.cpp @@ -2,9 +2,8 @@ class Solution { public: int kIncreasing(vector& arr, int k) { int ans = 0, n = arr.size(); - for (int i = 0; i < k; ++i) - { - vector t; + for (int i = 0; i < k; ++i) { + vector t; for (int j = i; j < n; j += k) t.push_back(arr[j]); ans += lis(t); } @@ -13,11 +12,12 @@ class Solution { int lis(vector& arr) { vector t; - for (int x : arr) - { + for (int x : arr) { auto it = upper_bound(t.begin(), t.end(), x); - if (it == t.end()) t.push_back(x); - else *it = x; + if (it == t.end()) + t.push_back(x); + else + *it = x; } return arr.size() - t.size(); } diff --git a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.cpp b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.cpp index 228ca4e52e963..f38f73a33e273 100644 --- a/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.cpp +++ b/solution/2100-2199/2120.Execution of All Suffix Instructions Staying in a Grid/Solution.cpp @@ -8,20 +8,17 @@ class Solution { mp['R'] = {0, 1}; mp['U'] = {-1, 0}; mp['D'] = {1, 0}; - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { int x = startPos[0], y = startPos[1]; int t = 0; - for (int j = i; j < m; ++j) - { + for (int j = i; j < m; ++j) { int a = mp[s[j]][0], b = mp[s[j]][1]; - if (0 <= x + a && x + a < n && 0 <= y + b && y + b < n) - { + if (0 <= x + a && x + a < n && 0 <= y + b && y + b < n) { x += a; y += b; ++t; - } - else break; + } else + break; } ans[i] = t; } diff --git a/solution/2100-2199/2121.Intervals Between Identical Elements/Solution.cpp b/solution/2100-2199/2121.Intervals Between Identical Elements/Solution.cpp index dac717b32e9e2..cc364940c47e1 100644 --- a/solution/2100-2199/2121.Intervals Between Identical Elements/Solution.cpp +++ b/solution/2100-2199/2121.Intervals Between Identical Elements/Solution.cpp @@ -5,15 +5,13 @@ class Solution { int n = arr.size(); for (int i = 0; i < n; ++i) d[arr[i]].push_back(i); vector ans(n); - for (auto& item : d) - { + for (auto& item : d) { auto& v = item.second; int m = v.size(); long long val = 0; for (int e : v) val += e; val -= m * v[0]; - for (int i = 0; i < v.size(); ++i) - { + for (int i = 0; i < v.size(); ++i) { int delta = i >= 1 ? v[i] - v[i - 1] : 0; val += i * delta - (m - i) * delta; ans[v[i]] = val; diff --git a/solution/2100-2199/2122.Recover the Original Array/Solution.cpp b/solution/2100-2199/2122.Recover the Original Array/Solution.cpp index ebddbd99b9899..77ddd988badfc 100644 --- a/solution/2100-2199/2122.Recover the Original Array/Solution.cpp +++ b/solution/2100-2199/2122.Recover the Original Array/Solution.cpp @@ -2,16 +2,14 @@ class Solution { public: vector recoverArray(vector& nums) { sort(nums.begin(), nums.end()); - for (int i = 1, n = nums.size(); i < n; ++i) - { + for (int i = 1, n = nums.size(); i < n; ++i) { int d = nums[i] - nums[0]; if (d == 0 || d % 2 == 1) continue; vector vis(n); vis[i] = true; vector ans; ans.push_back((nums[0] + nums[i]) >> 1); - for (int l = 1, r = i + 1; r < n; ++l, ++r) - { + for (int l = 1, r = i + 1; r < n; ++l, ++r) { while (l < n && vis[l]) ++l; while (r < n && nums[r] - nums[l] < d) ++r; if (r == n || nums[r] - nums[l] > d) break; diff --git a/solution/2100-2199/2125.Number of Laser Beams in a Bank/Solution.cpp b/solution/2100-2199/2125.Number of Laser Beams in a Bank/Solution.cpp index 2099eb690a549..82ab4c5c5d54a 100644 --- a/solution/2100-2199/2125.Number of Laser Beams in a Bank/Solution.cpp +++ b/solution/2100-2199/2125.Number of Laser Beams in a Bank/Solution.cpp @@ -3,14 +3,12 @@ class Solution { int numberOfBeams(vector& bank) { int ans = 0; int last = 0; - for (auto& b : bank) - { + for (auto& b : bank) { int t = 0; for (char& c : b) if (c == '1') ++t; - if (t) - { + if (t) { ans += last * t; last = t; } diff --git a/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp b/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp index fb370f375533b..0b0b082a7a3dd 100644 --- a/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp +++ b/solution/2100-2199/2126.Destroying Asteroids/Solution.cpp @@ -3,8 +3,7 @@ class Solution { bool asteroidsDestroyed(int mass, vector& asteroids) { sort(asteroids.begin(), asteroids.end()); long long m = mass; - for (int v : asteroids) - { + for (int v : asteroids) { if (m < v) return false; m += v; } diff --git a/solution/2100-2199/2127.Maximum Employees to Be Invited to a Meeting/Solution.cpp b/solution/2100-2199/2127.Maximum Employees to Be Invited to a Meeting/Solution.cpp index e33c7d5c63e71..02216d502d85b 100644 --- a/solution/2100-2199/2127.Maximum Employees to Be Invited to a Meeting/Solution.cpp +++ b/solution/2100-2199/2127.Maximum Employees to Be Invited to a Meeting/Solution.cpp @@ -8,22 +8,18 @@ class Solution { int n = fa.size(); vector vis(n); int ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (vis[i]) continue; vector cycle; int j = i; - while (!vis[j]) - { + while (!vis[j]) { cycle.push_back(j); vis[j] = true; j = fa[j]; } - for (int k = 0; k < cycle.size(); ++k) - { - if (cycle[k] == j) - { - ans = max(ans, (int) cycle.size() - k); + for (int k = 0; k < cycle.size(); ++k) { + if (cycle[k] == j) { + ans = max(ans, (int)cycle.size() - k); break; } } @@ -37,16 +33,17 @@ class Solution { vector dist(n, 1); for (int v : fa) ++indeg[v]; queue q; - for (int i = 0; i < n; ++i) if (indeg[i] == 0) q.push(i); - while (!q.empty()) - { + for (int i = 0; i < n; ++i) + if (indeg[i] == 0) q.push(i); + while (!q.empty()) { int i = q.front(); q.pop(); dist[fa[i]] = max(dist[fa[i]], dist[i] + 1); if (--indeg[fa[i]] == 0) q.push(fa[i]); } int ans = 0; - for (int i = 0; i < n; ++i) if (i == fa[fa[i]]) ans += dist[i]; + for (int i = 0; i < n; ++i) + if (i == fa[fa[i]]) ans += dist[i]; return ans; } }; \ No newline at end of file diff --git a/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/Solution.cpp b/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/Solution.cpp index 8af9f6e5a253d..080a2d3cb5cd4 100644 --- a/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/Solution.cpp +++ b/solution/2100-2199/2130.Maximum Twin Sum of a Linked List/Solution.cpp @@ -13,8 +13,7 @@ class Solution { int pairSum(ListNode* head) { ListNode* slow = head; ListNode* fast = head->next; - while (fast && fast->next) - { + while (fast && fast->next) { slow = slow->next; fast = fast->next->next; } @@ -23,8 +22,7 @@ class Solution { slow->next = nullptr; ListNode* pb = reverse(q); int ans = 0; - while (pa) - { + while (pa) { ans = max(ans, pa->val + pb->val); pa = pa->next; pb = pb->next; @@ -35,8 +33,7 @@ class Solution { ListNode* reverse(ListNode* head) { ListNode* dummy = new ListNode(); ListNode* curr = head; - while (curr) - { + while (curr) { ListNode* next = curr->next; curr->next = dummy->next; dummy->next = curr; diff --git a/solution/2100-2199/2132.Stamping the Grid/Solution.cpp b/solution/2100-2199/2132.Stamping the Grid/Solution.cpp index 77c7672cd0b33..87b22ad8cc669 100644 --- a/solution/2100-2199/2132.Stamping the Grid/Solution.cpp +++ b/solution/2100-2199/2132.Stamping the Grid/Solution.cpp @@ -3,22 +3,17 @@ class Solution { bool possibleToStamp(vector>& grid, int stampHeight, int stampWidth) { int m = grid.size(), n = grid[0].size(); vector> s(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { s[i + 1][j + 1] = s[i + 1][j] + s[i][j + 1] - s[i][j] + grid[i][j]; } } vector> d(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (grid[i][j]) continue; int x = i + stampHeight, y = j + stampWidth; - if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) - { + if (x <= m && y <= n && s[x][y] - s[i][y] - s[x][j] + s[i][j] == 0) { d[i][j]++; d[x][j]--; d[i][y]--; @@ -27,10 +22,8 @@ class Solution { } } vector> cnt(m + 1, vector(n + 1)); - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { cnt[i + 1][j + 1] = cnt[i + 1][j] + cnt[i][j + 1] - cnt[i][j] + d[i][j]; if (grid[i][j] == 0 && cnt[i + 1][j + 1] == 0) return false; } diff --git a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp index b257ca060ebe0..d8ff21fdd4663 100644 --- a/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp +++ b/solution/2100-2199/2133.Check if Every Row and Column Contains All Numbers/Solution.cpp @@ -2,21 +2,17 @@ class Solution { public: bool checkValid(vector>& matrix) { int n = matrix.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { vector seen(n); - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { int v = matrix[i][j] - 1; if (seen[v]) return false; seen[v] = true; } } - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { vector seen(n); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = matrix[i][j] - 1; if (seen[v]) return false; seen[v] = true; diff --git a/solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/Solution.cpp b/solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/Solution.cpp index 7065a8a488856..f0ea2dd571c24 100644 --- a/solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/Solution.cpp +++ b/solution/2100-2199/2134.Minimum Swaps to Group All 1's Together II/Solution.cpp @@ -7,8 +7,7 @@ class Solution { vector s((n << 1) + 1); for (int i = 0; i < (n << 1); ++i) s[i + 1] = s[i] + nums[i % n]; int mx = 0; - for (int i = 0; i < (n << 1); ++i) - { + for (int i = 0; i < (n << 1); ++i) { int j = i + cnt - 1; if (j < (n << 1)) mx = max(mx, s[j + 1] - s[i]); } diff --git a/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/Solution.cpp b/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/Solution.cpp index 6326da0bc0340..309f39c0308b1 100644 --- a/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/Solution.cpp +++ b/solution/2100-2199/2135.Count Words Obtained After Adding a Letter/Solution.cpp @@ -2,24 +2,20 @@ class Solution { public: int wordCount(vector& startWords, vector& targetWords) { unordered_set s; - for (auto& word : startWords) - { + for (auto& word : startWords) { int mask = 0; for (char c : word) mask |= (1 << (c - 'a')); s.insert(mask); } int ans = 0; - for (auto& word : targetWords) - { + for (auto& word : targetWords) { int mask = 0; for (char c : word) mask |= (1 << (c - 'a')); - for (char c : word) - { + for (char c : word) { int t = mask ^ (1 << (c - 'a')); - if (s.count(t)) - { + if (s.count(t)) { ++ans; break; } diff --git a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp index 08defc1ba1e4d..210b009377625 100644 --- a/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp +++ b/solution/2100-2199/2136.Earliest Possible Day of Full Bloom/Solution.cpp @@ -6,8 +6,7 @@ class Solution { for (int i = 0; i < n; ++i) arr.push_back({-growTime[i], plantTime[i]}); sort(arr.begin(), arr.end()); int ans = 0, t = 0; - for (auto [a, b] : arr) - { + for (auto [a, b] : arr) { t += b; ans = max(ans, t - a); } diff --git a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp index 67740717fb678..6d342d85e61c9 100644 --- a/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp +++ b/solution/2100-2199/2138.Divide a String Into Groups of Size k/Solution.cpp @@ -2,7 +2,8 @@ class Solution { public: vector divideString(string s, int k, char fill) { int n = s.size(); - if (n % k) for (int i = 0; i < k - n % k; ++i) s.push_back(fill); + if (n % k) + for (int i = 0; i < k - n % k; ++i) s.push_back(fill); vector ans; for (int i = 0; i < s.size() / k; ++i) ans.push_back(s.substr(i * k, k)); return ans; diff --git a/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp index 04e57038d2195..d6ea016debb4b 100644 --- a/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp +++ b/solution/2100-2199/2140.Solving Questions With Brainpower/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: long long mostPoints(vector>& questions) { vector memo(questions.size(), -1); - return dfs(0, questions, memo); + return dfs(0, questions, memo); } long long dfs(int i, vector>& questions, vector& memo) { diff --git a/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/Solution.cpp b/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/Solution.cpp index e463543082bc5..42d83a515c0ef 100644 --- a/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/Solution.cpp +++ b/solution/2100-2199/2144.Minimum Cost of Buying Candies With Discount/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minimumCost(vector& cost) { sort(cost.begin(), cost.end()); int ans = 0, n = cost.size(); - for (int i = n - 1; i >= 0; i -= 3) - { + for (int i = n - 1; i >= 0; i -= 3) { ans += cost[i]; if (i >= 1) ans += cost[i - 1]; } diff --git a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp index f3a97316862f5..526f4acb8d79e 100644 --- a/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp +++ b/solution/2100-2199/2145.Count the Hidden Sequences/Solution.cpp @@ -2,12 +2,11 @@ class Solution { public: int numberOfArrays(vector& differences, int lower, int upper) { long long num = 0, mi = 0, mx = 0; - for (int& d : differences) - { + for (int& d : differences) { num += d; mi = min(mi, num); mx = max(mx, num); } - return max(0, (int) (upper - lower - (mx - mi) + 1)); + return max(0, (int)(upper - lower - (mx - mi) + 1)); } }; \ No newline at end of file diff --git a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp index 447488d9b6b7d..7713f5b430823 100644 --- a/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp +++ b/solution/2100-2199/2146.K Highest Ranked Items Within a Price Range/Solution.cpp @@ -11,15 +11,12 @@ class Solution { q.emplace(row, col, 0); grid[row][col] = 0; vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto [i, j, d] = q.front(); q.pop(); - for (int l = 0; l < 4; ++l) - { + for (int l = 0; l < 4; ++l) { int x = i + dirs[l], y = j + dirs[l + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) - { + if (x >= 0 && x < m && y >= 0 && y < n && grid[x][y]) { if (low <= grid[x][y] && grid[x][y] <= high) items.emplace_back(d + 1, grid[x][y], x, y); grid[x][y] = 0; q.emplace(x, y, d + 1); @@ -28,8 +25,7 @@ class Solution { } sort(items.begin(), items.end()); vector> ans; - for (int i = 0; i < items.size() && i < k; ++i) - { + for (int i = 0; i < items.size() && i < k; ++i) { auto [d, p, x, y] = items[i]; ans.push_back({x, y}); } diff --git a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp index 5d62e45c88d20..cab461850652b 100644 --- a/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp +++ b/solution/2100-2199/2148.Count Elements With Strictly Smaller and Greater Elements/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int countElements(vector& nums) { int mi = 1e6, mx = -1e6; - for (int num : nums) - { + for (int num : nums) { mi = min(mi, num); mx = max(mx, num); } diff --git a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp index 31b2544655252..3cf5a6c6805d4 100644 --- a/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp +++ b/solution/2100-2199/2149.Rearrange Array Elements by Sign/Solution.cpp @@ -3,15 +3,11 @@ class Solution { vector rearrangeArray(vector& nums) { vector ans(nums.size()); int i = 0, j = 1; - for (int num : nums) - { - if (num > 0) - { + for (int num : nums) { + if (num > 0) { ans[i] = num; i += 2; - } - else - { + } else { ans[j] = num; j += 2; } diff --git a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp index b6fab78a9c236..4cdb56deec98d 100644 --- a/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp +++ b/solution/2100-2199/2150.Find All Lonely Numbers in the Array/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map counter; for (int num : nums) ++counter[num]; vector ans; - for (auto& e : counter) - { + for (auto& e : counter) { int k = e.first, v = e.second; if (v == 1 && !counter.count(k - 1) && !counter.count(k + 1)) ans.push_back(k); } diff --git a/solution/2100-2199/2151.Maximum Good People Based on Statements/Solution.cpp b/solution/2100-2199/2151.Maximum Good People Based on Statements/Solution.cpp index 15521f99a3037..684d58b4baeb7 100644 --- a/solution/2100-2199/2151.Maximum Good People Based on Statements/Solution.cpp +++ b/solution/2100-2199/2151.Maximum Good People Based on Statements/Solution.cpp @@ -9,12 +9,9 @@ class Solution { int check(int mask, vector>& statements) { int cnt = 0; int n = statements.size(); - for (int i = 0; i < n; ++i) - { - if ((mask >> i) & 1) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < n; ++i) { + if ((mask >> i) & 1) { + for (int j = 0; j < n; ++j) { int v = statements[i][j]; if (v < 2 && ((mask >> j) & 1) != v) return 0; } diff --git a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp index 57eae675842f8..e2c840cb86711 100644 --- a/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp +++ b/solution/2100-2199/2155.All Divisions With the Highest Score of a Binary Array/Solution.cpp @@ -5,14 +5,15 @@ class Solution { int mx = right; vector ans; ans.push_back(0); - for (int i = 0; i < nums.size(); ++i) - { - if (nums[i] == 0) ++left; - else --right; + for (int i = 0; i < nums.size(); ++i) { + if (nums[i] == 0) + ++left; + else + --right; int t = left + right; - if (mx == t) ans.push_back(i + 1); - else if (mx < t) - { + if (mx == t) + ans.push_back(i + 1); + else if (mx < t) { mx = t; ans.clear(); ans.push_back(i + 1); diff --git a/solution/2100-2199/2157.Groups of Strings/Solution.cpp b/solution/2100-2199/2157.Groups of Strings/Solution.cpp index accf41b465844..961504f840e70 100644 --- a/solution/2100-2199/2157.Groups of Strings/Solution.cpp +++ b/solution/2100-2199/2157.Groups of Strings/Solution.cpp @@ -7,8 +7,7 @@ class Solution { unordered_map size; mx = 0; n = words.size(); - for (auto& word : words) - { + for (auto& word : words) { int x = 0; for (auto& c : word) x |= 1 << (c - 'a'); p[x] = x; @@ -16,15 +15,11 @@ class Solution { mx = max(mx, size[x]); if (size[x] > 1) --n; } - for (auto& [x, _] : p) - { - for (int i = 0; i < 26; ++i) - { + for (auto& [x, _] : p) { + for (int i = 0; i < 26; ++i) { unite(x, x ^ (1 << i), p, size); - if ((x >> i) & 1) - { - for (int j = 0; j < 26; ++j) - { + if ((x >> i) & 1) { + for (int j = 0; j < 26; ++j) { if (((x >> j) & 1) == 0) unite(x, x ^ (1 << i) | (1 << j), p, size); } } diff --git a/solution/2100-2199/2158.Amount of New Area Painted Each Day/Solution.cpp b/solution/2100-2199/2158.Amount of New Area Painted Each Day/Solution.cpp index 58d02608e190a..b6a4c44c3c744 100644 --- a/solution/2100-2199/2158.Amount of New Area Painted Each Day/Solution.cpp +++ b/solution/2100-2199/2158.Amount of New Area Painted Each Day/Solution.cpp @@ -30,10 +30,9 @@ class SegmentTree { modify(l, r, v, root); } - void modify(int l, int r,int v, Node* node) { + void modify(int l, int r, int v, Node* node) { if (l > r) return; - if (node->l >= l && node->r <= r) - { + if (node->l >= l && node->r <= r) { node->v = node->r - node->l + 1; node->add = v; return; @@ -45,17 +44,17 @@ class SegmentTree { } int query(int l, int r) { - return query(l, r, root); + return query(l, r, root); } int query(int l, int r, Node* node) { if (l > r) return 0; - if (node->l >= l && node-> r <= r) return node->v; + if (node->l >= l && node->r <= r) return node->v; pushdown(node); int v = 0; if (l <= node->mid) v += query(l, r, node->left); if (r > node->mid) v += query(l, r, node->right); - return v; + return v; } void pushup(Node* node) { @@ -65,8 +64,7 @@ class SegmentTree { void pushdown(Node* node) { if (!node->left) node->left = new Node(node->l, node->mid); if (!node->right) node->right = new Node(node->mid + 1, node->r); - if (node->add) - { + if (node->add) { Node* left = node->left; Node* right = node->right; left->v = left->r - left->l + 1; @@ -84,8 +82,7 @@ class Solution { int n = paint.size(); vector ans(n); SegmentTree* tree = new SegmentTree(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int l = paint[i][0] + 1; int r = paint[i][1]; int v = tree->query(l, r); diff --git a/solution/2100-2199/2160.Minimum Sum of Four Digit Number After Splitting Digits/Solution.cpp b/solution/2100-2199/2160.Minimum Sum of Four Digit Number After Splitting Digits/Solution.cpp index 8fbd238a140f1..9ec73648de903 100644 --- a/solution/2100-2199/2160.Minimum Sum of Four Digit Number After Splitting Digits/Solution.cpp +++ b/solution/2100-2199/2160.Minimum Sum of Four Digit Number After Splitting Digits/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int minimumSum(int num) { vector nums; - while (num) - { + while (num) { nums.push_back(num % 10); num /= 10; } diff --git a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp index d34bd9369f0f8..227e94ca4f10d 100644 --- a/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp +++ b/solution/2100-2199/2161.Partition Array According to Given Pivot/Solution.cpp @@ -2,9 +2,12 @@ class Solution { public: vector pivotArray(vector& nums, int pivot) { vector ans; - for (int& x : nums) if (x < pivot) ans.push_back(x); - for (int& x : nums) if (x == pivot) ans.push_back(x); - for (int& x : nums) if (x > pivot) ans.push_back(x); + for (int& x : nums) + if (x < pivot) ans.push_back(x); + for (int& x : nums) + if (x == pivot) ans.push_back(x); + for (int& x : nums) + if (x > pivot) ans.push_back(x); return ans; } }; \ No newline at end of file diff --git a/solution/2100-2199/2162.Minimum Cost to Set Cooking Time/Solution.cpp b/solution/2100-2199/2162.Minimum Cost to Set Cooking Time/Solution.cpp index b011c276494c2..df58770b2644d 100644 --- a/solution/2100-2199/2162.Minimum Cost to Set Cooking Time/Solution.cpp +++ b/solution/2100-2199/2162.Minimum Cost to Set Cooking Time/Solution.cpp @@ -9,10 +9,10 @@ class Solution { if (m < 0 || m > 99 || s < 0 || s > 99) return INT_MAX; vector arr = {m / 10, m % 10, s / 10, s % 10}; int i = 0; - for (; i < 4 && arr[i] == 0; ++i); + for (; i < 4 && arr[i] == 0; ++i) + ; int t = 0; - for (; i < 4; ++i) - { + for (; i < 4; ++i) { if (arr[i] != prev) t += moveCost; t += pushCost; prev = arr[i]; diff --git a/solution/2100-2199/2164.Sort Even and Odd Indices Independently/Solution.cpp b/solution/2100-2199/2164.Sort Even and Odd Indices Independently/Solution.cpp index 34d2c11eeaafd..c276646638186 100644 --- a/solution/2100-2199/2164.Sort Even and Odd Indices Independently/Solution.cpp +++ b/solution/2100-2199/2164.Sort Even and Odd Indices Independently/Solution.cpp @@ -4,10 +4,11 @@ class Solution { int n = nums.size(); vector a; vector b; - for (int i = 0; i < n; ++i) - { - if (i % 2 == 0) a.push_back(nums[i]); - else b.push_back(nums[i]); + for (int i = 0; i < n; ++i) { + if (i % 2 == 0) + a.push_back(nums[i]); + else + b.push_back(nums[i]); } sort(a.begin(), a.end()); sort(b.begin(), b.end(), greater()); diff --git a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp index 4052a14bbfad8..6915407b07e1a 100644 --- a/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp +++ b/solution/2100-2199/2165.Smallest Value of the Rearranged Number/Solution.cpp @@ -5,24 +5,19 @@ class Solution { vector cnt(10); bool neg = num < 0; num = abs(num); - while (num) - { + while (num) { cnt[num % 10]++; num /= 10; } long long ans = 0; - if (neg) - { + if (neg) { for (int i = 9; i >= 0; --i) while (cnt[i]--) ans = ans * 10 + i; return -ans; } - if (cnt[0]) - { - for (int i = 1; i < 10; ++i) - { - if (cnt[i]) - { + if (cnt[0]) { + for (int i = 1; i < 10; ++i) { + if (cnt[i]) { ans = ans * 10 + i; cnt[i]--; break; diff --git a/solution/2100-2199/2166.Design Bitset/Solution.cpp b/solution/2100-2199/2166.Design Bitset/Solution.cpp index 4312d0466a418..f0c6b16c2c886 100644 --- a/solution/2100-2199/2166.Design Bitset/Solution.cpp +++ b/solution/2100-2199/2166.Design Bitset/Solution.cpp @@ -7,34 +7,34 @@ class Bitset { a = string(size, '0'); b = string(size, '1'); } - + void fix(int idx) { if (a[idx] == '0') a[idx] = '1', ++cnt; b[idx] = '0'; } - + void unfix(int idx) { if (a[idx] == '1') a[idx] = '0', --cnt; b[idx] = '1'; } - + void flip() { swap(a, b); cnt = a.size() - cnt; } - + bool all() { return cnt == a.size(); } - + bool one() { return cnt > 0; } - + int count() { return cnt; } - + string toString() { return a; } diff --git a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp index 97eead11082fa..72f3792646707 100644 --- a/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp +++ b/solution/2100-2199/2169.Count Operations to Obtain Zero/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int countOperations(int num1, int num2) { int ans = 0; - while (num1 && num2) - { + while (num1 && num2) { if (num1 > num2) swap(num1, num2); num2 -= num1; ++ans; diff --git a/solution/2100-2199/2170.Minimum Operations to Make the Array Alternating/Solution.cpp b/solution/2100-2199/2170.Minimum Operations to Make the Array Alternating/Solution.cpp index df64b80567c24..3cc8284ca355d 100644 --- a/solution/2100-2199/2170.Minimum Operations to Make the Array Alternating/Solution.cpp +++ b/solution/2100-2199/2170.Minimum Operations to Make the Array Alternating/Solution.cpp @@ -16,17 +16,13 @@ class Solution { unordered_map freq; for (; i < nums.size(); i += 2) ++freq[nums[i]]; int a = 0, n1 = 0, b = 0, n2 = 0; - for (auto& [k, v] : freq) - { - if (v > n1) - { + for (auto& [k, v] : freq) { + if (v > n1) { b = a; n2 = n1; a = k; n1 = v; - } - else if (v > n2) - { + } else if (v > n2) { b = k; n2 = v; } diff --git a/solution/2100-2199/2174.Remove All Ones With Row and Column Flips II/Solution.cpp b/solution/2100-2199/2174.Remove All Ones With Row and Column Flips II/Solution.cpp index 8aa2da8eb6ae0..bc698c0c6e00c 100644 --- a/solution/2100-2199/2174.Remove All Ones With Row and Column Flips II/Solution.cpp +++ b/solution/2100-2199/2174.Remove All Ones With Row and Column Flips II/Solution.cpp @@ -7,26 +7,21 @@ class Solution { for (int j = 0; j < n; ++j) if (grid[i][j]) state |= (1 << (i * n + j)); - queue q{{state}}; - unordered_set vis{{state}}; + queue q {{state}}; + unordered_set vis {{state}}; int ans = 0; - while (!q.empty()) - { - for (int k = q.size(); k > 0; --k) - { + while (!q.empty()) { + for (int k = q.size(); k > 0; --k) { state = q.front(); q.pop(); if (state == 0) return ans; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { if (grid[i][j] == 0) continue; int nxt = state; for (int r = 0; r < m; ++r) nxt &= ~(1 << (r * n + j)); for (int c = 0; c < n; ++c) nxt &= ~(1 << (i * n + c)); - if (!vis.count(nxt)) - { + if (!vis.count(nxt)) { vis.insert(nxt); q.push(nxt); } diff --git a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp index 2ae6292b68351..d7272b656926f 100644 --- a/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp +++ b/solution/2100-2199/2176.Count Equal and Divisible Pairs in an Array/Solution.cpp @@ -3,10 +3,8 @@ class Solution { int countPairs(vector& nums, int k) { int n = nums.size(); int ans = 0; - for (int i = 0; i < n; ++i) - { - for (int j = i + 1; j < n; ++j) - { + for (int i = 0; i < n; ++i) { + for (int j = i + 1; j < n; ++j) { if (nums[i] == nums[j] && (i * j) % k == 0) ++ans; } } diff --git a/solution/2100-2199/2178.Maximum Split of Positive Even Integers/Solution.cpp b/solution/2100-2199/2178.Maximum Split of Positive Even Integers/Solution.cpp index 75cb2495936f0..a676a913f66ae 100644 --- a/solution/2100-2199/2178.Maximum Split of Positive Even Integers/Solution.cpp +++ b/solution/2100-2199/2178.Maximum Split of Positive Even Integers/Solution.cpp @@ -3,8 +3,7 @@ class Solution { vector maximumEvenSplit(long long finalSum) { vector ans; if (finalSum % 2) return ans; - for (long long i = 2; i <= finalSum; i += 2) - { + for (long long i = 2; i <= finalSum; i += 2) { ans.push_back(i); finalSum -= i; } diff --git a/solution/2100-2199/2179.Count Good Triplets in an Array/Solution.cpp b/solution/2100-2199/2179.Count Good Triplets in an Array/Solution.cpp index 1949c47b4315e..eed78f719c19d 100644 --- a/solution/2100-2199/2179.Count Good Triplets in an Array/Solution.cpp +++ b/solution/2100-2199/2179.Count Good Triplets in an Array/Solution.cpp @@ -3,11 +3,12 @@ class BinaryIndexedTree { int n; vector c; - BinaryIndexedTree(int _n): n(_n), c(_n + 1){} + BinaryIndexedTree(int _n) + : n(_n) + , c(_n + 1) { } void update(int x, int delta) { - while (x <= n) - { + while (x <= n) { c[x] += delta; x += lowbit(x); } @@ -15,8 +16,7 @@ class BinaryIndexedTree { int query(int x) { int s = 0; - while (x > 0) - { + while (x > 0) { s += c[x]; x -= lowbit(x); } @@ -36,8 +36,7 @@ class Solution { for (int i = 0; i < n; ++i) pos[nums2[i]] = i + 1; BinaryIndexedTree* tree = new BinaryIndexedTree(n); long long ans = 0; - for (int& num : nums1) - { + for (int& num : nums1) { int p = pos[num]; int left = tree->query(p); int right = n - p - (tree->query(n) - tree->query(p)); diff --git a/solution/2100-2199/2180.Count Integers With Even Digit Sum/Solution.cpp b/solution/2100-2199/2180.Count Integers With Even Digit Sum/Solution.cpp index 21c03aa23869a..11330f466e6be 100644 --- a/solution/2100-2199/2180.Count Integers With Even Digit Sum/Solution.cpp +++ b/solution/2100-2199/2180.Count Integers With Even Digit Sum/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int countEven(int num) { int ans = 0; - for (int i = 1; i <= num; ++i) - { + for (int i = 1; i <= num; ++i) { int t = 0; for (int j = i; j; j /= 10) t += j % 10; if (t % 2 == 0) ++ans; diff --git a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp index b8deec4f7825a..9955f1f2339cf 100644 --- a/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp +++ b/solution/2100-2199/2181.Merge Nodes in Between Zeros/Solution.cpp @@ -14,11 +14,10 @@ class Solution { ListNode* dummy = new ListNode(); ListNode* tail = dummy; int s = 0; - for (ListNode* cur = head->next; cur; cur = cur->next) - { - if (cur->val) s += cur->val; - else - { + for (ListNode* cur = head->next; cur; cur = cur->next) { + if (cur->val) + s += cur->val; + else { tail->next = new ListNode(s); tail = tail->next; s = 0; diff --git a/solution/2100-2199/2182.Construct String With Repeat Limit/Solution.cpp b/solution/2100-2199/2182.Construct String With Repeat Limit/Solution.cpp index ad4bb6f6f1e5a..aaf4d07b19528 100644 --- a/solution/2100-2199/2182.Construct String With Repeat Limit/Solution.cpp +++ b/solution/2100-2199/2182.Construct String With Repeat Limit/Solution.cpp @@ -4,13 +4,10 @@ class Solution { vector cnt(26); for (char& c : s) cnt[c - 'a']++; string ans; - for (int i = 25; ~i; --i) - { + for (int i = 25; ~i; --i) { int j = i - 1; - while (true) - { - for (int k = min(cnt[i], repeatLimit); k; --k) - { + while (true) { + for (int k = min(cnt[i], repeatLimit); k; --k) { cnt[i]--; ans.push_back('a' + i); } diff --git a/solution/2100-2199/2187.Minimum Time to Complete Trips/Solution.cpp b/solution/2100-2199/2187.Minimum Time to Complete Trips/Solution.cpp index 8831bd23bd098..33bd8e1ac2127 100644 --- a/solution/2100-2199/2187.Minimum Time to Complete Trips/Solution.cpp +++ b/solution/2100-2199/2187.Minimum Time to Complete Trips/Solution.cpp @@ -2,14 +2,15 @@ class Solution { public: long long minimumTime(vector& time, int totalTrips) { int mi = *min_element(time.begin(), time.end()); - long long left = 1, right = (long long) mi * totalTrips; - while (left < right) - { + long long left = 1, right = (long long)mi * totalTrips; + while (left < right) { long long cnt = 0; long long mid = (left + right) >> 1; for (int v : time) cnt += mid / v; - if (cnt >= totalTrips) right = mid; - else left = mid + 1; + if (cnt >= totalTrips) + right = mid; + else + left = mid + 1; } return left; } diff --git a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.cpp b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.cpp index 5a76c42028c29..36a307ef9f552 100644 --- a/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.cpp +++ b/solution/2100-2199/2190.Most Frequent Number Following Key In an Array/Solution.cpp @@ -3,14 +3,11 @@ class Solution { int mostFrequent(vector& nums, int key) { vector cnt(1010); int mx = 0, ans = 0; - for (int i = 0; i < nums.size() - 1; ++i) - { - if (nums[i] == key) - { + for (int i = 0; i < nums.size() - 1; ++i) { + if (nums[i] == key) { int target = nums[i + 1]; ++cnt[target]; - if (mx < cnt[target]) - { + if (mx < cnt[target]) { mx = cnt[target]; ans = nums[i + 1]; } diff --git a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cpp b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cpp index 3af7bd3e58a89..ee0ce4578f8cd 100644 --- a/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cpp +++ b/solution/2100-2199/2192.All Ancestors of a Node in a Directed Acyclic Graph/Solution.cpp @@ -4,25 +4,20 @@ class Solution { vector> g(n); for (auto& e : edges) g[e[1]].push_back(e[0]); vector> ans; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { vector t; - if (g[i].empty()) - { + if (g[i].empty()) { ans.push_back(t); continue; } - queue q{{i}}; + queue q {{i}}; vector vis(n); vis[i] = true; - while (!q.empty()) - { - for (int j = q.size(); j > 0; --j) - { + while (!q.empty()) { + for (int j = q.size(); j > 0; --j) { int v = q.front(); q.pop(); - for (int u : g[v]) - { + for (int u : g[v]) { if (vis[u]) continue; vis[u] = true; q.push(u); diff --git a/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/Solution.cpp b/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/Solution.cpp index 60fd037a9cd0b..e20c4fa8515a0 100644 --- a/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/Solution.cpp +++ b/solution/2100-2199/2193.Minimum Number of Moves to Make Palindrome/Solution.cpp @@ -3,16 +3,12 @@ class Solution { int minMovesToMakePalindrome(string s) { int n = s.size(); int ans = 0; - for (int i = 0, j = n - 1; i < j; ++i) - { + for (int i = 0, j = n - 1; i < j; ++i) { bool even = false; - for (int k = j; k != i; --k) - { - if (s[i] == s[k]) - { + for (int k = j; k != i; --k) { + if (s[i] == s[k]) { even = true; - for (; k < j; ++k) - { + for (; k < j; ++k) { swap(s[k], s[k + 1]); ++ans; } diff --git a/solution/2100-2199/2195.Append K Integers With Minimal Sum/Solution.cpp b/solution/2100-2199/2195.Append K Integers With Minimal Sum/Solution.cpp index 6debe22fa9cff..81636b0647b94 100644 --- a/solution/2100-2199/2195.Append K Integers With Minimal Sum/Solution.cpp +++ b/solution/2100-2199/2195.Append K Integers With Minimal Sum/Solution.cpp @@ -5,8 +5,7 @@ class Solution { nums.push_back(2e9); sort(nums.begin(), nums.end()); long long ans = 0; - for (int i = 1; i < nums.size(); ++i) - { + for (int i = 1; i < nums.size(); ++i) { int a = nums[i - 1], b = nums[i]; int n = min(k, b - a - 1); if (n <= 0) continue; diff --git a/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.cpp b/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.cpp index 19e6e67fe4170..d12c0cba56bfc 100644 --- a/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.cpp +++ b/solution/2100-2199/2196.Create Binary Tree From Descriptions/Solution.cpp @@ -14,17 +14,17 @@ class Solution { TreeNode* createBinaryTree(vector>& descriptions) { unordered_map m; unordered_set vis; - for (auto& d : descriptions) - { + for (auto& d : descriptions) { int p = d[0], c = d[1], left = d[2]; if (!m.count(p)) m[p] = new TreeNode(p); if (!m.count(c)) m[c] = new TreeNode(c); - if (left) m[p]->left = m[c]; - else m[p]->right = m[c]; + if (left) + m[p]->left = m[c]; + else + m[p]->right = m[c]; vis.insert(c); } - for (auto& [v, node] : m) - { + for (auto& [v, node] : m) { if (!vis.count(v)) return node; } return nullptr; diff --git a/solution/2100-2199/2198.Number of Single Divisor Triplets/Solution.cpp b/solution/2100-2199/2198.Number of Single Divisor Triplets/Solution.cpp index e3efb26570d85..e444bc0f65b20 100644 --- a/solution/2100-2199/2198.Number of Single Divisor Triplets/Solution.cpp +++ b/solution/2100-2199/2198.Number of Single Divisor Triplets/Solution.cpp @@ -4,20 +4,21 @@ class Solution { vector counter(101); for (int& x : nums) ++counter[x]; long long ans = 0; - for (int i = 1; i <= 100; ++i) - { - for (int j = 1; j <= 100; ++j) - { - for (int k = 1; k <= 100; ++k) - { + for (int i = 1; i <= 100; ++i) { + for (int j = 1; j <= 100; ++j) { + for (int k = 1; k <= 100; ++k) { int cnt1 = counter[i], cnt2 = counter[j], cnt3 = counter[k]; int s = i + j + k; int cnt = (s % i == 0) + (s % j == 0) + (s % k == 0); if (cnt != 1) continue; - if (i == j) ans += 1ll * cnt1 * (cnt1 - 1) * cnt3; - else if (i == k) ans += 1ll * cnt1 * (cnt1 - 1) * cnt2; - else if (j == k) ans += 1ll * cnt1 * cnt2 * (cnt2 - 1); - else ans += 1ll * cnt1 * cnt2 * cnt3; + if (i == j) + ans += 1ll * cnt1 * (cnt1 - 1) * cnt3; + else if (i == k) + ans += 1ll * cnt1 * (cnt1 - 1) * cnt2; + else if (j == k) + ans += 1ll * cnt1 * cnt2 * (cnt2 - 1); + else + ans += 1ll * cnt1 * cnt2 * cnt3; } } } diff --git a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.cpp b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.cpp index 45e095a38e8f9..d5fa87ca2d76a 100644 --- a/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.cpp +++ b/solution/2200-2299/2200.Find All K-Distant Indices in an Array/Solution.cpp @@ -3,12 +3,9 @@ class Solution { vector findKDistantIndices(vector& nums, int key, int k) { int n = nums.size(); vector ans; - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (abs(i - j) <= k && nums[j] == key) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (abs(i - j) <= k && nums[j] == key) { ans.push_back(i); break; } diff --git a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp index 75b6fc9678462..ec2da4b9a83d0 100644 --- a/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp +++ b/solution/2200-2299/2201.Count Artifacts That Can Be Extracted/Solution.cpp @@ -10,12 +10,9 @@ class Solution { bool check(vector& a, unordered_set& s, int n) { int r1 = a[0], c1 = a[1], r2 = a[2], c2 = a[3]; - for (int i = r1; i <= r2; ++i) - { - for (int j = c1; j <= c2; ++j) - { - if (!s.count(i * n + j)) - { + for (int i = r1; i <= r2; ++i) { + for (int j = c1; j <= c2; ++j) { + if (!s.count(i * n + j)) { return false; } } diff --git a/solution/2200-2299/2202.Maximize the Topmost Element After K Moves/Solution.cpp b/solution/2200-2299/2202.Maximize the Topmost Element After K Moves/Solution.cpp index f35d1899dfa86..c35fe3fa3df91 100644 --- a/solution/2200-2299/2202.Maximize the Topmost Element After K Moves/Solution.cpp +++ b/solution/2200-2299/2202.Maximize the Topmost Element After K Moves/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int maximumTop(vector& nums, int k) { if (k == 0) return nums[0]; int n = nums.size(); - if (n == 1) - { + if (n == 1) { if (k % 2) return -1; return nums[0]; } diff --git a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp index ab2f94a023a11..fcd9989338aa1 100644 --- a/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp +++ b/solution/2200-2299/2207.Maximize Number of Subsequences in a String/Solution.cpp @@ -4,8 +4,7 @@ class Solution { long long ans = 0; char a = pattern[0], b = pattern[1]; vector cnt(26); - for (char& c : text) - { + for (char& c : text) { if (c == b) ans += cnt[a - 'a']; cnt[c - 'a']++; } diff --git a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp index a854c8b9a7ece..cc37499ef12de 100644 --- a/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp +++ b/solution/2200-2299/2208.Minimum Operations to Halve Array Sum/Solution.cpp @@ -3,15 +3,13 @@ class Solution { int halveArray(vector& nums) { priority_queue q; long long s = 0; - for (int& v : nums) - { + for (int& v : nums) { s += v; q.push(v); } double d = s / 2.0; int ans = 0; - while (d > 0) - { + while (d > 0) { double t = q.top() / 2; q.pop(); d -= t; diff --git a/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.cpp b/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.cpp index a604ec40db0d2..68da0329f353a 100644 --- a/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.cpp +++ b/solution/2200-2299/2210.Count Hills and Valleys in an Array/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int countHillValley(vector& nums) { int ans = 0; - for (int i = 1, j = 0; i < nums.size() - 1; ++i) - { + for (int i = 1, j = 0; i < nums.size() - 1; ++i) { if (nums[i] == nums[i + 1]) continue; if (nums[i] > nums[j] && nums[i] > nums[i + 1]) ++ans; if (nums[i] < nums[j] && nums[i] < nums[i + 1]) ++ans; diff --git a/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp b/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp index 47b472901a5e2..b630e97a169ba 100644 --- a/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp +++ b/solution/2200-2299/2211.Count Collisions on a Road/Solution.cpp @@ -1,17 +1,16 @@ class Solution { public: int countCollisions(string directions) { - int l = 0, r = directions.size() -1, count = 0; + int l = 0, r = directions.size() - 1, count = 0; while (l <= r && directions[l] == 'L') { l++; } while (l <= r && directions[r] == 'R') { r--; } - for (int i = l; i <=r; i++) { + for (int i = l; i <= r; i++) { count += directions[i] != 'S'; } return count; - } }; \ No newline at end of file diff --git a/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.cpp b/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.cpp index 51508a0e86450..0530b3d4c618c 100644 --- a/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.cpp +++ b/solution/2200-2299/2212.Maximum Points in an Archery Competition/Solution.cpp @@ -3,28 +3,22 @@ class Solution { vector maximumBobPoints(int numArrows, vector& aliceArrows) { int n = aliceArrows.size(); int state = 0, mx = -1; - for (int mask = 1; mask < 1 << n; ++mask) - { + for (int mask = 1; mask < 1 << n; ++mask) { int cnt = 0, points = 0; - for (int i = 0; i < n; ++i) - { - if ((mask >> i) & 1) - { + for (int i = 0; i < n; ++i) { + if ((mask >> i) & 1) { cnt += aliceArrows[i] + 1; points += i; } } - if (cnt <= numArrows && mx < points) - { + if (cnt <= numArrows && mx < points) { state = mask; mx = points; } } vector ans(n); - for (int i = 0; i < n; ++i) - { - if ((state >> i) & 1) - { + for (int i = 0; i < n; ++i) { + if ((state >> i) & 1) { ans[i] = aliceArrows[i] + 1; numArrows -= ans[i]; } diff --git a/solution/2200-2299/2213.Longest Substring of One Repeating Character/Solution.cpp b/solution/2200-2299/2213.Longest Substring of One Repeating Character/Solution.cpp index bec2e8eef38fa..84f3c737a2513 100644 --- a/solution/2200-2299/2213.Longest Substring of One Repeating Character/Solution.cpp +++ b/solution/2200-2299/2213.Longest Substring of One Repeating Character/Solution.cpp @@ -21,8 +21,7 @@ class SegmentTree { void build(int u, int l, int r) { tr[u]->l = l; tr[u]->r = r; - if (l == r) - { + if (l == r) { tr[u]->lmx = tr[u]->rmx = tr[u]->mx = tr[u]->size = 1; tr[u]->lc = tr[u]->rc = s[l - 1]; return; @@ -34,14 +33,15 @@ class SegmentTree { } void modify(int u, int x, char v) { - if (tr[u]->l == x && tr[u]->r == x) - { + if (tr[u]->l == x && tr[u]->r == x) { tr[u]->lc = tr[u]->rc = v; return; } int mid = (tr[u]->l + tr[u]->r) >> 1; - if (x <= mid) modify(u << 1, x, v); - else modify(u << 1 | 1, x, v); + if (x <= mid) + modify(u << 1, x, v); + else + modify(u << 1 | 1, x, v); pushup(u); } @@ -66,8 +66,7 @@ class SegmentTree { root->lmx = left->lmx; root->rmx = right->rmx; - if (left->rc == right->lc) - { + if (left->rc == right->lc) { if (left->lmx == left->size) root->lmx += right->lmx; if (right->rmx == right->size) root->rmx += left->rmx; root->mx = max(root->mx, left->rmx + right->lmx); @@ -85,8 +84,7 @@ class Solution { SegmentTree* tree = new SegmentTree(s); int k = queryCharacters.size(); vector ans(k); - for (int i = 0; i < k; ++i) - { + for (int i = 0; i < k; ++i) { int x = queryIndices[i] + 1; tree->modify(1, x, queryCharacters[i]); ans[i] = tree->query(1, 1, s.size())->mx; diff --git a/solution/2200-2299/2214.Minimum Health to Beat Game/Solution.cpp b/solution/2200-2299/2214.Minimum Health to Beat Game/Solution.cpp index 00d7479f475e7..3765dee021f78 100644 --- a/solution/2200-2299/2214.Minimum Health to Beat Game/Solution.cpp +++ b/solution/2200-2299/2214.Minimum Health to Beat Game/Solution.cpp @@ -3,8 +3,7 @@ class Solution { long long minimumHealth(vector& damage, int armor) { long long s = 0; int mx = damage[0]; - for (int& v : damage) - { + for (int& v : damage) { s += v; mx = max(mx, v); } diff --git a/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.cpp b/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.cpp index 6f5e75d8f3d7b..7b6e4f0142a76 100644 --- a/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.cpp +++ b/solution/2200-2299/2217.Find Palindrome With Fixed Length/Solution.cpp @@ -4,11 +4,9 @@ class Solution { int l = (intLength + 1) >> 1; long long start = pow(10, l - 1), end = pow(10, l) - 1; vector ans; - for (int& q : queries) - { + for (int& q : queries) { long long v = start + q - 1; - if (v > end) - { + if (v > end) { ans.push_back(-1); continue; } diff --git a/solution/2200-2299/2218.Maximum Value of K Coins From Piles/Solution.cpp b/solution/2200-2299/2218.Maximum Value of K Coins From Piles/Solution.cpp index 31918cbd675bc..8f358e8405fa6 100644 --- a/solution/2200-2299/2218.Maximum Value of K Coins From Piles/Solution.cpp +++ b/solution/2200-2299/2218.Maximum Value of K Coins From Piles/Solution.cpp @@ -2,20 +2,16 @@ class Solution { public: int maxValueOfCoins(vector>& piles, int k) { vector> presum; - for (auto& p : piles) - { + for (auto& p : piles) { int m = p.size(); vector s(m + 1); for (int i = 0; i < m; ++i) s[i + 1] = s[i] + p[i]; presum.push_back(s); } vector dp(k + 1); - for (auto& s : presum) - { - for (int j = k; ~j; --j) - { - for (int idx = 0; idx < s.size(); ++idx) - { + for (auto& s : presum) { + for (int j = k; ~j; --j) { + for (int idx = 0; idx < s.size(); ++idx) { if (j >= idx) dp[j] = max(dp[j], dp[j - idx] + s[idx]); } } diff --git a/solution/2200-2299/2220.Minimum Bit Flips to Convert Number/Solution.cpp b/solution/2200-2299/2220.Minimum Bit Flips to Convert Number/Solution.cpp index 97caf96aedea5..06604f2c98b3d 100644 --- a/solution/2200-2299/2220.Minimum Bit Flips to Convert Number/Solution.cpp +++ b/solution/2200-2299/2220.Minimum Bit Flips to Convert Number/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minBitFlips(int start, int goal) { int t = start ^ goal; int ans = 0; - while (t) - { + while (t) { ans += t & 1; t >>= 1; } diff --git a/solution/2200-2299/2222.Number of Ways to Select Buildings/Solution.cpp b/solution/2200-2299/2222.Number of Ways to Select Buildings/Solution.cpp index cef4bf2895e7a..fa87b9ba4b1a8 100644 --- a/solution/2200-2299/2222.Number of Ways to Select Buildings/Solution.cpp +++ b/solution/2200-2299/2222.Number of Ways to Select Buildings/Solution.cpp @@ -7,15 +7,11 @@ class Solution { int cnt1 = n - cnt0; int c0 = 0, c1 = 0; long long ans = 0; - for (char& c : s) - { - if (c == '0') - { + for (char& c : s) { + if (c == '0') { ans += c1 * (cnt1 - c1); ++c0; - } - else - { + } else { ans += c0 * (cnt0 - c0); ++c1; } diff --git a/solution/2200-2299/2224.Minimum Number of Operations to Convert Time/Solution.cpp b/solution/2200-2299/2224.Minimum Number of Operations to Convert Time/Solution.cpp index 049d2476d7a18..35bebaaba0be2 100644 --- a/solution/2200-2299/2224.Minimum Number of Operations to Convert Time/Solution.cpp +++ b/solution/2200-2299/2224.Minimum Number of Operations to Convert Time/Solution.cpp @@ -5,8 +5,7 @@ class Solution { int b = stoi(correct.substr(0, 2)) * 60 + stoi(correct.substr(3, 2)); int ans = 0, d = b - a; vector inc = {60, 15, 5, 1}; - for (int i : inc) - { + for (int i : inc) { ans += d / i; d %= i; } diff --git a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp index 3bab37840e946..3771d8402358a 100644 --- a/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp +++ b/solution/2200-2299/2225.Find Players With Zero or One Losses/Solution.cpp @@ -2,15 +2,13 @@ class Solution { public: vector> findWinners(vector>& matches) { unordered_map cnt; - for (auto& m : matches) - { + for (auto& m : matches) { int a = m[0], b = m[1]; if (!cnt.count(a)) cnt[a] = 0; ++cnt[b]; } vector> ans(2); - for (auto& [u, v] : cnt) - { + for (auto& [u, v] : cnt) { if (v < 2) ans[v].push_back(u); } sort(ans[0].begin(), ans[0].end()); diff --git a/solution/2200-2299/2226.Maximum Candies Allocated to K Children/Solution.cpp b/solution/2200-2299/2226.Maximum Candies Allocated to K Children/Solution.cpp index 4bc7edc072c54..097e6764f6518 100644 --- a/solution/2200-2299/2226.Maximum Candies Allocated to K Children/Solution.cpp +++ b/solution/2200-2299/2226.Maximum Candies Allocated to K Children/Solution.cpp @@ -2,13 +2,14 @@ class Solution { public: int maximumCandies(vector& candies, long long k) { int left = 0, right = 1e7; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; long long cnt = 0; for (int& v : candies) cnt += v / mid; - if (cnt >= k) left = mid; - else right = mid - 1; + if (cnt >= k) + left = mid; + else + right = mid - 1; } return left; } diff --git a/solution/2200-2299/2227.Encrypt and Decrypt Strings/Solution.cpp b/solution/2200-2299/2227.Encrypt and Decrypt Strings/Solution.cpp index 7048861d59a06..12f376c62711e 100644 --- a/solution/2200-2299/2227.Encrypt and Decrypt Strings/Solution.cpp +++ b/solution/2200-2299/2227.Encrypt and Decrypt Strings/Solution.cpp @@ -7,17 +7,16 @@ class Encrypter { for (int i = 0; i < keys.size(); ++i) mp[keys[i]] = values[i]; for (auto v : dictionary) cnt[encrypt(v)]++; } - + string encrypt(string word1) { string res = ""; - for (char c : word1) - { + for (char c : word1) { if (!mp.count(c)) return ""; res += mp[c]; } return res; } - + int decrypt(string word2) { return cnt[word2]; } diff --git a/solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/Solution.cpp b/solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/Solution.cpp index 2f8bdab793712..faca23e21e1e1 100644 --- a/solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/Solution.cpp +++ b/solution/2200-2299/2231.Largest Number After Digit Swaps by Parity/Solution.cpp @@ -3,22 +3,18 @@ class Solution { int largestInteger(int num) { vector cnt(10); int x = num; - while (x) - { + while (x) { cnt[x % 10]++; x /= 10; } x = num; int ans = 0; long t = 1; - while (x) - { + while (x) { int v = x % 10; x /= 10; - for (int y = 0; y < 10; ++y) - { - if (((v ^ y) & 1) == 0 && cnt[y] > 0) - { + for (int y = 0; y < 10; ++y) { + if (((v ^ y) & 1) == 0 && cnt[y] > 0) { cnt[y]--; ans += y * t; t *= 10; diff --git a/solution/2200-2299/2233.Maximum Product After K Increments/Solution.cpp b/solution/2200-2299/2233.Maximum Product After K Increments/Solution.cpp index 1417c2e225018..19ed3988b54aa 100644 --- a/solution/2200-2299/2233.Maximum Product After K Increments/Solution.cpp +++ b/solution/2200-2299/2233.Maximum Product After K Increments/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int maximumProduct(vector& nums, int k) { int mod = 1e9 + 7; make_heap(nums.begin(), nums.end(), greater()); - while (k--) - { + while (k--) { pop_heap(nums.begin(), nums.end(), greater()); ++nums.back(); push_heap(nums.begin(), nums.end(), greater()); diff --git a/solution/2200-2299/2237.Count Positions on Street With Required Brightness/Solution.cpp b/solution/2200-2299/2237.Count Positions on Street With Required Brightness/Solution.cpp index 5de26804cfce2..86d9c812c8ee9 100644 --- a/solution/2200-2299/2237.Count Positions on Street With Required Brightness/Solution.cpp +++ b/solution/2200-2299/2237.Count Positions on Street With Required Brightness/Solution.cpp @@ -2,15 +2,13 @@ class Solution { public: int meetRequirement(int n, vector>& lights, vector& requirement) { vector d(100010); - for (auto& e : lights) - { + for (auto& e : lights) { int i = max(0, e[0] - e[1]), j = min(n - 1, e[0] + e[1]); ++d[i]; --d[j + 1]; } int s = 0, ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { s += d[i]; if (s >= requirement[i]) ++ans; } diff --git a/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp index 6fc2597045a09..1cd7003d8e05b 100644 --- a/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp +++ b/solution/2200-2299/2239.Find Closest Number to Zero/Solution.cpp @@ -2,11 +2,9 @@ class Solution { public: int findClosestNumber(vector& nums) { int ans = 0, d = 1e6; - for (int& v : nums) - { + for (int& v : nums) { int t = abs(v); - if (t < d || (t == d && v > ans)) - { + if (t < d || (t == d && v > ans)) { ans = v; d = t; } diff --git a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp index 91609af3996eb..d319a2519250b 100644 --- a/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp +++ b/solution/2200-2299/2240.Number of Ways to Buy Pens and Pencils/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: long long waysToBuyPensPencils(int total, int cost1, int cost2) { long long ans = 0; - for (int x = 0; x <= total / cost1; ++x) - { + for (int x = 0; x <= total / cost1; ++x) { int v = total - x * cost1; ans += v / cost2 + 1; } diff --git a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.cpp b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.cpp index 005b5c30a82a7..e2c4b7c1c34ce 100644 --- a/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.cpp +++ b/solution/2200-2299/2244.Minimum Rounds to Complete All Tasks/Solution.cpp @@ -4,8 +4,7 @@ class Solution { unordered_map cnt; for (int& t : tasks) ++cnt[t]; int ans = 0; - for (auto& [_, v] : cnt) - { + for (auto& [_, v] : cnt) { if (v == 1) return -1; ans += v / 3 + (v % 3 == 0 ? 0 : 1); } diff --git a/solution/2200-2299/2249.Count Lattice Points Inside a Circle/Solution.cpp b/solution/2200-2299/2249.Count Lattice Points Inside a Circle/Solution.cpp index 36bbf80468fed..239d716652b23 100644 --- a/solution/2200-2299/2249.Count Lattice Points Inside a Circle/Solution.cpp +++ b/solution/2200-2299/2249.Count Lattice Points Inside a Circle/Solution.cpp @@ -2,15 +2,11 @@ class Solution { public: int countLatticePoints(vector>& circles) { int ans = 0; - for (int i = 0; i <= 200; ++i) - { - for (int j = 0; j <= 200; ++j) - { - for (auto& c : circles) - { + for (int i = 0; i <= 200; ++i) { + for (int j = 0; j <= 200; ++j) { + for (auto& c : circles) { int x = c[0] - i, y = c[1] - j, r = c[2]; - if (x * x + y * y <= r * r) - { + if (x * x + y * y <= r * r) { ++ans; break; } diff --git a/solution/2200-2299/2250.Count Number of Rectangles Containing Each Point/Solution.cpp b/solution/2200-2299/2250.Count Number of Rectangles Containing Each Point/Solution.cpp index c5ed0871af554..488f6fa0c5eae 100644 --- a/solution/2200-2299/2250.Count Number of Rectangles Containing Each Point/Solution.cpp +++ b/solution/2200-2299/2250.Count Number of Rectangles Containing Each Point/Solution.cpp @@ -6,12 +6,10 @@ class Solution { for (auto& r : rectangles) d[r[1]].push_back(r[0]); for (auto& v : d) sort(v.begin(), v.end()); vector ans; - for (auto& p : points) - { + for (auto& p : points) { int x = p[0], y = p[1]; int cnt = 0; - for (int h = y; h < n; ++h) - { + for (int h = y; h < n; ++h) { auto& xs = d[h]; cnt += xs.size() - (lower_bound(xs.begin(), xs.end(), x) - xs.begin()); } diff --git a/solution/2200-2299/2256.Minimum Average Difference/Solution.cpp b/solution/2200-2299/2256.Minimum Average Difference/Solution.cpp index efdf71fb62d3c..9341f31d1e20f 100644 --- a/solution/2200-2299/2256.Minimum Average Difference/Solution.cpp +++ b/solution/2200-2299/2256.Minimum Average Difference/Solution.cpp @@ -9,13 +9,11 @@ class Solution { for (int i = 1; i < n; ++i) s[i] = s[i - 1] + nums[i]; int ans = 0; ll mi = LONG_MAX; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { ll a = s[i] / (i + 1); ll b = i == n - 1 ? 0 : (s[n - 1] - s[i]) / (n - i - 1); ll t = abs(a - b); - if (mi > t) - { + if (mi > t) { ans = i; mi = t; } diff --git a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.cpp b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.cpp index 4df3ea6951b4b..30706f8aa8d60 100644 --- a/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.cpp +++ b/solution/2200-2299/2257.Count Unguarded Cells in the Grid/Solution.cpp @@ -5,14 +5,11 @@ class Solution { for (auto& e : guards) g[e[0]][e[1]] = 'g'; for (auto& e : walls) g[e[0]][e[1]] = 'w'; vector> dirs = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; - for (auto& p : guards) - { - for (auto& dir : dirs) - { + for (auto& p : guards) { + for (auto& dir : dirs) { int a = dir[0], b = dir[1]; int x = p[0], y = p[1]; - while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w' && g[x + a][y + b] != 'g') - { + while (x + a >= 0 && x + a < m && y + b >= 0 && y + b < n && g[x + a][y + b] != 'w' && g[x + a][y + b] != 'g') { x += a; y += b; g[x][y] = 'v'; diff --git a/solution/2200-2299/2258.Escape the Spreading Fire/Solution.cpp b/solution/2200-2299/2258.Escape the Spreading Fire/Solution.cpp index 05467a66c6e1f..81c2185fccfed 100644 --- a/solution/2200-2299/2258.Escape the Spreading Fire/Solution.cpp +++ b/solution/2200-2299/2258.Escape the Spreading Fire/Solution.cpp @@ -5,11 +5,12 @@ class Solution { int maximumMinutes(vector>& grid) { int m = grid.size(), n = grid[0].size(); int left = -1, right = m * n; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; - if (check(mid, grid)) left = mid; - else right = mid - 1; + if (check(mid, grid)) + left = mid; + else + right = mid - 1; } return left == m * n ? 1e9 : left; } @@ -18,12 +19,9 @@ class Solution { int m = grid.size(), n = grid[0].size(); vector> fire(m, vector(n)); queue> f; - for (int i = 0; i < m; ++i) - { - for (int j = 0; j < n; ++j) - { - if (grid[i][j] == 1) - { + for (int i = 0; i < m; ++i) { + for (int j = 0; j < n; ++j) { + if (grid[i][j] == 1) { fire[i][j] = true; f.push({i, j}); } @@ -34,18 +32,14 @@ class Solution { vector> vis(m, vector(n)); q.push({0, 0}); vis[0][0] = true; - while (!q.empty()) - { - for (int i = q.size(); i > 0; --i) - { + while (!q.empty()) { + for (int i = q.size(); i > 0; --i) { auto p = q.front(); q.pop(); if (fire[p[0]][p[1]]) continue; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = p[0] + dirs[k], y = p[1] + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && !fire[x][y] &&!vis[x][y] && grid[x][y] == 0) - { + if (x >= 0 && x < m && y >= 0 && y < n && !fire[x][y] && !vis[x][y] && grid[x][y] == 0) { if (x == m - 1 && y == n - 1) return true; vis[x][y] = true; q.push({x, y}); @@ -60,15 +54,12 @@ class Solution { queue> spread(vector>& fire, queue>& f, vector>& grid) { queue> nf; int m = grid.size(), n = grid[0].size(); - while (!f.empty()) - { + while (!f.empty()) { auto p = f.front(); f.pop(); - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = p[0] + dirs[k], y = p[1] + dirs[k + 1]; - if (x >= 0 && x < m && y >= 0 && y < n && !fire[x][y] && grid[x][y] == 0) - { + if (x >= 0 && x < m && y >= 0 && y < n && !fire[x][y] && grid[x][y] == 0) { fire[x][y] = true; nf.push({x, y}); } diff --git a/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/Solution.cpp b/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/Solution.cpp index 496f9a33e30ff..6819615ee0572 100644 --- a/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/Solution.cpp +++ b/solution/2200-2299/2259.Remove Digit From Number to Maximize Result/Solution.cpp @@ -2,11 +2,9 @@ class Solution { public: string removeDigit(string number, char digit) { string ans = "0"; - for (int i = 0, n = number.size(); i < n; ++i) - { + for (int i = 0, n = number.size(); i < n; ++i) { char d = number[i]; - if (d == digit) - { + if (d == digit) { string t = number.substr(0, i) + number.substr(i + 1, n - i); if (ans < t) ans = t; } diff --git a/solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/Solution.cpp b/solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/Solution.cpp index 7a9dc3c17f1a4..bd949eb67f332 100644 --- a/solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/Solution.cpp +++ b/solution/2200-2299/2260.Minimum Consecutive Cards to Pick Up/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int minimumCardPickup(vector& cards) { unordered_map m; int ans = 1e6; - for (int i = 0; i < cards.size(); ++i) - { + for (int i = 0; i < cards.size(); ++i) { int c = cards[i]; if (m.count(c)) ans = min(ans, i - m[c] + 1); m[c] = i; diff --git a/solution/2200-2299/2261.K Divisible Elements Subarrays/Solution.cpp b/solution/2200-2299/2261.K Divisible Elements Subarrays/Solution.cpp index c00a98286edb5..f1824766668f9 100644 --- a/solution/2200-2299/2261.K Divisible Elements Subarrays/Solution.cpp +++ b/solution/2200-2299/2261.K Divisible Elements Subarrays/Solution.cpp @@ -2,12 +2,10 @@ class Solution { public: int countDistinct(vector& nums, int k, int p) { unordered_set s; - for (int i = 0, n = nums.size(); i < n; ++i) - { + for (int i = 0, n = nums.size(); i < n; ++i) { int cnt = 0; string t = ""; - for (int j = i; j < n; ++j) - { + for (int j = i; j < n; ++j) { if (nums[j] % p == 0) ++cnt; if (cnt > k) break; t += to_string(nums[j]) + ","; diff --git a/solution/2200-2299/2262.Total Appeal of A String/Solution.cpp b/solution/2200-2299/2262.Total Appeal of A String/Solution.cpp index ad44c28264291..9f626a13c8bcd 100644 --- a/solution/2200-2299/2262.Total Appeal of A String/Solution.cpp +++ b/solution/2200-2299/2262.Total Appeal of A String/Solution.cpp @@ -3,8 +3,7 @@ class Solution { long long appealSum(string s) { long long ans = 0, t = 0; vector pos(26, -1); - for (int i = 0; i < s.size(); ++i) - { + for (int i = 0; i < s.size(); ++i) { int c = s[i] - 'a'; t += i - pos[c]; ans += t; diff --git a/solution/2200-2299/2264.Largest 3-Same-Digit Number in String/Solution.cpp b/solution/2200-2299/2264.Largest 3-Same-Digit Number in String/Solution.cpp index 1626a5313a4b5..2b1719c425507 100644 --- a/solution/2200-2299/2264.Largest 3-Same-Digit Number in String/Solution.cpp +++ b/solution/2200-2299/2264.Largest 3-Same-Digit Number in String/Solution.cpp @@ -1,8 +1,7 @@ class Solution { public: string largestGoodInteger(string num) { - for (char i = '9'; i >= '0'; --i) - { + for (char i = '9'; i >= '0'; --i) { string t(3, i); if (num.find(t) != string::npos) return t; } diff --git a/solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/Solution.cpp b/solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/Solution.cpp index 6e1195eb80303..20ba4d6722ca3 100644 --- a/solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/Solution.cpp +++ b/solution/2200-2299/2267.Check if There Is a Valid Parentheses String Path/Solution.cpp @@ -15,8 +15,7 @@ class Solution { if (t < 0) return false; int m = grid.size(), n = grid[0].size(); if (i == m - 1 && j == n - 1) return t == 0; - for (int k = 0; k < 2; ++k) - { + for (int k = 0; k < 2; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x < m && y < n && dfs(x, y, t, grid)) return true; } diff --git a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp index 24425a88c94eb..d7c311261b85d 100644 --- a/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp +++ b/solution/2200-2299/2268.Minimum Number of Keypresses/Solution.cpp @@ -5,8 +5,7 @@ class Solution { for (char& c : s) ++cnt[c - 'a']; sort(cnt.begin(), cnt.end()); int ans = 0; - for (int i = 1, j = 1; i <= 26; ++i) - { + for (int i = 1, j = 1; i <= 26; ++i) { ans += j * cnt[26 - i]; if (i % 9 == 0) ++j; } diff --git a/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.cpp b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.cpp index e6f705b6d4cf7..ce78879085d0c 100644 --- a/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.cpp +++ b/solution/2200-2299/2269.Find the K-Beauty of a Number/Solution.cpp @@ -3,8 +3,7 @@ class Solution { int divisorSubstrings(int num, int k) { int ans = 0; string s = to_string(num); - for (int i = 0; i < s.size() - k + 1; ++i) - { + for (int i = 0; i < s.size() - k + 1; ++i) { int t = stoi(s.substr(i, k)); ans += t && num % t == 0; } diff --git a/solution/2200-2299/2270.Number of Ways to Split Array/Solution.cpp b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.cpp index ceef684e36c71..ee688d9bb07a0 100644 --- a/solution/2200-2299/2270.Number of Ways to Split Array/Solution.cpp +++ b/solution/2200-2299/2270.Number of Ways to Split Array/Solution.cpp @@ -4,8 +4,7 @@ class Solution { long long s = accumulate(nums.begin(), nums.end(), 0ll); long long t = 0; int ans = 0; - for (int i = 0; i < nums.size() - 1; ++i) - { + for (int i = 0; i < nums.size() - 1; ++i) { t += nums[i]; ans += t >= s - t; } diff --git a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp index d7e47e9d9255a..0cc800926f796 100644 --- a/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp +++ b/solution/2200-2299/2279.Maximum Bags With Full Capacity of Rocks/Solution.cpp @@ -6,8 +6,7 @@ class Solution { for (int i = 0; i < n; ++i) d[i] = capacity[i] - rocks[i]; sort(d.begin(), d.end()); int ans = 0; - for (int& v : d) - { + for (int& v : d) { if (v > additionalRocks) break; ++ans; additionalRocks -= v; diff --git a/solution/2200-2299/2280.Minimum Lines to Represent a Line Chart/Solution.cpp b/solution/2200-2299/2280.Minimum Lines to Represent a Line Chart/Solution.cpp index 6e675363d867c..c6c170f35488f 100644 --- a/solution/2200-2299/2280.Minimum Lines to Represent a Line Chart/Solution.cpp +++ b/solution/2200-2299/2280.Minimum Lines to Represent a Line Chart/Solution.cpp @@ -4,12 +4,11 @@ class Solution { sort(stockPrices.begin(), stockPrices.end()); int dx = 0, dy = 1; int ans = 0; - for (int i = 1; i < stockPrices.size(); ++i) - { + for (int i = 1; i < stockPrices.size(); ++i) { int x = stockPrices[i - 1][0], y = stockPrices[i - 1][1]; int x1 = stockPrices[i][0], y1 = stockPrices[i][1]; int dx1 = x1 - x, dy1 = y1 - y; - if ((long long) dy * dx1 != (long long) dx * dy1) ++ans; + if ((long long)dy * dx1 != (long long)dx * dy1) ++ans; dx = dx1; dy = dy1; } diff --git a/solution/2200-2299/2281.Sum of Total Strength of Wizards/Solution.cpp b/solution/2200-2299/2281.Sum of Total Strength of Wizards/Solution.cpp index 76fa4981a22de..d11e1cab07da6 100644 --- a/solution/2200-2299/2281.Sum of Total Strength of Wizards/Solution.cpp +++ b/solution/2200-2299/2281.Sum of Total Strength of Wizards/Solution.cpp @@ -5,15 +5,13 @@ class Solution { vector left(n, -1); vector right(n, n); stack stk; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { while (!stk.empty() && strength[stk.top()] >= strength[i]) stk.pop(); if (!stk.empty()) left[i] = stk.top(); stk.push(i); } stk = stack(); - for (int i = n - 1; i >= 0; --i) - { + for (int i = n - 1; i >= 0; --i) { while (!stk.empty() && strength[stk.top()] > strength[i]) stk.pop(); if (!stk.empty()) right[i] = stk.top(); stk.push(i); @@ -24,14 +22,13 @@ class Solution { vector ss(n + 2); for (int i = 0; i < n + 1; ++i) ss[i + 1] = (ss[i] + s[i]) % mod; int ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = strength[i]; int l = left[i] + 1, r = right[i] - 1; - long a = (long) (i - l + 1) * (ss[r + 2] - ss[i + 1]); - long b = (long) (r - i + 1) * (ss[i + 1] - ss[l]); + long a = (long)(i - l + 1) * (ss[r + 2] - ss[i + 1]); + long b = (long)(r - i + 1) * (ss[i + 1] - ss[l]); ans = (ans + v * ((a - b) % mod)) % mod; } - return (int) (ans + mod) % mod; + return (int)(ans + mod) % mod; } }; \ No newline at end of file diff --git a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.cpp b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.cpp index 034af187d9a98..53dffbd965c8a 100644 --- a/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.cpp +++ b/solution/2200-2299/2283.Check if Number Has Equal Digit Count and Digit Value/Solution.cpp @@ -3,8 +3,7 @@ class Solution { bool digitCount(string num) { vector cnt(10); for (char& c : num) ++cnt[c - '0']; - for (int i = 0; i < num.size(); ++i) - { + for (int i = 0; i < num.size(); ++i) { int v = num[i] - '0'; if (cnt[i] != v) return false; } diff --git a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp index 14649d45e59cc..75e3dbd2d321e 100644 --- a/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp +++ b/solution/2200-2299/2284.Sender With Largest Word Count/Solution.cpp @@ -3,18 +3,15 @@ class Solution { string largestWordCount(vector& messages, vector& senders) { unordered_map cnt; int n = senders.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = 0; - for (char& c : messages[i]) - { + for (char& c : messages[i]) { if (c == ' ') ++v; } cnt[senders[i]] += v + 1; } string ans = senders[0]; - for (auto& [u, v] : cnt) - { + for (auto& [u, v] : cnt) { if (v > cnt[ans] || (v == cnt[ans] && u > ans)) ans = u; } return ans; diff --git a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp index 3d4d678b51775..2772b440780c4 100644 --- a/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp +++ b/solution/2200-2299/2285.Maximum Total Importance of Roads/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: long long maximumImportance(int n, vector>& roads) { vector deg(n); - for (auto& r : roads) - { + for (auto& r : roads) { ++deg[r[0]]; ++deg[r[1]]; } diff --git a/solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.cpp b/solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.cpp index 1c421d003877e..9f1c42e24ab97 100644 --- a/solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.cpp +++ b/solution/2200-2299/2287.Rearrange Characters to Make Target String/Solution.cpp @@ -6,11 +6,10 @@ class Solution { for (char& c : s) ++cnt1[c - 'a']; for (char& c : target) ++cnt2[c - 'a']; int ans = 100; - for (int i = 0; i < 26; ++i) - { + for (int i = 0; i < 26; ++i) { if (cnt2[i] <= 0) continue; if (cnt1[i] < cnt2[i]) return 0; - ans = min(ans, cnt1[i] / cnt2[i]); + ans = min(ans, cnt1[i] / cnt2[i]); } return ans; } diff --git a/solution/2200-2299/2289.Steps to Make Array Non-decreasing/Solution.cpp b/solution/2200-2299/2289.Steps to Make Array Non-decreasing/Solution.cpp index fa9347f6deb03..6af5f80d84967 100644 --- a/solution/2200-2299/2289.Steps to Make Array Non-decreasing/Solution.cpp +++ b/solution/2200-2299/2289.Steps to Make Array Non-decreasing/Solution.cpp @@ -4,10 +4,8 @@ class Solution { stack stk; int ans = 0, n = nums.size(); vector dp(n); - for (int i = n - 1; i >= 0; --i) - { - while (!stk.empty() && nums[i] > nums[stk.top()]) - { + for (int i = n - 1; i >= 0; --i) { + while (!stk.empty() && nums[i] > nums[stk.top()]) { dp[i] = max(dp[i] + 1, dp[stk.top()]); ans = max(ans, dp[i]); stk.pop(); diff --git a/solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/Solution.cpp b/solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/Solution.cpp index 5e1816e5f1c8e..ae9f998d2869c 100644 --- a/solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/Solution.cpp +++ b/solution/2200-2299/2290.Minimum Obstacle Removal to Reach Corner/Solution.cpp @@ -2,22 +2,22 @@ class Solution { public: int minimumObstacles(vector>& grid) { int m = grid.size(), n = grid[0].size(); - deque> q{{0, 0, 0}}; + deque> q {{0, 0, 0}}; vector> vis(m, vector(n)); vector dirs = {-1, 0, 1, 0, -1}; - while (!q.empty()) - { + while (!q.empty()) { auto [i, j, k] = q.front(); q.pop_front(); if (i == m - 1 && j == n - 1) return k; if (vis[i][j]) continue; vis[i][j] = true; - for (int o = 0; o < 4; ++o) - { + for (int o = 0; o < 4; ++o) { int x = i + dirs[o], y = j + dirs[o + 1]; if (x >= 0 && x < m && y >= 0 && y < n) { - if (grid[x][y] == 0) q.push_front({x, y, k}); - else q.push_back({x, y, k + 1}); + if (grid[x][y] == 0) + q.push_front({x, y, k}); + else + q.push_back({x, y, k + 1}); } } } diff --git a/solution/2200-2299/2291.Maximum Profit From Trading Stocks/Solution.cpp b/solution/2200-2299/2291.Maximum Profit From Trading Stocks/Solution.cpp index bfb14566628c0..4acf66294180b 100644 --- a/solution/2200-2299/2291.Maximum Profit From Trading Stocks/Solution.cpp +++ b/solution/2200-2299/2291.Maximum Profit From Trading Stocks/Solution.cpp @@ -2,7 +2,7 @@ class Solution { public: int maximumProfit(vector& present, vector& future, int budget) { int n = present.size(); - vectordp(budget + 1); + vector dp(budget + 1); for (int i = 0; i < n; i++) { for (int j = budget; j >= present[i]; j--) { dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]); diff --git a/solution/2200-2299/2293.Min Max Game/Solution.cpp b/solution/2200-2299/2293.Min Max Game/Solution.cpp index 61810f0c20580..de0abd771e334 100644 --- a/solution/2200-2299/2293.Min Max Game/Solution.cpp +++ b/solution/2200-2299/2293.Min Max Game/Solution.cpp @@ -4,8 +4,7 @@ class Solution { int n = nums.size(); if (n == 1) return nums[0]; vector t(n >> 1); - for (int i = 0; i < t.size(); ++i) - { + for (int i = 0; i < t.size(); ++i) { int a = nums[i << 1], b = nums[i << 1 | 1]; t[i] = (i & 1) == 1 ? max(a, b) : min(a, b); } diff --git a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution.cpp b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution.cpp index f5eab371eafd4..2d5eb37ed18dc 100644 --- a/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution.cpp +++ b/solution/2200-2299/2294.Partition Array Such That Maximum Difference Is K/Solution.cpp @@ -3,13 +3,12 @@ class Solution { int partitionArray(vector& nums, int k) { sort(nums.begin(), nums.end()); int d = 0, ans = 1; - for (int i = 1; i < nums.size(); ++i) - { + for (int i = 1; i < nums.size(); ++i) { int a = nums[i - 1], b = nums[i]; int t = b - a; - if (d + t <= k) d += t; - else - { + if (d + t <= k) + d += t; + else { d = 0; ++ans; } diff --git a/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp b/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp index f6474db7b3c85..3984884ebfedb 100644 --- a/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp +++ b/solution/2200-2299/2295.Replace Elements in an Array/Solution.cpp @@ -4,8 +4,7 @@ class Solution { int n = nums.size(); unordered_map d; for (int i = 0; i < n; ++i) d[nums[i]] = i; - for (auto& op : operations) - { + for (auto& op : operations) { int a = op[0], b = op[1]; int idx = d[a]; d.erase(a); diff --git a/solution/2200-2299/2299.Strong Password Checker II/Solution.cpp b/solution/2200-2299/2299.Strong Password Checker II/Solution.cpp index a6442d7629f66..cef8e1d11fe7c 100644 --- a/solution/2200-2299/2299.Strong Password Checker II/Solution.cpp +++ b/solution/2200-2299/2299.Strong Password Checker II/Solution.cpp @@ -4,14 +4,17 @@ class Solution { if (password.size() < 8) return false; int ans = 0; char prev = '.'; - for (char& c : password) - { + for (char& c : password) { if (c == prev) return false; prev = c; - if (c >= 'a' && c <= 'z') ans |= 1; - else if (c >= 'A' && c <= 'Z') ans |= 2; - else if (c >= '0' && c <= '9') ans |= 4; - else ans |= 8; + if (c >= 'a' && c <= 'z') + ans |= 1; + else if (c >= 'A' && c <= 'Z') + ans |= 2; + else if (c >= '0' && c <= '9') + ans |= 4; + else + ans |= 8; } return ans == 15; } diff --git a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp index 53c20567dbbf0..1737379f5db90 100644 --- a/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp +++ b/solution/2300-2399/2300.Successful Pairs of Spells and Potions/Solution.cpp @@ -4,14 +4,14 @@ class Solution { sort(potions.begin(), potions.end()); int m = potions.size(); vector ans; - for (int& s : spells) - { + for (int& s : spells) { int left = 0, right = m; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; - if (1ll * s * potions[mid] >= success) right = mid; - else left = mid + 1; + if (1ll * s * potions[mid] >= success) + right = mid; + else + left = mid + 1; } ans.push_back(m - left); } diff --git a/solution/2300-2399/2301.Match Substring After Replacement/Solution.cpp b/solution/2300-2399/2301.Match Substring After Replacement/Solution.cpp index e4e5368b0754a..129138d15c2bc 100644 --- a/solution/2300-2399/2301.Match Substring After Replacement/Solution.cpp +++ b/solution/2300-2399/2301.Match Substring After Replacement/Solution.cpp @@ -4,11 +4,9 @@ class Solution { unordered_map> d; for (auto& m : mappings) d[m[0]].insert(m[1]); int n = s.size(), k = sub.size(); - for (int i = 0; i <= n - k; ++i) - { + for (int i = 0; i <= n - k; ++i) { bool flag = true; - for (int j = 0; j < k; ++j) - { + for (int j = 0; j < k; ++j) { char a = s[i + j], b = sub[j]; if (a == b || d[b].count(a)) continue; flag = false; diff --git a/solution/2300-2399/2302.Count Subarrays With Score Less Than K/Solution.cpp b/solution/2300-2399/2302.Count Subarrays With Score Less Than K/Solution.cpp index 7a028062554c9..e12fa848cb0dd 100644 --- a/solution/2300-2399/2302.Count Subarrays With Score Less Than K/Solution.cpp +++ b/solution/2300-2399/2302.Count Subarrays With Score Less Than K/Solution.cpp @@ -7,15 +7,15 @@ class Solution { vector s(n + 1); for (int i = 0; i < n; ++i) s[i + 1] = s[i] + nums[i]; ll ans = 0; - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { if (nums[i - 1] >= k) continue; int left = 1, right = i; - while (left < right) - { + while (left < right) { int mid = (left + right + 1) >> 1; - if ((s[i] - s[i - mid]) * mid < k) left = mid; - else right = mid - 1; + if ((s[i] - s[i - mid]) * mid < k) + left = mid; + else + right = mid - 1; } ans += left; } diff --git a/solution/2300-2399/2303.Calculate Amount Paid in Taxes/Solution.cpp b/solution/2300-2399/2303.Calculate Amount Paid in Taxes/Solution.cpp index 044d800861437..06638d811394a 100644 --- a/solution/2300-2399/2303.Calculate Amount Paid in Taxes/Solution.cpp +++ b/solution/2300-2399/2303.Calculate Amount Paid in Taxes/Solution.cpp @@ -3,8 +3,7 @@ class Solution { double calculateTax(vector>& brackets, int income) { double ans = 0; int idx = 0, prev = 0; - while (income) - { + while (income) { int a = brackets[idx][0], b = brackets[idx][1]; int d = a - prev; ans += min(d, income) * b / 100.0; diff --git a/solution/2300-2399/2304.Minimum Path Cost in a Grid/Solution.cpp b/solution/2300-2399/2304.Minimum Path Cost in a Grid/Solution.cpp index f2f51c60d4069..bbc84877d53f4 100644 --- a/solution/2300-2399/2304.Minimum Path Cost in a Grid/Solution.cpp +++ b/solution/2300-2399/2304.Minimum Path Cost in a Grid/Solution.cpp @@ -4,17 +4,13 @@ class Solution { int m = grid.size(), n = grid[0].size(); int inf = INT_MAX; vector f(n); - for (int i = 0; i < m; ++i) - { + for (int i = 0; i < m; ++i) { vector g(n); - for (int j = 0; j < n; ++j) - { + for (int j = 0; j < n; ++j) { g[j] = grid[i][j]; int t = inf; - if (i) - { - for (int k = 0; k < n; ++k) - { + if (i) { + for (int k = 0; k < n; ++k) { t = min(t, f[k] + moveCost[grid[i - 1][k]][j]); } } diff --git a/solution/2300-2399/2305.Fair Distribution of Cookies/Solution.cpp b/solution/2300-2399/2305.Fair Distribution of Cookies/Solution.cpp index f0f25535c780f..4e2e5533dd84e 100644 --- a/solution/2300-2399/2305.Fair Distribution of Cookies/Solution.cpp +++ b/solution/2300-2399/2305.Fair Distribution of Cookies/Solution.cpp @@ -15,15 +15,12 @@ class Solution { } void dfs(int u) { - if (u == cookies.size()) - { + if (u == cookies.size()) { ans = min(ans, *max_element(cnt.begin(), cnt.end())); return; } - for (int i = 0; i < k; ++i) - { - if (cnt[i] + cookies[u] < ans) - { + for (int i = 0; i < k; ++i) { + if (cnt[i] + cookies[u] < ans) { cnt[i] += cookies[u]; dfs(u + 1); cnt[i] -= cookies[u]; diff --git a/solution/2300-2399/2306.Naming a Company/Solution.cpp b/solution/2300-2399/2306.Naming a Company/Solution.cpp index 4e6f263d8b219..4265b41df3cbf 100644 --- a/solution/2300-2399/2306.Naming a Company/Solution.cpp +++ b/solution/2300-2399/2306.Naming a Company/Solution.cpp @@ -3,27 +3,21 @@ class Solution { long long distinctNames(vector& ideas) { unordered_set s(ideas.begin(), ideas.end()); vector> f(26, vector(26)); - for (auto v : ideas) - { + for (auto v : ideas) { int i = v[0] - 'a'; - for (int j = 0; j < 26; ++j) - { + for (int j = 0; j < 26; ++j) { v[0] = j + 'a'; - if (!s.count(v)) - { + if (!s.count(v)) { ++f[i][j]; } } } long long ans = 0; - for (auto v : ideas) - { + for (auto v : ideas) { int i = v[0] - 'a'; - for (int j = 0; j < 26; ++j) - { + for (int j = 0; j < 26; ++j) { v[0] = j + 'a'; - if (!s.count(v)) - { + if (!s.count(v)) { ans += f[j][i]; } } diff --git a/solution/2300-2399/2310.Sum of Numbers With Units Digit K/Solution.cpp b/solution/2300-2399/2310.Sum of Numbers With Units Digit K/Solution.cpp index 52176d6bbbcb7..acb0c02e42c84 100644 --- a/solution/2300-2399/2310.Sum of Numbers With Units Digit K/Solution.cpp +++ b/solution/2300-2399/2310.Sum of Numbers With Units Digit K/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int minimumNumbers(int num, int k) { if (num == 0) return 0; - for (int i = 1; i <= num; ++i) - { + for (int i = 1; i <= num; ++i) { int t = num - k * i; if (t >= 0 && t % 10 == 0) return i; } diff --git a/solution/2300-2399/2311.Longest Binary Subsequence Less Than or Equal to K/Solution.cpp b/solution/2300-2399/2311.Longest Binary Subsequence Less Than or Equal to K/Solution.cpp index 027ee7bd2bfa3..a8ab5a0307c35 100644 --- a/solution/2300-2399/2311.Longest Binary Subsequence Less Than or Equal to K/Solution.cpp +++ b/solution/2300-2399/2311.Longest Binary Subsequence Less Than or Equal to K/Solution.cpp @@ -3,11 +3,10 @@ class Solution { int longestSubsequence(string s, int k) { int ans = 0; long long v = 0; - for (int i = s.size() - 1; ~i; --i) - { - if (s[i] == '0') ++ans; - else if (ans < 32 && v + (1ll << ans) <= k) - { + for (int i = s.size() - 1; ~i; --i) { + if (s[i] == '0') + ++ans; + else if (ans < 32 && v + (1ll << ans) <= k) { v += 1ll << ans; ++ans; } diff --git a/solution/2300-2399/2312.Selling Pieces of Wood/Solution.cpp b/solution/2300-2399/2312.Selling Pieces of Wood/Solution.cpp index c8fb8952c4c78..b1e43cc0a5e20 100644 --- a/solution/2300-2399/2312.Selling Pieces of Wood/Solution.cpp +++ b/solution/2300-2399/2312.Selling Pieces of Wood/Solution.cpp @@ -6,7 +6,7 @@ class Solution { vector> memo(m + 1, vector(n + 1, -1)); vector> d(m + 1, vector(n + 1)); for (auto& p : prices) d[p[0]][p[1]] = p[2]; - return dfs(m, n, d, memo); + return dfs(m, n, d, memo); } ll dfs(int m, int n, vector>& d, vector>& memo) { diff --git a/solution/2300-2399/2315.Count Asterisks/Solution.cpp b/solution/2300-2399/2315.Count Asterisks/Solution.cpp index efd3bfad0ee4c..a7bca3c092f68 100644 --- a/solution/2300-2399/2315.Count Asterisks/Solution.cpp +++ b/solution/2300-2399/2315.Count Asterisks/Solution.cpp @@ -2,10 +2,11 @@ class Solution { public: int countAsterisks(string s) { int ans = 0, t = 0; - for (char& c : s) - { - if (c == '|') t ^= 1; - else if (c == '*') ans += t == 0; + for (char& c : s) { + if (c == '|') + t ^= 1; + else if (c == '*') + ans += t == 0; } return ans; } diff --git a/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.cpp b/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.cpp index f892ab923547e..18eab584e0b82 100644 --- a/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.cpp +++ b/solution/2300-2399/2316.Count Unreachable Pairs of Nodes in an Undirected Graph/Solution.cpp @@ -6,18 +6,17 @@ class Solution { long long countPairs(int n, vector>& edges) { vis.resize(n); g.resize(n, vector()); - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; g[a].push_back(b); g[b].push_back(a); } vector arr; - for (int i = 0; i < n; ++i) if (!vis[i]) arr.push_back(dfs(i)); + for (int i = 0; i < n; ++i) + if (!vis[i]) arr.push_back(dfs(i)); long long ans = 0; int t = 0; - for (int& v : arr) - { + for (int& v : arr) { t += v; ans += 1ll * v * (n - t); } @@ -27,7 +26,8 @@ class Solution { int dfs(int i) { int res = 1; vis[i] = true; - for (int j : g[i]) if (!vis[j]) res += dfs(j); + for (int j : g[i]) + if (!vis[j]) res += dfs(j); return res; } }; \ No newline at end of file diff --git a/solution/2300-2399/2319.Check if Matrix Is X-Matrix/Solution.cpp b/solution/2300-2399/2319.Check if Matrix Is X-Matrix/Solution.cpp index 5fa68193b57c5..14e43e13e88d2 100644 --- a/solution/2300-2399/2319.Check if Matrix Is X-Matrix/Solution.cpp +++ b/solution/2300-2399/2319.Check if Matrix Is X-Matrix/Solution.cpp @@ -2,15 +2,12 @@ class Solution { public: bool checkXMatrix(vector>& grid) { int n = grid.size(); - for (int i = 0; i < n; ++i) - { - for (int j = 0; j < n; ++j) - { - if (i == j || i == n - j - 1) - { + for (int i = 0; i < n; ++i) { + for (int j = 0; j < n; ++j) { + if (i == j || i == n - j - 1) { if (grid[i][j] == 0) return false; - } - else if (grid[i][j]) return false; + } else if (grid[i][j]) + return false; } } return true; diff --git a/solution/2300-2399/2320.Count Number of Ways to Place Houses/Solution.cpp b/solution/2300-2399/2320.Count Number of Ways to Place Houses/Solution.cpp index 2ab0b2727f9a2..59c3e28f64b15 100644 --- a/solution/2300-2399/2320.Count Number of Ways to Place Houses/Solution.cpp +++ b/solution/2300-2399/2320.Count Number of Ways to Place Houses/Solution.cpp @@ -4,12 +4,11 @@ class Solution { int mod = 1e9 + 7; vector> f(n, vector(2)); f[0] = {1, 1}; - for (int i = 1; i < n; ++i) - { + for (int i = 1; i < n; ++i) { f[i][0] = (f[i - 1][0] + f[i - 1][1]) % mod; f[i][1] = f[i - 1][0]; } long s = f[n - 1][0] + f[n - 1][1]; - return (int) ((s * s) % mod); + return (int)((s * s) % mod); } }; \ No newline at end of file diff --git a/solution/2300-2399/2321.Maximum Score Of Spliced Array/Solution.cpp b/solution/2300-2399/2321.Maximum Score Of Spliced Array/Solution.cpp index 89b6a691b60a2..d33e5d9b28fdc 100644 --- a/solution/2300-2399/2321.Maximum Score Of Spliced Array/Solution.cpp +++ b/solution/2300-2399/2321.Maximum Score Of Spliced Array/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int maximumsSplicedArray(vector& nums1, vector& nums2) { int s1 = 0, s2 = 0, n = nums1.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { s1 += nums1[i]; s2 += nums2[i]; } @@ -13,11 +12,12 @@ class Solution { int f(vector& nums1, vector& nums2) { int t = nums1[0] - nums2[0]; int mx = t; - for (int i = 1; i < nums1.size(); ++i) - { + for (int i = 1; i < nums1.size(); ++i) { int v = nums1[i] - nums2[i]; - if (t > 0) t += v; - else t = v; + if (t > 0) + t += v; + else + t = v; mx = max(mx, t); } return mx; diff --git a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cpp b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cpp index 50b59c24f3c96..f802fca15468a 100644 --- a/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cpp +++ b/solution/2300-2399/2322.Minimum Score After Removals on a Tree/Solution.cpp @@ -10,18 +10,15 @@ class Solution { int minimumScore(vector& nums, vector>& edges) { n = nums.size(); g.resize(n, vector()); - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; g[a].push_back(b); g[b].push_back(a); } for (int& v : nums) s ^= v; this->nums = nums; - for (int i = 0; i < n; ++i) - { - for (int j : g[i]) - { + for (int i = 0; i < n; ++i) { + for (int j : g[i]) { s1 = dfs(i, -1, j); dfs2(i, -1, j); } @@ -31,21 +28,22 @@ class Solution { int dfs(int i, int fa, int x) { int res = nums[i]; - for (int j : g[i]) if (j != fa && j != x) res ^= dfs(j, i, x); + for (int j : g[i]) + if (j != fa && j != x) res ^= dfs(j, i, x); return res; } int dfs2(int i, int fa, int x) { int res = nums[i]; - for (int j : g[i]) if (j != fa && j != x) - { - int a = dfs2(j, i, x); - res ^= a; - int b = s1 ^ a; - int c = s ^ s1; - int t = max(max(a, b), c) - min(min(a, b), c); - ans = min(ans, t); - } + for (int j : g[i]) + if (j != fa && j != x) { + int a = dfs2(j, i, x); + res ^= a; + int b = s1 ^ a; + int c = s ^ s1; + int t = max(max(a, b), c) - min(min(a, b), c); + ans = min(ans, t); + } return res; } }; \ No newline at end of file diff --git a/solution/2300-2399/2325.Decode the Message/Solution.cpp b/solution/2300-2399/2325.Decode the Message/Solution.cpp index 511b34bfa3b03..0851d25861562 100644 --- a/solution/2300-2399/2325.Decode the Message/Solution.cpp +++ b/solution/2300-2399/2325.Decode the Message/Solution.cpp @@ -5,8 +5,7 @@ class Solution { d[' '] = ' '; int i = 0; string lowcase = "abcdefghijklmnopqrstuvwxyz"; - for (char c : key) - { + for (char c : key) { if (d.count(c)) continue; d[c] = lowcase[i]++; } diff --git a/solution/2300-2399/2326.Spiral Matrix IV/Solution.cpp b/solution/2300-2399/2326.Spiral Matrix IV/Solution.cpp index 0e246ad177ed5..1ae3cd31736c0 100644 --- a/solution/2300-2399/2326.Spiral Matrix IV/Solution.cpp +++ b/solution/2300-2399/2326.Spiral Matrix IV/Solution.cpp @@ -14,17 +14,15 @@ class Solution { vector> ans(m, vector(n, -1)); int i = 0, j = 0, p = 0; vector> dirs = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; - while (1) - { + while (1) { ans[i][j] = head->val; head = head->next; if (!head) break; - while (1) - { + while (1) { int x = i + dirs[p][0], y = j + dirs[p][1]; - if (x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0) p = (p + 1) % 4; - else - { + if (x < 0 || y < 0 || x >= m || y >= n || ans[x][y] >= 0) + p = (p + 1) % 4; + else { i = x, j = y; break; } diff --git a/solution/2300-2399/2327.Number of People Aware of a Secret/Solution.cpp b/solution/2300-2399/2327.Number of People Aware of a Secret/Solution.cpp index 26c209fb60804..57e77afa5b2b6 100644 --- a/solution/2300-2399/2327.Number of People Aware of a Secret/Solution.cpp +++ b/solution/2300-2399/2327.Number of People Aware of a Secret/Solution.cpp @@ -8,14 +8,12 @@ class Solution { vector d(m); vector cnt(m); cnt[1] = 1; - for (int i = 1; i <= n; ++i) - { + for (int i = 1; i <= n; ++i) { if (!cnt[i]) continue; d[i] = (d[i] + cnt[i]) % mod; d[i + forget] = (d[i + forget] - cnt[i] + mod) % mod; int nxt = i + delay; - while (nxt < i + forget) - { + while (nxt < i + forget) { cnt[nxt] = (cnt[nxt] + cnt[i]) % mod; ++nxt; } diff --git a/solution/2300-2399/2328.Number of Increasing Paths in a Grid/Solution.cpp b/solution/2300-2399/2328.Number of Increasing Paths in a Grid/Solution.cpp index cdb53dd399a2a..760aedf396e29 100644 --- a/solution/2300-2399/2328.Number of Increasing Paths in a Grid/Solution.cpp +++ b/solution/2300-2399/2328.Number of Increasing Paths in a Grid/Solution.cpp @@ -14,8 +14,7 @@ class Solution { if (f[i][j]) return f[i][j]; int res = 1; vector dirs = {-1, 0, 1, 0, -1}; - for (int k = 0; k < 4; ++k) - { + for (int k = 0; k < 4; ++k) { int x = i + dirs[k], y = j + dirs[k + 1]; if (x >= 0 && x < g.size() && y >= 0 && y < g[0].size() && g[x][y] > g[i][j]) res = (res + dfs(x, y, f, g)) % mod; diff --git a/solution/2300-2399/2332.The Latest Time to Catch a Bus/Solution.cpp b/solution/2300-2399/2332.The Latest Time to Catch a Bus/Solution.cpp index 227daaf9b84bb..1948c6225787b 100644 --- a/solution/2300-2399/2332.The Latest Time to Catch a Bus/Solution.cpp +++ b/solution/2300-2399/2332.The Latest Time to Catch a Bus/Solution.cpp @@ -4,8 +4,7 @@ class Solution { sort(buses.begin(), buses.end()); sort(passengers.begin(), passengers.end()); int j = 0, c = 0; - for (int t : buses) - { + for (int t : buses) { c = capacity; while (c && j < passengers.size() && passengers[j] <= t) --c, ++j; } diff --git a/solution/2300-2399/2333.Minimum Sum of Squared Difference/Solution.cpp b/solution/2300-2399/2333.Minimum Sum of Squared Difference/Solution.cpp index 2da353918944d..b458e9b2f6472 100644 --- a/solution/2300-2399/2333.Minimum Sum of Squared Difference/Solution.cpp +++ b/solution/2300-2399/2333.Minimum Sum of Squared Difference/Solution.cpp @@ -8,31 +8,28 @@ class Solution { ll s = 0; int mx = 0; int k = k1 + k2; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { d[i] = abs(nums1[i] - nums2[i]); s += d[i]; mx = max(mx, d[i]); } if (s <= k) return 0; int left = 0, right = mx; - while (left < right) - { + while (left < right) { int mid = (left + right) >> 1; ll t = 0; for (int v : d) t += max(v - mid, 0); - if (t <= k) right = mid; - else left = mid + 1; + if (t <= k) + right = mid; + else + left = mid + 1; } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { k -= max(0, d[i] - left); d[i] = min(d[i], left); } - for (int i = 0; i < n && k; ++i) - { - if (d[i] == left) - { + for (int i = 0; i < n && k; ++i) { + if (d[i] == left) { --k; --d[i]; } diff --git a/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/Solution.cpp b/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/Solution.cpp index dddbad54856f7..965c1914abe79 100644 --- a/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/Solution.cpp +++ b/solution/2300-2399/2334.Subarray With Elements Greater Than Varying Threshold/Solution.cpp @@ -5,23 +5,20 @@ class Solution { vector left(n, -1); vector right(n, n); stack stk; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = nums[i]; while (!stk.empty() && nums[stk.top()] >= v) stk.pop(); if (!stk.empty()) left[i] = stk.top(); stk.push(i); } stk = stack(); - for (int i = n - 1; ~i; --i) - { + for (int i = n - 1; ~i; --i) { int v = nums[i]; while (!stk.empty() && nums[stk.top()] >= v) stk.pop(); if (!stk.empty()) right[i] = stk.top(); stk.push(i); } - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = nums[i]; int k = right[i] - left[i] - 1; if (v > threshold / k) return k; diff --git a/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.cpp b/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.cpp index 205751e804d58..f44bf38f0f8f3 100644 --- a/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.cpp +++ b/solution/2300-2399/2335.Minimum Amount of Time to Fill Cups/Solution.cpp @@ -2,8 +2,7 @@ class Solution { public: int fillCups(vector& amount) { int ans = 0; - while (amount[0] + amount[1] + amount[2]) - { + while (amount[0] + amount[1] + amount[2]) { sort(amount.begin(), amount.end()); ++ans; amount[2]--; diff --git a/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.cpp b/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.cpp index d1b3c8d9a1ef0..223f10d9ac6de 100644 --- a/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.cpp +++ b/solution/2300-2399/2336.Smallest Number in Infinite Set/Solution.cpp @@ -3,16 +3,16 @@ class SmallestInfiniteSet { unordered_set black; SmallestInfiniteSet() { - } - + int popSmallest() { int i = 1; - for (; black.count(i); ++i); + for (; black.count(i); ++i) + ; black.insert(i); return i; } - + void addBack(int num) { black.erase(num); } diff --git a/solution/2300-2399/2337.Move Pieces to Obtain a String/Solution.cpp b/solution/2300-2399/2337.Move Pieces to Obtain a String/Solution.cpp index e9fd54ca2403f..6fe1416f3644f 100644 --- a/solution/2300-2399/2337.Move Pieces to Obtain a String/Solution.cpp +++ b/solution/2300-2399/2337.Move Pieces to Obtain a String/Solution.cpp @@ -6,8 +6,7 @@ class Solution { auto a = f(start); auto b = f(target); if (a.size() != b.size()) return false; - for (int i = 0; i < a.size(); ++i) - { + for (int i = 0; i < a.size(); ++i) { auto x = a[i], y = b[i]; if (x.first != y.first) return false; if (x.first == 1 && x.second < y.second) return false; @@ -18,10 +17,11 @@ class Solution { vector> f(string s) { vector res; - for (int i = 0; i < s.size(); ++i) - { - if (s[i] == 'L') res.push_back({1, i}); - else if (s[i] == 'R') res.push_back({2, i}); + for (int i = 0; i < s.size(); ++i) { + if (s[i] == 'L') + res.push_back({1, i}); + else if (s[i] == 'R') + res.push_back({2, i}); } return res; } diff --git a/solution/2300-2399/2342.Max Sum of a Pair With Equal Sum of Digits/Solution.cpp b/solution/2300-2399/2342.Max Sum of a Pair With Equal Sum of Digits/Solution.cpp index 4ff51fa5aebe1..ea416f6944f23 100644 --- a/solution/2300-2399/2342.Max Sum of a Pair With Equal Sum of Digits/Solution.cpp +++ b/solution/2300-2399/2342.Max Sum of a Pair With Equal Sum of Digits/Solution.cpp @@ -2,23 +2,19 @@ class Solution { public: int maximumSum(vector& nums) { unordered_map> d; - for (int i = 0; i < nums.size(); ++i) - { + for (int i = 0; i < nums.size(); ++i) { int v = nums[i]; int t = 0; - while (v) - { + while (v) { t += v % 10; v /= 10; } d[t].push_back(nums[i]); } int ans = -1; - for (auto& [_, v] : d) - { + for (auto& [_, v] : d) { int n = v.size(); - if (n > 1) - { + if (n > 1) { sort(v.begin(), v.end()); ans = max(ans, v[n - 1] + v[n - 2]); } diff --git a/solution/2300-2399/2344.Minimum Deletions to Make Array Divisible/Solution.cpp b/solution/2300-2399/2344.Minimum Deletions to Make Array Divisible/Solution.cpp index 325af3e93c1a4..82388b7537b65 100644 --- a/solution/2300-2399/2344.Minimum Deletions to Make Array Divisible/Solution.cpp +++ b/solution/2300-2399/2344.Minimum Deletions to Make Array Divisible/Solution.cpp @@ -4,7 +4,8 @@ class Solution { int x = numsDivide[0]; for (int i = 1; i < numsDivide.size(); ++i) x = gcd(x, numsDivide[i]); sort(nums.begin(), nums.end()); - for (int i = 0; i < nums.size(); ++i) if (x % nums[i] == 0) return i; + for (int i = 0; i < nums.size(); ++i) + if (x % nums[i] == 0) return i; return -1; } diff --git a/solution/2300-2399/2347.Best Poker Hand/Solution.cpp b/solution/2300-2399/2347.Best Poker Hand/Solution.cpp index 58be42afddde0..664fd40fe2632 100644 --- a/solution/2300-2399/2347.Best Poker Hand/Solution.cpp +++ b/solution/2300-2399/2347.Best Poker Hand/Solution.cpp @@ -4,8 +4,10 @@ class Solution { unordered_set s(suits.begin(), suits.end()); if (s.size() == 1) return "Flush"; vector cnt(20); - for (int v : ranks) if (++cnt[v] >= 3) return "Three of a Kind"; - for (int v : cnt) if (v == 2) return "Pair"; + for (int v : ranks) + if (++cnt[v] >= 3) return "Three of a Kind"; + for (int v : cnt) + if (v == 2) return "Pair"; return "High Card"; } }; \ No newline at end of file diff --git a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp index 50cf8eb48a2fb..353827cb68fa0 100644 --- a/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp +++ b/solution/2300-2399/2348.Number of Zero-Filled Subarrays/Solution.cpp @@ -3,11 +3,10 @@ class Solution { long long zeroFilledSubarray(vector& nums) { long long ans = 0; int cnt = 0; - for (int v : nums) - { - if (v == 0) ++cnt; - else - { + for (int v : nums) { + if (v == 0) + ++cnt; + else { ans += 1ll * (1 + cnt) * cnt / 2; cnt = 0; } diff --git a/solution/2300-2399/2349.Design a Number Container System/Solution.cpp b/solution/2300-2399/2349.Design a Number Container System/Solution.cpp index abd95ad3f2b1b..812ebd94c9301 100644 --- a/solution/2300-2399/2349.Design a Number Container System/Solution.cpp +++ b/solution/2300-2399/2349.Design a Number Container System/Solution.cpp @@ -4,20 +4,18 @@ class NumberContainers { map> t; NumberContainers() { - } - + void change(int index, int number) { auto it = mp.find(index); - if (it != mp.end()) - { + if (it != mp.end()) { t[it->second].erase(index); it->second = number; - } - else mp[index] = number; + } else + mp[index] = number; t[number].insert(index); } - + int find(int number) { auto it = t.find(number); return it == t.end() || it->second.empty() ? -1 : *it->second.begin(); diff --git a/solution/2300-2399/2350.Shortest Impossible Sequence of Rolls/Solution.cpp b/solution/2300-2399/2350.Shortest Impossible Sequence of Rolls/Solution.cpp index 787d46fe85139..112db309138bf 100644 --- a/solution/2300-2399/2350.Shortest Impossible Sequence of Rolls/Solution.cpp +++ b/solution/2300-2399/2350.Shortest Impossible Sequence of Rolls/Solution.cpp @@ -3,11 +3,9 @@ class Solution { int shortestSequence(vector& rolls, int k) { unordered_set s; int ans = 1; - for (int v : rolls) - { + for (int v : rolls) { s.insert(v); - if (s.size() == k) - { + if (s.size() == k) { s.clear(); ++ans; } diff --git a/solution/2300-2399/2351.First Letter to Appear Twice/Solution.cpp b/solution/2300-2399/2351.First Letter to Appear Twice/Solution.cpp index b2b2442a6fd1d..03757fb84770e 100644 --- a/solution/2300-2399/2351.First Letter to Appear Twice/Solution.cpp +++ b/solution/2300-2399/2351.First Letter to Appear Twice/Solution.cpp @@ -2,7 +2,8 @@ class Solution { public: char repeatedCharacter(string s) { vector cnt(26); - for (char c : s) if (++cnt[c - 'a'] == 2) return c; + for (char c : s) + if (++cnt[c - 'a'] == 2) return c; return '.'; } }; \ No newline at end of file diff --git a/solution/2300-2399/2353.Design a Food Rating System/Solution.cpp b/solution/2300-2399/2353.Design a Food Rating System/Solution.cpp index c001525b53f21..44ed3a67f1240 100644 --- a/solution/2300-2399/2353.Design a Food Rating System/Solution.cpp +++ b/solution/2300-2399/2353.Design a Food Rating System/Solution.cpp @@ -7,22 +7,21 @@ class FoodRatings { public: FoodRatings(vector& foods, vector& cuisines, vector& ratings) { int n = foods.size(); - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { string a = foods[i], b = cuisines[i]; int c = ratings[i]; mp[a] = pis(c, b); t[b].insert(pis(-c, a)); } } - + void changeRating(string food, int newRating) { - pis &p = mp[food]; + pis& p = mp[food]; t[p.second].erase(pis(-p.first, food)); p.first = newRating; t[p.second].insert(pis(-p.first, food)); } - + string highestRated(string cuisine) { return t[cuisine].begin()->second; } diff --git a/solution/2300-2399/2354.Number of Excellent Pairs/Solution.cpp b/solution/2300-2399/2354.Number of Excellent Pairs/Solution.cpp index 626417d46503c..dc3cac589db69 100644 --- a/solution/2300-2399/2354.Number of Excellent Pairs/Solution.cpp +++ b/solution/2300-2399/2354.Number of Excellent Pairs/Solution.cpp @@ -5,13 +5,10 @@ class Solution { vector cnt(32); for (int v : s) ++cnt[__builtin_popcount(v)]; long long ans = 0; - for (int v : s) - { + for (int v : s) { int t = __builtin_popcount(v); - for (int i = 0; i < 32; ++i) - { - if (t + i >= k) - { + for (int i = 0; i < 32; ++i) { + if (t + i >= k) { ans += cnt[i]; } } diff --git a/solution/2300-2399/2355.Maximum Number of Books You Can Take/Solution.cpp b/solution/2300-2399/2355.Maximum Number of Books You Can Take/Solution.cpp index a75cd8f5e1883..25ec263f18426 100644 --- a/solution/2300-2399/2355.Maximum Number of Books You Can Take/Solution.cpp +++ b/solution/2300-2399/2355.Maximum Number of Books You Can Take/Solution.cpp @@ -8,8 +8,7 @@ class Solution { for (int i = 0; i < n; ++i) nums[i] = books[i] - i; vector left(n, -1); stack stk; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { while (!stk.empty() && nums[stk.top()] >= nums[i]) stk.pop(); if (!stk.empty()) left[i] = stk.top(); stk.push(i); @@ -17,8 +16,7 @@ class Solution { vector dp(n); dp[0] = books[0]; ll ans = 0; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { int v = books[i]; int j = left[i]; int cnt = min(v, i - j); diff --git a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.cpp b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.cpp index fd81c0c972e52..ee436ad134476 100644 --- a/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.cpp +++ b/solution/2300-2399/2357.Make Array Zero by Subtracting Equal Amounts/Solution.cpp @@ -2,7 +2,8 @@ class Solution { public: int minimumOperations(vector& nums) { unordered_set s; - for (int v : nums) if (v) s.insert(v); + for (int v : nums) + if (v) s.insert(v); return s.size(); } }; \ No newline at end of file diff --git a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.cpp b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.cpp index 921fd0aa0dadd..2c408d2e4f574 100644 --- a/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.cpp +++ b/solution/2300-2399/2358.Maximum Number of Groups Entering a Competition/Solution.cpp @@ -5,12 +5,10 @@ class Solution { int ans = 1; vector prev = {1, grades[0]}; vector curr = {0, 0}; - for (int i = 1; i < grades.size(); ++i) - { + for (int i = 1; i < grades.size(); ++i) { curr[0]++; curr[1] += grades[i]; - if (prev[0] < curr[0] && prev[1] < curr[1]) - { + if (prev[0] < curr[0] && prev[1] < curr[1]) { prev = curr; curr = {0, 0}; ++ans; diff --git a/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.cpp b/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.cpp index f028bc23e9c8b..57206ee28fc7b 100644 --- a/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.cpp +++ b/solution/2300-2399/2360.Longest Cycle in a Graph/Solution.cpp @@ -4,23 +4,19 @@ class Solution { int n = edges.size(); vector vis(n); int ans = -1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i) { if (vis[i]) continue; int curr = i; vector cycle; - while (curr != -1 && !vis[curr]) - { + while (curr != -1 && !vis[curr]) { cycle.push_back(curr); vis[curr] = true; curr = edges[curr]; } if (curr == -1) continue; - for (int j = 0; j < cycle.size(); ++j) - { - if (cycle[j] == curr) - { - ans = max(ans, (int) cycle.size() - j); + for (int j = 0; j < cycle.size(); ++j) { + if (cycle[j] == curr) { + ans = max(ans, (int)cycle.size() - j); break; } } diff --git a/solution/2300-2399/2363.Merge Similar Items/Solution.cpp b/solution/2300-2399/2363.Merge Similar Items/Solution.cpp index 43b00bc0b1447..34b66fad0ec47 100644 --- a/solution/2300-2399/2363.Merge Similar Items/Solution.cpp +++ b/solution/2300-2399/2363.Merge Similar Items/Solution.cpp @@ -5,7 +5,8 @@ class Solution { for (auto& e : items1) cnt[e[0]] += e[1]; for (auto& e : items2) cnt[e[0]] += e[1]; vector> ans; - for (int i = 0; i < cnt.size(); ++i) if (cnt[i]) ans.push_back({i, cnt[i]}); + for (int i = 0; i < cnt.size(); ++i) + if (cnt[i]) ans.push_back({i, cnt[i]}); return ans; } }; \ No newline at end of file diff --git a/solution/2300-2399/2365.Task Scheduler II/Solution.cpp b/solution/2300-2399/2365.Task Scheduler II/Solution.cpp index 66af4cf5fd32b..7c4aaaa283385 100644 --- a/solution/2300-2399/2365.Task Scheduler II/Solution.cpp +++ b/solution/2300-2399/2365.Task Scheduler II/Solution.cpp @@ -3,8 +3,7 @@ class Solution { long long taskSchedulerII(vector& tasks, int space) { unordered_map mp; long long ans = 0; - for (int v : tasks) - { + for (int v : tasks) { ++ans; if (mp.count(v)) ans = max(ans, mp[v]); mp[v] = ans + space + 1; diff --git a/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.cpp b/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.cpp index db189557b5eb4..67c6fc0df58d3 100644 --- a/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.cpp +++ b/solution/2300-2399/2366.Minimum Replacements to Sort the Array/Solution.cpp @@ -4,11 +4,9 @@ class Solution { long long ans = 0; int n = nums.size(); int mi = nums[n - 1]; - for (int i = n - 2; ~i; --i) - { + for (int i = n - 2; ~i; --i) { int v = nums[i]; - if (v <= mi) - { + if (v <= mi) { mi = v; continue; } diff --git a/solution/2300-2399/2368.Reachable Nodes With Restrictions/Solution.cpp b/solution/2300-2399/2368.Reachable Nodes With Restrictions/Solution.cpp index e24d5c681f315..d584f28fa9dcc 100644 --- a/solution/2300-2399/2368.Reachable Nodes With Restrictions/Solution.cpp +++ b/solution/2300-2399/2368.Reachable Nodes With Restrictions/Solution.cpp @@ -1,11 +1,10 @@ class Solution { public: int ans; - + int reachableNodes(int n, vector>& edges, vector& restricted) { vector> g(n); - for (auto& e : edges) - { + for (auto& e : edges) { int a = e[0], b = e[1]; g[a].push_back(b); g[b].push_back(a); @@ -16,7 +15,7 @@ class Solution { dfs(0, g, vis); return ans; } - + void dfs(int u, vector>& g, vector& vis) { if (vis[u]) return; vis[u] = true;