leetcode(c++)(分治)
#include <iostream> #include <vector> using namespace std; struct Node{ int val = 0; Node* next = nullptr; Node(int val_):val(val_){} }; int conquer(const vector<int>& nums,int target,int left,int right) { int cnt = 0; for(int i = left; i <= right; ++i) { if(nums[i] == target) { ++cnt; } } return cnt; } int divide(const vector<int>& nums,int left ,int right) { if(left == right)return nums[left]; int mid = left + (right - left) / 2; int leftRes = divide(nums,left,mid); int rightRes = divide(nums,mid+1,right); if(leftRes == rightRes)return leftRes; int leftCnt = conquer(nums,leftRes,left,right); int rightCnt = conquer(nums,rightRes,left,right); return leftCnt > rightCnt?leftRes : rightRes; } int majorityNums(const vector<int>& nums) { return divide(nums,0,nums.size() - 1); } double pow(int x,int n) { if(0 == n)return 1; double y = pow(x,n / 2); if(0 == n % 2)return y * y; return y * y * x; } double MyPow(int x, int n) { if(x == 0 || x == 1)return x; if(n < 0)return 1 / pow(x,-n); return pow(x,n); } void swap(vector<int>& nums,int i,int j) { int tmp = nums[i]; nums[i] = nums[j]; nums[j] = tmp; return; } int conquer(vector<int>&nums,int left,int right) { int pivot = nums[right],wall = left; for(int i = left ; i < right; ++i) { if(nums[i] < pivot) { swap(nums,i,wall); ++wall; } } swap(nums,wall,right); return wall; } void divide(vector<int>& nums,int left,int right,int k) { if(left >= right)return; int pos = conquer(nums,left,right); if(pos == nums.size() - k)return; else if(pos < nums.size() - k)divide(nums,pos + 1,right,k); else divide(nums,left,pos-1,k); } int findKthArray(vector<int>& nums,int k) { divide(nums,0,nums.size()-1,k); return nums[nums.size() - k]; } vector<Node*>generateLists(const vector<vector<int>>&lists) { int n = lists.size(); vector<Node*>res(n); for(int i = 0; i < n; ++i) { Node* node = new Node(lists[i][0]); Node* dummy = new Node(0); dummy->next = node; for(int j = 1; j < lists[i].size(); ++j) { node->next = new Node(lists[i][j]); node = node->next; } res[i] = dummy->next; } return res; } void print(const vector<Node*>& lists) { for(int i = 0; i < lists.size(); ++i) { Node* node = lists[i]; while(node != nullptr) { cout << node->val << " "; node = node->next; } cout << endl; } } Node* merge(Node* l1, Node* l2) { if(nullptr == l1) return l2; if(nullptr == l2) return l1; if(l1->val < l2->val) { l1->next = merge(l1->next,l2); return l1; } else { l2->next = merge(l1,l2->next); return l2; } } Node* partition(vector<Node*>& lists,int start, int end) { if(start == end)return lists[start]; if(start < end) { int mid = start + (end - start) / 2; Node* l1 = partition(lists,start,mid); Node* l2 = partition(lists,mid + 1, end); return merge(l1,l2); } return nullptr; } Node* mergeKList(vector<Node*>& lists) { return partition(lists,0,lists.size() - 1); } void print(Node* node) { while(nullptr != node) { cout << node->val << " "; node = node->next; } cout << endl; } int main() { //LeetCode169 vector<int>nums{3,2,3,2,3}; cout << majorityNums(nums) << endl; //LeetCode50 cout << MyPow(2,10) << endl; //LeetCode215 nums = {3,2,1,5,6,4}; int k = 2; cout << findKthArray(nums,k) << endl; //LeetCode23 vector<vector<int>>lists{{1,4,5},{1,3,4},{2,6}}; auto ls = generateLists(lists); Node* node = mergeKList(ls); print(node); return 0; }