From e60c11bb5cba7e97c66d468aa1d68cc7ac538dea Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sat, 23 Dec 2023 11:57:10 +0000 Subject: [PATCH 01/12] =?UTF-8?q?add=202209040079/chapter9/lc=E7=AC=AC?= =?UTF-8?q?=E4=B8=89=E5=A4=A7=E7=9A=84=E6=95=B0.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...1\345\244\247\347\232\204\346\225\260.cpp" | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 "2209040079/chapter9/lc\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" diff --git "a/2209040079/chapter9/lc\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" "b/2209040079/chapter9/lc\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" new file mode 100644 index 00000000..0cc67f26 --- /dev/null +++ "b/2209040079/chapter9/lc\347\254\254\344\270\211\345\244\247\347\232\204\346\225\260.cpp" @@ -0,0 +1,33 @@ +class Solution { +public: + int thirdMax(vector& nums) { +int i,len = nums.size(),a = nums[0], b = INT_MIN, c = INT_MIN; + if(len == 1) return nums[0]; + if(len == 2) return max(nums[0], nums[1]); + bool flag = false; + for(i=0; i a){ + if(b!= INT_MIN){ + c = b; + b = a; + a = nums[i]; + }else{ + b= a; + a = nums[i]; + } + }else if(nums[i] < a && nums[i] > b){ + if(b != INT_MIN){ + c = b; + b = nums[i]; + }else{ + b = nums[i]; + } + }else if(nums[i] < b && nums[i] > c){ + c= nums[i]; + } + } + if(b == c || (flag==false && c == INT_MIN)) return a; + return c; + } +}; \ No newline at end of file -- Gitee From efe7a8af230382f6d2ba28ee3409644920e03f7e Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sat, 23 Dec 2023 12:13:36 +0000 Subject: [PATCH 02/12] =?UTF-8?q?add=202209040079/chapter9/lc=E8=AE=BE?= =?UTF-8?q?=E8=AE=A1=E5=93=88=E5=B8=8C=E9=9B=86=E5=90=88.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...0\345\270\214\351\233\206\345\220\210.cpp" | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 "2209040079/chapter9/lc\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" diff --git "a/2209040079/chapter9/lc\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" "b/2209040079/chapter9/lc\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" new file mode 100644 index 00000000..0339dea8 --- /dev/null +++ "b/2209040079/chapter9/lc\350\256\276\350\256\241\345\223\210\345\270\214\351\233\206\345\220\210.cpp" @@ -0,0 +1,38 @@ +class MyHashSet { +public: + MyHashSet():data(N) { + + } + + void add(int key) { +int t=m(key); + for(auto i=data[t].begin();i!=data[t].end();i++){ + if((*i)==key) return; + } + data[t].push_back(key); + } + + void remove(int key) { +int t=m(key); + for(auto i=data[t].begin();i!=data[t].end();i++){ + if((*i)==key){ + data[t].erase(i); + return; + } + } + } + + bool contains(int key) { +int t=m(key); + for(auto i=data[t].begin();i!=data[t].end();i++){ + if((*i)==key) return true; + } + return false; + } + private: + vector> data; + static const int N=100010; + static int m(int key){ + return key%N; + } +}; \ No newline at end of file -- Gitee From d54d1063f43b4ca9cfccd95a8ab8a847a538effe Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sat, 23 Dec 2023 15:43:50 +0000 Subject: [PATCH 03/12] =?UTF-8?q?add=202209040079/chapter9/lc=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E4=B8=AD=E7=9A=84=E7=AC=ACK=E4=B8=AA=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E5=85=83=E7=B4=A0.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...\200\345\244\247\345\205\203\347\264\240.cpp" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "2209040079/chapter9/lc\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" diff --git "a/2209040079/chapter9/lc\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" "b/2209040079/chapter9/lc\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..0fc3841f --- /dev/null +++ "b/2209040079/chapter9/lc\346\225\260\347\273\204\344\270\255\347\232\204\347\254\254K\344\270\252\346\234\200\345\244\247\345\205\203\347\264\240.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + int findKthLargest(vector& nums, int k) { + int i,m=0; +vectora(20001,0); + for(int i:nums)a[i+10000]++; + for(i=20000;i>=0;i--){ + while(a[i]>0){ + a[i]--; + m++; + if(m==k)return i-10000; + } + } + return 0; + } +}; \ No newline at end of file -- Gitee From a89f3e6075e156a95c7bf96ff6e63cce6e4aeaed Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sat, 23 Dec 2023 16:17:17 +0000 Subject: [PATCH 04/12] =?UTF-8?q?add=202209040079/chapter9/lc=20O(1)=20?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=8F=92=E5=85=A5=E3=80=81=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E5=92=8C=E8=8E=B7=E5=8F=96=E9=9A=8F=E6=9C=BA=E5=85=83=E7=B4=A0?= =?UTF-8?q?.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...7\346\234\272\345\205\203\347\264\240.cpp" | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 "2209040079/chapter9/lc O(1) \346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" diff --git "a/2209040079/chapter9/lc O(1) \346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" "b/2209040079/chapter9/lc O(1) \346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..0ee97561 --- /dev/null +++ "b/2209040079/chapter9/lc O(1) \346\227\266\351\227\264\346\217\222\345\205\245\343\200\201\345\210\240\351\231\244\345\222\214\350\216\267\345\217\226\351\232\217\346\234\272\345\205\203\347\264\240.cpp" @@ -0,0 +1,35 @@ +class RandomizedSet { +public: + vector n; + unordered_map m; + RandomizedSet() { + + } + + bool insert(int val) { + if (m.find(val) != m.end()) { + return false; + } + m[val] =n.size(); + n.push_back(val); + return true; + } + + bool remove(int val) { + if (m.find(val) == m.end()) { + return false; + } + int index = m[val]; + int last = n[n.size() - 1]; + m[last] = index; + m.erase(val); + n[index] = last; + n.pop_back(); + return true; + } + + int getRandom() { + int idx = rand() % (int) n.size(); + return n[idx]; + } +}; \ No newline at end of file -- Gitee From cc90a72b10ccb575489e16e405836a03efb1597f Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sat, 23 Dec 2023 16:18:03 +0000 Subject: [PATCH 05/12] =?UTF-8?q?=E6=96=B0=E5=BB=BA=20chapter10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2209040079/chapter10/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 2209040079/chapter10/.keep diff --git a/2209040079/chapter10/.keep b/2209040079/chapter10/.keep new file mode 100644 index 00000000..e69de29b -- Gitee From d0e06d59679289b818054a9217b864df89c229b9 Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sun, 24 Dec 2023 11:52:28 +0000 Subject: [PATCH 06/12] =?UTF-8?q?add=202209040079/chapter10/lc=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E6=8E=92=E5=88=97=E5=AD=97=E7=AC=A6=E4=B8=B2=E9=97=AE?= =?UTF-8?q?=E9=A2=98.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...\254\246\344\270\262\351\227\256\351\242\230.cpp" | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 "2209040079/chapter10/lc\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262\351\227\256\351\242\230.cpp" diff --git "a/2209040079/chapter10/lc\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262\351\227\256\351\242\230.cpp" "b/2209040079/chapter10/lc\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262\351\227\256\351\242\230.cpp" new file mode 100644 index 00000000..af7856f9 --- /dev/null +++ "b/2209040079/chapter10/lc\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262\351\227\256\351\242\230.cpp" @@ -0,0 +1,12 @@ +class Solution { +public: + string restoreString(string s, vector& indices) { + int i,l=indices.size(); + string t(l,0); + for(i=0;i Date: Sun, 24 Dec 2023 12:22:40 +0000 Subject: [PATCH 07/12] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2022?= =?UTF-8?q?09040079/chapter10/.keep?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2209040079/chapter10/.keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 2209040079/chapter10/.keep diff --git a/2209040079/chapter10/.keep b/2209040079/chapter10/.keep deleted file mode 100644 index e69de29b..00000000 -- Gitee From 23c86908ce6c3ffc1f1e66219f83095f7e7bfaa1 Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sun, 24 Dec 2023 12:23:08 +0000 Subject: [PATCH 08/12] =?UTF-8?q?add=202209040079/chapter10/lc=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E9=93=BE=E8=A1=A8.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...2\345\272\217\351\223\276\350\241\250.cpp" | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 "2209040079/chapter10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" diff --git "a/2209040079/chapter10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2209040079/chapter10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 00000000..c766bc84 --- /dev/null +++ "b/2209040079/chapter10/lc\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,40 @@ +class Solution { +public: + ListNode* sortList(ListNode* head) { + return sortList(head,nullptr); + } + ListNode* sortList(ListNode* head,ListNode* tail){ + if(head==nullptr) return head; + if(head->next==tail){ + head->next=nullptr; + return head; + } + ListNode* slow=head,*fast=head; + while(fast!=tail){ + slow=slow->next; + fast=fast->next; + if(fast!=tail){ + fast=fast->next; + } + } + ListNode* mid=slow; + return merge(sortList(head,slow),sortList(slow,tail)); + } + ListNode* merge(ListNode* l1,ListNode* l2){ + ListNode* result=new ListNode(-1); + ListNode* p=result; + while(l1&&l2){ + if(l1->valval){ + p->next=l1; + l1=l1->next; + }else { + p->next=l2; + l2=l2->next; + } + p=p->next; + } + if(l1) p->next=l1; + if(l2) p->next=l2; + return result->next; + } +}; \ No newline at end of file -- Gitee From f05c0d6af50715eb49f04f256688046e5c43be74 Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sun, 24 Dec 2023 12:43:31 +0000 Subject: [PATCH 09/12] =?UTF-8?q?add=202209040079/chapter10/lc=E6=8C=89?= =?UTF-8?q?=E5=A5=87=E5=81=B6=E6=8E=92=E5=BA=8F=E6=95=B0=E7=BB=84=E2=85=A1?= =?UTF-8?q?.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...7\346\225\260\347\273\204\342\205\241.cpp" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "2209040079/chapter10/lc\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" diff --git "a/2209040079/chapter10/lc\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" "b/2209040079/chapter10/lc\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" new file mode 100644 index 00000000..5174cbc4 --- /dev/null +++ "b/2209040079/chapter10/lc\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204\342\205\241.cpp" @@ -0,0 +1,24 @@ +class Solution { +public: + vector sortArrayByParityII(vector& nums) { +vectora; + vectorb; + vectorc; + int i,m=nums.size(); + for(i,i=0;i Date: Sun, 24 Dec 2023 13:24:21 +0000 Subject: [PATCH 10/12] =?UTF-8?q?add=202209040079/chapter10/lc=E6=9C=80?= =?UTF-8?q?=E6=8E=A5=E8=BF=91=E5=8E=9F=E7=82=B9=E7=9A=84K=E4=B8=AA?= =?UTF-8?q?=E7=82=B9.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...1\347\232\204K\344\270\252\347\202\271.cpp" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "2209040079/chapter10/lc\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.cpp" diff --git "a/2209040079/chapter10/lc\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.cpp" "b/2209040079/chapter10/lc\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.cpp" new file mode 100644 index 00000000..cb65b71d --- /dev/null +++ "b/2209040079/chapter10/lc\346\234\200\346\216\245\350\277\221\345\216\237\347\202\271\347\232\204K\344\270\252\347\202\271.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + vector> kClosest(vector>& points, int k) { + priority_queue,vector>,greater<>> m; + vector> r; + int i,sum; + for(i=0;i Date: Sun, 24 Dec 2023 14:25:55 +0000 Subject: [PATCH 11/12] =?UTF-8?q?add=202209040079/chapter10/lc=E6=9C=80?= =?UTF-8?q?=E5=B0=8F=E7=9A=84K=E4=B8=AA=E6=95=B0.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...7\347\232\204K\344\270\252\346\225\260.cpp" | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 "2209040079/chapter10/lc\346\234\200\345\260\217\347\232\204K\344\270\252\346\225\260.cpp" diff --git "a/2209040079/chapter10/lc\346\234\200\345\260\217\347\232\204K\344\270\252\346\225\260.cpp" "b/2209040079/chapter10/lc\346\234\200\345\260\217\347\232\204K\344\270\252\346\225\260.cpp" new file mode 100644 index 00000000..ff88b422 --- /dev/null +++ "b/2209040079/chapter10/lc\346\234\200\345\260\217\347\232\204K\344\270\252\346\225\260.cpp" @@ -0,0 +1,18 @@ +class Solution { +public: + vector smallestK(vector& arr, int k) { + vector m; + int i; + if (arr.empty() || k > arr.size()) + return m; + + make_heap(arr.begin(), arr.end(), greater()); + for (i = 0; i < k; ++i) + { + pop_heap(arr.begin(), arr.end(), greater()); + m.push_back(arr.back()); + arr.pop_back(); + } + return m; + } +}; \ No newline at end of file -- Gitee From 816a84fc504b223c4d42ac7028900a1fee337608 Mon Sep 17 00:00:00 2001 From: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> Date: Sun, 24 Dec 2023 14:41:08 +0000 Subject: [PATCH 12/12] =?UTF-8?q?add=202209040079/chapter10/lc=E5=89=8Dk?= =?UTF-8?q?=E4=B8=AA=E9=AB=98=E9=A2=91=E5=85=83=E7=B4=A0.cpp.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: lujian <13419817+want-to-eat-delicious-food@user.noreply.gitee.com> --- ...0\351\242\221\345\205\203\347\264\240.cpp" | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 "2209040079/chapter10/lc\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" diff --git "a/2209040079/chapter10/lc\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" "b/2209040079/chapter10/lc\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" new file mode 100644 index 00000000..833fc745 --- /dev/null +++ "b/2209040079/chapter10/lc\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" @@ -0,0 +1,24 @@ +class Solution { +public: + vector topKFrequent(vector& nums, int k) { + vector s(k); +unordered_map n; +int i; + for (int num : nums) { + n[num]++; + } + priority_queue, vector>, greater>> m; + for (auto& p : n) { + m.emplace(p.second, p.first); + if (m.size() > k) { + m.pop(); + } + } + for (i = k - 1; i >= 0; i--) { + s[i] = m.top().second; + m.pop(); + } + + return s; + } +}; \ No newline at end of file -- Gitee