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