LeetCode 第 180 场周赛

5356. 矩阵中的幸运数

class Solution {
public:
    vector luckyNumbers (vector>& matrix) {
        vector result;
        for(int i = 0; i < matrix.size();i++)
        {
            for(int j = 0;j < matrix[i].size(); j++)
            {
                int temp = matrix[i][j];
                int min_col = *min_element(matrix[i].begin(), matrix[i].end());
                int max_row = 0;
                for(int k = 0; k < matrix.size();k++)
                {
                    max_row = max(max_row, matrix[k][j]);
                }
                if(temp == max_row && temp == min_col)
                {
                    result.push_back(temp);
                }
            }
        }
        return result;
    }
};

5357. 设计一个支持增量操作的栈

class CustomStack {
public:
    vector s;
    int pos = 0;
    int maxSize;
    CustomStack(int maxSize) {
        this->maxSize = maxSize;
        s = vector(maxSize,0);
    }
    
    void push(int x) {
        if(pos0) {
            return s[--pos];
        }
        else {
            return -1;
        }
        
    }
    
    void increment(int k, int val) {
        k = min(k,pos);
        for(int i = 0; i < k;i++)  {
            s[i] += val;
        }
    }
   
};

5179. 将二叉搜索树变平衡

class Solution {
public:
    int arr[100001];
    int n;
    TreeNode* sortedArrayToBST(int arr[], int start, int end) {
      if (start > end) return NULL;
      int mid = start + (end - start) / 2;
      struct TreeNode *root = new TreeNode(arr[mid]);
      root->left = sortedArrayToBST(arr, start, mid-1);
      root->right = sortedArrayToBST(arr, mid+1, end);
      return root;
    }
    
    TreeNode* sortedArrayToBST(int arr[], int n) {
      return sortedArrayToBST(arr, 0, n-1);
    }
    void zhongxu(TreeNode* root) {
        if(root!=NULL) {
            zhongxu(root->left);
            arr[n++] = root->val;
            zhongxu(root->right);
        }
    }
    
    TreeNode* balanceBST(TreeNode* root) {
        zhongxu(root);
        return sortedArrayToBST(arr, 0, n-1);
    }
};

5359. 最大的团队表现值

你可能感兴趣的:(LeetCode)