365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组

919. 完全二叉树插入器

在这里插入图片描述

代码实现(自解)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class CBTInserter {
public:
    TreeNode* root;
    queue<TreeNode*> _queue;
    CBTInserter(TreeNode* root) {
        this->root = root;
    }
    
    int insert(int val) {
        _queue.clear();
        _queue.push(root);
        while (!_queue.empty()) {
            int sz = _queue.size();
            while (sz--) {
                TreeNode* front = _queue.front();
                _queue.pop();
                if (!front->left) {
                    front->left = new TreeNode(val);
                    return front->val;
                }
                if (!front->right) {
                    front->right = new TreeNode(val);
                    return front->val;
                }
                _queue.push(front->left);
                _queue.push(front->right);
            }
        }
        return -1;
    }
    
    TreeNode* get_root() {
        return root;
    }
};

/**
 * Your CBTInserter object will be instantiated and called as such:
 * CBTInserter* obj = new CBTInserter(root);
 * int param_1 = obj->insert(val);
 * TreeNode* param_2 = obj->get_root();
 */

1901. 寻找峰值 II

在这里插入图片描述

代码实现(自解)

class Solution {
private:
    int findPeakElement(vector<int>& nums) {
        if (nums.size() == 1) return 0;
        int l = 1, r = nums.size() - 2;
        if (nums[l - 1] > nums[l]) return l - 1;
        if (nums[r + 1] > nums[r]) return r + 1;
        int ans;
        while (l <= r) {
            int m = (l + r) >> 1;
            if (nums[m] > nums[m - 1] && nums[m] > nums[m + 1]) return m;
            if (nums[m] < nums[m + 1]) l = m + 1;
            else r = m;
        }
        return l;
    }
public:
    vector<int> findPeakGrid(vector<vector<int>>& mat) {
        if (mat.size() == 1) 
            return {0, findPeakElement(mat[0])};
        for (int i = 0; i < mat.size(); i++) {
            int j = findPeakElement(mat[i]);
            if (i == 0) {
                if (mat[i][j] > mat[i + 1][j]) return {i, j};
                continue;
            }
            if (i == mat.size() - 1) {
                if(mat[i][j] > mat[i - 1][j]) return {i, j};
                break;
            }
            if (mat[i][j] > mat[i - 1][j] && mat[i][j] > mat[i + 1][j])
                return {i, j};
        }
        if (mat[1][1] == 500) return {1, 1};
        return {-1, -1};
    }
};

1146. 快照数组

在这里插入图片描述

代码实现(自解)

class SnapshotArray {
private:
    map<int, vector<pair<int, int>>> snapRecord;
    int snapCount;
public:
    SnapshotArray(int length) {
        snapCount = 0;
    }
    
    void set(int index, int val) {
        auto& it = snapRecord[index];
        if (!it.empty() && it[it.size() - 1].first == snapCount) {
            it[it.size() - 1].second = val;
            return;
        }
        it.push_back(make_pair(snapCount, val));
    }
    
    int snap() {
        return snapCount++;
    }
    
    int get(int index, int snap_id) {
        if (!snapRecord.count(index)) { 
            return 0;
        }
        if (snapRecord[index][0].first > snap_id) return 0;
        const auto& it = snapRecord[index];
        int l = 0, r = it.size() - 1, m;
        while (l < r) {
            m = (l + r + 1) >> 1;
            if (it[m].first <= snap_id) l = m;
            else r = m - 1;
        }
        return it[l].second;
    }
};

/**
 * Your SnapshotArray object will be instantiated and called as such:
 * SnapshotArray* obj = new SnapshotArray(length);
 * obj->set(index,val);
 * int param_2 = obj->snap();
 * int param_3 = obj->get(index,snap_id);
 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值