1000.函数求值:直接暴力模拟即可。
class Solution {
public:
int F(int k, int n) {
int ans[15][15] = {0};
int i,j,kk,cur;
for(i = 0;i <= n;i++)
ans[0][i] = i;
for(i = 1;i <= k;i++){
for(j = 1;j <= n;j++){
cur = 0;
for(kk = 1;kk <= j;kk++)
cur += ans[i - 1][kk];
ans[i][j] = cur;
}
}
return ans[k][n];
}
};
1001.会议安排:分别对A、B两个数组降序排序,然后对两个数组从头依次进行匹配,有点类似于贪心。
class Solution {
public:
int assignConferenceRoom(vector<int>& A, vector<int>& B) {
sort(A.begin(),A.end(),greater<int>());
sort(B.begin(),B.end(),greater<int>());
int ans = 0;
vector<int>::iterator itra = A.begin();
vector<int>::iterator itrb = B.begin();
while (itra != A.end() && itrb != B.end()) {
if(*itra <= *itrb){
ans ++;
itrb ++;
}
itra ++;
}
return ans;
}
};
1002.等价二叉树:递归判断。
class Solution {
public:
bool isEqual(TreeNode* p, TreeNode* q) {
if(p == NULL && q == NULL)
return true;
if(p == NULL || q == NULL)
return false;
return (p->val == q->val)&&isEqual(p->left,q->left) && isEqual(p->right,q->right);
}
};
1003.相连的1:BFS。
class Solution {
public:
int countConnectedOnes(vector<vector<char>>& A){
int ans = 0,i,j;
bool found = true;
vector<vector<bool>> visited(A.size(),vector<bool>(A[0].size(),false));
while(found){
found = false;
for(i = 0;i < A.size();i++)
for(j = 0;j < A[0].size();j++){
if(A[i][j] == '1' && !visited[i][j]{
found = true;
queue<pair<int,int>> q;
q.push(pair<int, int>(i,j));
visited[i][j] = true;
int x[4] = {-1,1,0,0};
int y[4] = {0,0,-1,1};
while(!q.empty()){
pair<int, int> cur = q.front();
q.pop();
for(int ii = 0;ii < 4;ii++)
if(cur.first + y[ii] < A.size() && cur.first + y[ii] >= 0 && cur.second + x[ii] < A[0].size() && cur.second + x[ii] >= 0)
if(!visited[cur.first + y[ii]][cur.second + x[ii]] && A[cur.first + y[ii]][cur.second + x[ii]] == '1'){
q.push(pair<int, int>(cur.first + y[ii],cur.second + x[ii]));
visited[cur.first + y[ii]][cur.second + x[ii]] = true;}
}
ans ++;
}
}
}
return ans;
}
};
1004.无环图:拓扑排序。
class Solution {
public:
bool isDAG(int n, vector<pair<int, int>>& edges) {
vector<int> out(n,0);
vector<vector<int>> in(n,vector<int>());
vector<pair<int, int>>::iterator itr = edges.begin();
while(itr != edges.end()){
out[(*itr).first] ++;
in[(*itr).second].push_back((*itr).first);
itr++;
}
bool found_out_0 = true;
vector<bool> visited(n,false);
while(found_out_0){
found_out_0 = false;
for(int i = 0;i < out.size();i++)
if(out[i] == 0 && visited[i] == false){
found_out_0 = true;
visited[i] = true;
for(int j = 0;j < in[i].size();j++)
out[in[i][j]]--;
}
}
for(vector<bool>::iterator itr = visited.begin();itr != visited.end();itr++)
if(*itr == false)
return false;
return true;
}
};
1005.最大和:维护一个数组maxsum保留在当前下标下,最大和是多少,往后不断更新。maxsum[i] = max(maxsum[i – 2] + A[i],maxsum[i – 1]);
class Solution {
public:
int maxSum(vector<int>& A) {
if(A.size() < 2)
return A[0];
if(A.size() == 2)
return A[0] > A[1] ? A[0] : A[1];
int maxsum[100000] = {0};
maxsum[0] = A[0],maxsum[1] = A[0] > A[1] ? A[0] : A[1];
for(int i = 2;i < A.size();i++)
maxsum[i] = max(maxsum[i - 2] + A[i],maxsum[i - 1]);
return maxsum[A.size() - 1];
}
};
1006.单词变换:dp[i][j]表示A串从第0个字符开始到第i个字符和B串从第0个字符开始到第j个字符,这两个字串的编辑距离。字符串的下标从1开始。若A[i]==B[j],dp[i + 1][j + 1] = dp[i][j];否则dp[i + 1][j + 1] = min(min(dp[i + 1][j] + 1,dp[i][j + 1] + 1),dp[i][j] + 1)。
class Solution {
public:
int minDistance(string word1, string word2) {
if(word1.size() == 0)
return word2.size();
if(word2.size() == 0)
return word1.size();
vector<vector<int>> dp(word1.size()+1,vector<int>(word2.size()+1));
int i,j;
for(i = 0;i < word1.size();i++)
dp[i][0] = i;
for(j = 0;j < word2.size();j++)
dp[0][j] = j;
for(i = 0;i < word1.size();i++)
for(j = 0;j < word2.size();j++){
char a = word1[i],b = word2[j];
if(a == b)
dp[i + 1][j + 1] = dp[i][j];
else
dp[i + 1][j + 1] = min(min(dp[i + 1][j] + 1,dp[i][j + 1] + 1),dp[i][j] + 1);
}
return dp[word1.size()][word2.size()];
}
};