2020_02_04 LeetCode刷题

1551. 使数组中所有元素相等的最小操作数

简单模拟题。根据n为奇数偶数可以找到规律,然后分情况讨论即可。

class Solution {
public:
    int minOperations(int n) {
        int ans = 0;
        if(n % 2 == 0) {
            for(int i = 0; i < n / 2; i++) {
                int sum1 = 2 * i + 1;
                int sum2 = 2 * (n - 1 - i) + 1;
                ans += (sum2 - sum1) / 2;
            }
        } else {
            for(int i = 0; i < n / 2; i++) {
                int sum1 = 2 * i + 1;
                int sum2 = 2 * (n / 2) + 1;
                ans += (sum2 - sum1);
            }
        }
        return ans;
    }
};

1302. 层数最深叶子节点的和

简单模拟题。运用哈希表记录每层的节点和,之后找出哈希表中最大的层数然后返回和即可。

/**
 * 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:
    int deepestLeavesSum(TreeNode* root) {
        map<int,int> p;
        dfs(root,p,0);
        int ans = 0, max1 = 0;
        for(map<int,int>::iterator it1=p.begin();it1!=p.end();it1++) {
            if(it1->first > max1) {
                max1 = it1 -> first;
                ans = it1 -> second;
            }
        }
        return ans;
    }
    void dfs(TreeNode* root, map<int,int>& p,int deepth) {
        if(root == nullptr)
            return;
        p[deepth] += root->val;
        dfs(root->right,p,deepth + 1);
        dfs(root->left,p,deepth + 1);
    }
};

1637. 两点之间不包含任何点的最宽垂直面积

简单模拟题。直接统计所有的x坐标,排序后求出两两之间的最大差值即可。

class Solution {
public:
    int maxWidthOfVerticalArea(vector<vector<int>>& points) {
        vector<int> sum;
        int len1 = points.size();
        for(int i = 0;i < len1; i++)
            sum.push_back(points[i][0]);
        sort(sum.begin(),sum.end());
        int ans = 0;
        for(int i = 1; i < len1; i++) 
            ans = max(ans, sum[i] - sum[i - 1]);
        return ans;
    }
};

1689. 十-二进制数的最少数目

简单模拟题。找规律,很容易发现最少的数目为每一位上的最大值决定,所以找出每一位上的最大值即可。

class Solution {
public:
    int minPartitions(string n) {
        int ans = 0, len1 = n.length();
        for(int i = 0; i < len1; i++) {
            int sum = n[i] - '0';
            ans = max(ans, sum);
        }
        return ans;
    }
};

1476. 子矩形查询

简单模拟题。按照题目意思模拟即可。

class SubrectangleQueries {
public:
    vector<vector<int>> rectangle1;
    SubrectangleQueries(vector<vector<int>>& rectangle) {
        rectangle1 = rectangle;
    }
    
    void updateSubrectangle(int row1, int col1, int row2, int col2, int newValue) {
        for(int i = row1; i <= row2 ;i++)
            for(int j = col1; j <= col2; j++) 
                rectangle1[i][j] = newValue;
    }
    
    int getValue(int row, int col) {
        return rectangle1[row][col];
    }
};

/**
 * Your SubrectangleQueries object will be instantiated and called as such:
 * SubrectangleQueries* obj = new SubrectangleQueries(rectangle);
 * obj->updateSubrectangle(row1,col1,row2,col2,newValue);
 * int param_2 = obj->getValue(row,col);
 */

 

上一篇:珂朵莉树学习笔记


下一篇:10.07