diff --git "a/2209040031/chap.10/lc148\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2209040031/chap.10/lc148\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..c99781141eab8e5142033b5e4db38da377f53b00 --- /dev/null +++ "b/2209040031/chap.10/lc148\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,47 @@ +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, mid), sortList(mid, tail)); + } + + ListNode* merge(ListNode* head1, ListNode* head2) { + ListNode* dummyHead = new ListNode(0); + ListNode* temp = dummyHead, *temp1 = head1, *temp2 = head2; + while (temp1 != nullptr && temp2 != nullptr) { + if (temp1->val <= temp2->val) { + temp->next = temp1; + temp1 = temp1->next; + } else { + temp->next = temp2; + temp2 = temp2->next; + } + temp = temp->next; + } + if (temp1 != nullptr) { + temp->next = temp1; + } else if (temp2 != nullptr) { + temp->next = temp2; + } + return dummyHead->next; + } +}; \ No newline at end of file diff --git "a/2209040031/chap.10/lc1528\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" "b/2209040031/chap.10/lc1528\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..314b7aa7e4ae4b4b66737895e286bb55365a76f8 --- /dev/null +++ "b/2209040031/chap.10/lc1528\351\207\215\346\226\260\346\216\222\345\210\227\345\255\227\347\254\246\344\270\262.cpp" @@ -0,0 +1,12 @@ +class Solution { +public: + string restoreString(string s, vector& indices) { + int length = s.length(); + string result(length, 0); + + for(int i = 0; i < length; i++) { + result[indices[i]] = s[i]; + } + return result; + } +}; \ No newline at end of file diff --git "a/2209040031/chap.10/lc347\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" "b/2209040031/chap.10/lc347\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..fe294eef0828499f5cc3f876732f39921abc7b49 --- /dev/null +++ "b/2209040031/chap.10/lc347\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" @@ -0,0 +1,32 @@ +class Solution { +public: + static bool cmp(pair& m, pair& n) { + return m.second > n.second; + } + + vector topKFrequent(vector& nums, int k) { + unordered_map occurrences; + for (auto& v : nums) { + occurrences[v]++; + } + + // pair 的第一个元素代表数组的值,第二个元素代表了该值出现的次数 + priority_queue, vector>, decltype(&cmp)> q(cmp); + for (auto& [num, count] : occurrences) { + if (q.size() == k) { + if (q.top().second < count) { + q.pop(); + q.emplace(num, count); + } + } else { + q.emplace(num, count); + } + } + vector ret; + while (!q.empty()) { + ret.emplace_back(q.top().first); + q.pop(); + } + return ret; + } +}; \ No newline at end of file diff --git "a/2209040031/chap.10/lc922\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.cpp" "b/2209040031/chap.10/lc922\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..8948ad3f9830097d603213dfbad04d0efded2e1a --- /dev/null +++ "b/2209040031/chap.10/lc922\346\214\211\345\245\207\345\201\266\346\216\222\345\272\217\346\225\260\347\273\204.cpp" @@ -0,0 +1,23 @@ +class Solution { +public: + vector sortArrayByParityII(vector& nums) { + int n = nums.size(); + vector ans(n); + + int i = 0; + for (int x: nums) { + if (x % 2 == 0) { + ans[i] = x; + i += 2; + } + } + i = 1; + for (int x: nums) { + if (x % 2 == 1) { + ans[i] = x; + i += 2; + } + } + return ans; + } +}; \ No newline at end of file diff --git "a/2209040031/chap.10/lc973\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/2209040031/chap.10/lc973\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..998a99972a8e0707ef9de17925f83c5c8160322f --- /dev/null +++ "b/2209040031/chap.10/lc973\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,9 @@ +class Solution { +public: + vector> kClosest(vector>& points, int k) { + sort(points.begin(), points.end(), [](const vector& u, const vector& v) { + return u[0] * u[0] + u[1] * u[1] < v[0] * v[0] + v[1] * v[1]; + }); + return {points.begin(), points.begin() + k}; + } +}; \ No newline at end of file