【leetcode】2.5
阿新 • • 發佈:2021-02-06
技術標籤:ACM演算法
215. 陣列中的第K個最大元素
https://leetcode-cn.com/problems/kth-largest-element-in-an-array/
class Solution {
public:
int ans;
void qsort(vector<int>& a, int left, int right,int k){
if(left<=right){
int low = left;
int high = right;
int q = a[low];
while(low<high){
while(a[high] >= q &&low < high) high--;
a[low]= a[high];
while(a[low] <= q && low < high) low++;
a[high]= a[low];
}
a[low] = q;
if(low == k){
ans = q;
return ;
}
qsort(a, left, low-1,k);
qsort(a, low+1, right,k);
}
}
int findKthLargest(vector<int>& nums, int k) {
//cout<<nums.size()-k<<endl;
qsort(nums, 0, nums.size()-1,nums.size()-k);
return ans;
}
};
103. 二叉樹的鋸齒形層序遍歷
https://leetcode-cn.com/problems/binary-tree-zigzag-level-order-traversal/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> ans;
deque<int> anss;
if(root == NULL) return ans;
queue<TreeNode* > q;
bool s=0;
q.push(root);
while(!q.empty()){
int size= q.size();
for(int i=0;i<size;i++){
root = q.front();
q.pop();
if(s)
anss.push_back(root->val);
else
anss.push_front(root->val);
if(root->right != NULL){
q.push(root->right);
}
if(root->left != NULL){
q.push(root->left);
}
}
ans.emplace_back((vector<int>){anss.begin(), anss.end()});
anss.clear();
s = !s;
}
return ans;
}
};
160. 相交連結串列
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
stack<ListNode *> h1;
stack<ListNode *> h2;
ListNode *ans=NULL;
while(headA != NULL){
h1.push(headA);
headA=headA->next;
}
while(headB != NULL){
h2.push(headB);
headB=headB->next;
}
while(!h1.empty()&&!h2.empty()&&h1.top() == h2.top()) {
ans=h1.top();
if(!h1.empty()) h1.pop();
if(!h2.empty()) h2.pop();
}
return ans;
}
};
25. K 個一組翻轉連結串列
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
void bing(ListNode *begin, ListNode *end, ListNode *&kbegin, ListNode *&kend){
ListNode *l=begin;
ListNode *r=end;
stack<ListNode *> st;
while(l!=r){
st.push(l);
l=l->next;
}
st.push(r);
kbegin=st.top();
kend=kbegin;
st.pop();
while(!st.empty()){
cout<<"bb"<<st.top()->val<<endl;
kend->next = st.top();
kend = st.top();
st.pop();
}
}
ListNode* reverseKGroup(ListNode* head, int k) {
ListNode* ans=new ListNode ;
ans->next = head;
ListNode *qf=ans;
ListNode *q =ans->next;
ListNode *kbegin , *kend ;
ListNode *anss = qf;
while(1){
ListNode *begin = q;
for(int i=0;i<k;i++){
qf=q;
if(q==NULL) return ans->next;
else q = q -> next;
}
bing(begin, qf, kbegin, kend);
cout<<"anss "<<anss->val<<endl;
anss->next = kbegin;
kend -> next = q;
anss = kend;
}
return ans->next;
}
};
210. 課程表 II
class Solution {
public:
struct Edge{
int v;
int next;
}edge[5000];
int head[5000];
int anss[5000];
int cnt=0;
void add(int u,int v){
edge[cnt].v = v;
edge[cnt].next = head[u];
head[u]=cnt++;
}
void init(){
memset(head,-1,sizeof head);
cnt=0;
}
vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
init();
vector<int> as;
int n=prerequisites.size();
for(int i=0;i<n;i++){
anss[prerequisites[i][0]]++;
for(int j=1;j<prerequisites[i].size();j++){
add(prerequisites[i][j], prerequisites[i][0]);
}
}
queue<int> q;
for(int i=0;i<numCourses;i++){
if(!anss[i]){
q.push(i);
as.push_back(i);
}
}
while(!q.empty()){
int p=q.front();
q.pop();
for(int j=head[p];~j;j=edge[j].next){
anss[edge[j].v]--;
if(!anss[edge[j].v]){
anss[edge[j].v]--;
as.push_back(edge[j].v);
q.push(edge[j].v);
}
}
}
if(as.size()==numCourses) return as;
as.clear();
return as;
}
};