这周既然有contest 就做的这个
前5题如下:
遗憾的是,后两题都超时了
不过既然是模拟,也没有去找答案了
思路写在注释里:
//
// 1.cpp
// leecode
//
// Created by 陆炫宇 on 17/6/15.
// Copyright © 2017年 陆炫宇. All rights reserved.
//1.求和问题 2.排序问题 (1)超时 (2)好了 用回老思路 分分钟解决SB
//3.二叉树的遍历 4.有点儿像 子图数量 至于用什么算法 思路: (1) 遍历 直到没有1; 超时了 (2)去掉外层循环 用队列装 超时2 (3)第一遍N2-》a个1 第二遍内部循环复杂度分析4个方向探索 队列 不知道怎么分析时间复杂度
//5.有向图 判断为DAG 算法有拓扑排序? 找到一个入度为0的点 去除之后
//数据结构:点集合 n + 边集合 pair<a b> 那么算法来 计算 入度呢 有的: 边集合中 遍历: 但是结果会是:复杂度过高 (1) 拓扑算法 依旧超时
#include <stdio.h>
#include<vector>
#include<iostream>
#include<queue>
//#include<pair>
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
bool isDAG(int n,vector<pair<int,int>> a){
int mark=n;
while(!a.empty()){
int l=a.size();
//int *in=new int[n+1];
int in[n+1];
for(int j=0;j<n;j++)
in[j]=0;
in[mark]=-1;
vector<pair<int,int>>::iterator it=a.begin();
for (it=a.begin();it!=a.end();it++){
//cout<<(*it).second;
in[it->second]++;
}
mark=n;
int i=0;
for(i=0;i<n;i++){
if (in[i]==0){
mark=i;
in[i]=-1;
break;
}
}
if(mark==n)
return false;
for (it=a.begin();it!=a.end();){
if(it->first==mark){
a.erase(it);
continue;
}
it++;
}
}
return true;
}
};
//int main(){
// int n=2;
// vector<pair<int,int>> edg;
// edg.push_back(make_pair(0, 1));
// edg.push_back(make_pair(1, 0));
// Solution s;
// cout<<s.isDAG(n, edg);
//
//}
int countConnectedOnes(vector<vector<char>>& A) {
int h=A.size();
int w=A[0].size();
int i,j,count=0;
queue<pair<int,int>> q;
queue<pair<int,int>> qof1;
//cout<<A[0][0];
for(i=0;i<h;i++){
for(j=0;j<w;j++){
if(A[i][j]=='1'){
qof1.push(make_pair(i,j));
}
}
}
while(!qof1.empty()){
q.push(qof1.front());
bool flag=true;
int a=q.front().first;
int b=q.front().second;
if(A[a][b]=='0'){
flag=false;
qof1.pop();
q.pop();
continue;
}
while(!q.empty()){
int a=q.front().first;
int b=q.front().second;
A[a][b]='0';
q.pop();
//find1
if(a-1>=0&&A[a-1][b]=='1')
q.push(make_pair(a-1,b));
if(b-1>=0&&A[a][b-1]=='1')
q.push(make_pair(a,b-1));
if(a+1<h&&A[a+1][b]=='1')
q.push(make_pair(a+1,b));
if(b+1<w&&A[a][b+1]=='1')
q.push(make_pair(a,b+1));
}
qof1.pop();
if(flag)
count++;
}
return count;
}
bool isEqual(TreeNode* p, TreeNode* q) {
queue<TreeNode*> a;
queue<TreeNode*> b;
if(p==NULL&&q==NULL)
return true;
if(p==NULL&&q!=NULL)
return false;
if(p!=NULL&&q==NULL)
return false;
a.push(p);
b.push(q);
//a,b not empty
while(!a.empty()&&!b.empty()){
if(a.front()->val!=b.front()->val)
return false;
TreeNode* left1=a.front()->left,*right1=a.front()->right;
TreeNode* left2=b.front()->left,*right2=b.front()->right;
if(left1!=NULL&&left2==NULL)
return false;
if(left1==NULL&&left2!=NULL)
return false;
if(left1&&left2){
a.push(left1);
b.push(left2);
}
if(right1!=NULL&&right2==NULL)
return false;
if(right1==NULL&&right2!=NULL)
return false;
if(right1&&right2){
a.push(right1);
b.push(right2);
}
a.pop();
b.pop();
}
return true;
}
int F(int k, int n) {
if(k==0)
return n;
int sum=0;
for(int i=1;i<n+1;i++)
sum+=F(k-1,i);
return sum;
}
int assignConferenceRoom(vector<int>& a, vector<int>& b) {
int count=0;
sort(a.begin(), a.end(),less<int>());
sort(b.begin(), b.end(),less<int>());
vector<int>::iterator t1,t2;
t1=a.begin();
t2=b.begin();
while(t1!=a.end()&&t2!=b.end()){
if((*t1)<=(*t2)){
count++;
*t1++;
*t2++;
}
else
*t2++;
}
return count;
}
//
//int main(){
// Solution s;
// vector<vector<char> > A;
// vector<char> B;
// B.push_back('1');
// B.push_back('1');
// B.push_back(0);
// vector<char> C;
// C.push_back(0);
// C.push_back('1');
// C.push_back('1');
// vector<char> D;
// D.push_back('1');
// D.push_back('1');
// D.push_back('1');
// A.push_back(B);
//
// A.push_back(C);
// A.push_back(D);
// cout<<s.countConnectedOnes(A);
//}
//
//int main(){
// Solution s;
// TreeNode *a=new TreeNode(1);
// a->left=new TreeNode(2);
// a->right=new TreeNode(3);
// TreeNode *b=new TreeNode(1);
// //b->left=new TreeNode(2);
// b->right=new TreeNode(3);
// cout<<s.isEqual(a, b)<<endl;
//}
//
//
// int assignConferenceRoom(vector<int>& a, vector<int>& b) {
// int count=0;
// sort(a.begin(), a.end(),greater<int>());
// sort(b.begin(), b.end(),less<int>());
// int c=a.size();
// int d=b.size();
// bool aa[c];
// for(int k=0;k<c;k++)
// aa[k]=false;
// int i,j;
// // vector<int>::iterator j;
// for(i=0;i<d;i++){
// for(j=0;j<c;j++){
// //for (j=b.begin();j!=b.end();j++){
// if(aa[j])
// continue;
// if(b[i]>=(a[j])){
// count++;
// aa[j]=true;
// break;
// }
// }
// }
//
// return count;
// }