diff --git "a/2209040042/chapter10/\345\211\215k\344\270\252\351\253\230\351\242\221\345\205\203\347\264\240.cpp" "b/2209040042/chapter10/\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..5190f498317a9315035c1ee2c7d660d8666c15f1 --- /dev/null +++ "b/2209040042/chapter10/\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/2209040042/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" "b/2209040042/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..faa9237f5ddec6d988abf36aaa1e2509629e75b9 --- /dev/null +++ "b/2209040042/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.cpp" @@ -0,0 +1,22 @@ +#include +void main() +{ + int n[10] = { 25,35,68,79,21,13,98,7,16,62 };//定义一个大小为10的数组 + int i, j, temp; + for (i = 1; i <= 9; i++)//外层循环是比较的轮数,数组内有10个数,那么就应该比较10-1=9轮 + { + for (j = 0; j <= 9 - i; j++)//内层循环比较的是当前一轮的比较次数,例如:第一轮比较9-1=8次,第二轮比较9-2=7次 + { + if (n[j] > n[j + 1])//相邻两个数如果逆序,则交换位置 + { + temp = n[j]; + n[j] = n[j + 1]; + n[j + 1] = temp; + } + } + } + printf("排序过后的数顺序:\n"); + for (i = 0; i < 10; i++) + printf("%-4d", n[i]); + printf("\n"); +} \ No newline at end of file diff --git "a/2209040042/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.cpp" "b/2209040042/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..95d9a44392311fb929f1b68835a072d018c01004 --- /dev/null +++ "b/2209040042/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.cpp" @@ -0,0 +1,33 @@ +#include +#include +#include +using namespace std; + +typedef struct student { + char name[20]; + int age, score; +}student; + +student s[105]; + +bool compare(student s1, student s2) { + if (s1.score != s2.score) + return s1.score > s2.score; + else if (strcmp(s1.name, s2.name) != 0) + return strcmp(s1.name, s2.name) < 0; + else + return s1.age < s2.age; +} + +int main() { + int n; + cin >> n; + for (int i = 0; i < n; ++i) { + cin >> s[i].name >> s[i].age >> s[i].score; + } + sort(s, s + n, compare); + for (int i = 0; i < n; ++i) { + cout << s[i].name << ' ' << s[i].age << ' ' << s[i].score << endl; + } + return 0; +} \ No newline at end of file diff --git "a/2209040042/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" "b/2209040042/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..65c070440badee36eb9be9bac0004fc0cd984261 --- /dev/null +++ "b/2209040042/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.cpp" @@ -0,0 +1,58 @@ +#include +#include + +using namespace std; + +void randData(int* a,int n) //产生n个0-99的随机数,用于创建随机数组用来作为排序前数列 +{ + srand(time(NULL)); + for(int i=0;i=end) return; + int i = start; + int j = end; + int key = a[i]; + while (i=key) + { + j--; + } + a[i] = a[j]; + while(i sortArrayByParityII(vector& nums) { + vector path(nums.size()); + int ou=0; + int ji=1; + for(int num:nums) + { + if(num%2==0){ + path[ou]=num; + ou+=2; + } + else { + path[ji]=num; + ji+=2; + } + } + return path; + } +}; \ No newline at end of file diff --git "a/2209040042/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" "b/2209040042/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..460e31c358a424899c1b951252904ad42a0e93cc --- /dev/null +++ "b/2209040042/chapter10/\346\216\222\345\272\217\351\223\276\350\241\250.cpp" @@ -0,0 +1,38 @@ +struct ListNode* sortList(struct ListNode* head) { + //排序板子 + struct ListNode *radix[10], *ptr[10]; + for(int i = 0; i < 10; ++i){ + radix[i] = (struct ListNode*)malloc(sizeof(struct ListNode)); + radix[i]->next = NULL; + ptr[i] = radix[i]; + } + //开始基数排序,cur为当前待排序节点 + struct ListNode* cur = head, *temp; + for(int bit = 1; bit <= 100000; bit *= 10){ + //插到板子上 + while(cur){ + temp = cur->next; + int px = ((cur->val+100000)/ bit) % 10; + ptr[px]->next = cur; + cur->next = NULL; + ptr[px] = ptr[px]->next; + cur = temp; + } + //收集 + struct ListNode *prev = ptr[0]; + for(int i = 1; i < 10; ++i){ + if(radix[i]->next){ + prev->next = radix[i]->next; + prev = ptr[i]; + } + } + //新的开始节点 + cur = radix[0]->next; + //恢复板子为空 + for(int i = 0; i < 10; ++i){ + radix[i]->next = NULL; + ptr[i] = radix[i]; + } + } + return cur; +} \ No newline at end of file diff --git "a/2209040042/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" "b/2209040042/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..47510eb05ce34966bba4d094e04ef182452703a2 --- /dev/null +++ "b/2209040042/chapter10/\346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260.cpp" @@ -0,0 +1,11 @@ +class Solution { +public: + vector smallestK(vector& arr, int k) { + sort(arr.begin(), arr.end()); + vector ans(k); + for (int i = 0; i < k; i ++) { + ans[i] = arr[i]; + } + return ans; + } +}; \ No newline at end of file diff --git "a/2209040042/chapter10/\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/2209040042/chapter10/\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..96ce7bf883fa1cc3af1ed60f76a5efeb8c6e307b --- /dev/null +++ "b/2209040042/chapter10/\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,30 @@ +class Solution { + +public: + // 大顶堆比较函数 + class mycomparison { + public: + bool operator()(const pair>& lhs, const pair>& rhs) { + return lhs.first < rhs.first; + } + }; + vector> kClosest(vector>& points, int K) { + // 定义一个大顶堆 + priority_queue>, vector>>, mycomparison> pri_que; + for(int i = 0; i < points.size(); i++) { + int x = points[i][0]; + int y = points[i][1]; + pair> p(x * x + y * y, points[i]); // key:距离,value是(x,y) + pri_que.push(p); + if (pri_que.size() > K) { // 如果队列的大小大于了K,则队列弹出,保证队列的大小一直为k + pri_que.pop(); + } + } + vector> result(K); // 把队列里元素放入数组 + for (int i = 0; i < K; i++) { + result[i] = pri_que.top().second; + pri_que.pop(); + } + return result; + } +}; \ No newline at end of file diff --git "a/2209040042/chapter10/\351\207\215\346\226\260\346\216\222\345\272\217\345\255\227\347\254\246\344\270\262.cpp" "b/2209040042/chapter10/\351\207\215\346\226\260\346\216\222\345\272\217\345\255\227\347\254\246\344\270\262.cpp" new file mode 100644 index 0000000000000000000000000000000000000000..7e6d27d6083eefd68f52750ec182715f7e2161e9 --- /dev/null +++ "b/2209040042/chapter10/\351\207\215\346\226\260\346\216\222\345\272\217\345\255\227\347\254\246\344\270\262.cpp" @@ -0,0 +1,15 @@ +class Solution { +public: + string restoreString(string s, vector& indices) { + int sz = s.size(); + for (int i = 0; i < sz; ++i) { + int j = i; + while (indices[j] > 0) { + swap(s[i], s[indices[j]]); + indices[j] = -indices[j]; + j = -indices[j]; + } + } + return s; + } +}; \ No newline at end of file