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 0000000000000000000000000000000000000000..833fc745285a89b4caff1d54bf1bb2d491d24a90 --- /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 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 0000000000000000000000000000000000000000..5174cbc4e1210ef9b7ba1e3b2e5d1c1fe0bdedf3 --- /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;inext==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 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 0000000000000000000000000000000000000000..ff88b4226eafbbdbc58123ee644617d8c146c90a --- /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 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 0000000000000000000000000000000000000000..cb65b71d282f1d2a1d54185c88fd68a229efed67 --- /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& indices) { + int i,l=indices.size(); + string t(l,0); + for(i=0;i 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 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 0000000000000000000000000000000000000000..0fc3841f8ecf092374cd06613d86fa2e9bc1d156 --- /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 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 0000000000000000000000000000000000000000..0cc67f26c1dcc07be43da6c5900a695afcf2207d --- /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 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 0000000000000000000000000000000000000000..0339dea8147c00b3d641fe80593289f9789ba65a --- /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