第十九周:[sicily] 算法机考模拟题

1000. 函数求值

保存历层结果暴力破解

     int F(int k, int n) {
           int a[k+2][n+2];
           for(int i = 0; i < n+1; i++)
                a[0][i] = i;
           for (int i = 0; i < k; i++) {
               int total = 0;
               for (int j = 1; j <= n; j++) {
                   total += a[i][j];
                   a[i+1][j] = total;
               }
           }
           return a[k][n];
      }                                
1001.会议安排

对A、B增序排序,按顺序比较算出最大会议数

    int assignConferenceRoom(vector<int>& A, vector<int>& B) {
       sort(A.begin(), A.end());
       sort(B.begin(), B.end());
       int res = 0;
           for(int j = 0; res<A.size() && j < B.size();j++) {
           if (A[res] <= B[j]) {
               res++;
           }
       }
       return res;      
    }                               
1002.等价二叉树

递归求解即可

   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

循环查找到1则进行深度搜索,查找到1则置为0

void changeRightBottom(vector<vector<char>>& A, int x, int y, int& r, int& c) {
    A[x][y] = '0';
    if(x+1 < r && A[x+1][y]=='1') changeRightBottom(A,x+1,y,r,c);
    if(x > 0 && A[x-1][y]=='1')
changeRightBottom(A,x-1,y,r,c);
    if(y+1 < c && A[x][y+1]=='1') changeRightBottom(A,x,y+1,r,c);
    if(y > 0 && A[x][y-1]=='1') changeRightBottom(A,x,y-1,r,c);

}
int countConnectedOnes(vector<vector<char>>& A) {
    int row = A.size();
    if(row == 0) return 0;
    int col = A[0].size();
    if(col == 0) return 0;
    int res = 0;
    for(int i = 0; i < row; i++) {
        for(int j = 0; j < col; j++) {
            if(A[i][j]!='0') {
                changeRightBottom(A, i, j, row, col);
                res++;
            }
        }
    }
    return res;
}                                
1004.判断无环图

转换成图后,进行深度优先搜索,有重复入栈为有环图,返回false

bool DFS(vector<vector<int>>& g, vector<int>& s, int index){
    if(find(s.begin(), s.end(), index) != s.end())
        return false;
    s.push_back(index);
    for(int i = 0; i < g[index].size(); i++) {
        if(!DFS(g, s, g[index][i]))
            return false;
    }
    s.pop_back();
    return true;
}

bool isDAG(int n, vector<pair<int, int>>& edges) {
    if(n < 2) return true;
    vector<vector<int>> graph(n);
    for(int i = 0; i < edges.size(); i++) {
        int in = edges[i].first, out = edges[i].second;
        graph[in].push_back(out);
    }
    vector<int> s;
    for(int i = 0; i < n; i++){
        if(!DFS(graph, s, i))return false;
    }
    return true;
}                           
1005.最大和

动态规划:起始状态为a[0] = A[0], a[1] = max(A[1], A[0]), 转移方程a[i] = max(A[i] + a[i-2], a[i-1])

    int maxSum(vector<int>& A) {
          int a[100000] = {0};
          a[0] = A[0];
          a[1] = A[1] > A[0] ? A[1]:A[0];
          for(int i = 2; i < A.size(); i++){
              a[i] = max(A[i] + a[i-2], a[i-1]);
          } 
          return a[A.size() - 1];
    }
1006.单词变换

课本原题:dp[i][j]表示长度为i的word1与长度为j的word2

int minDistance(string word1, string word2) {
    int l1 = word1.size(),
        l2 = word2.size(), 
        ll1 = l1+1, 
        ll2 = l2+1;
    vector<vector<int>> d(ll1, vector<int>(ll2, 0));
    for(int i = 0; i < ll2; i++)
        d[0][i] = i;
    for(int i = 0; i < ll1; i++)
        d[i][0] = i;
    for(int i = 1; i < ll1; i++) {
        for(int j = 1; j < ll2; j++) {
            if(word1[i-1] == word2[j-1])
                d[i][j] = d[i-1][j-1];
            else {
                int m = min(d[i-1][j], d[i][j-1]);
                d[i][j] = min(m, d[i-1][j-1]) + 1;
            }

        }
    }
    return d[l1][l2];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值