当前位置:网站首页>365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
365 day challenge leetcode 1000 questions - day 039 full binary tree inserter + find peak II + snapshot array
2022-07-29 05:25:00 【ShowM3TheCode】
List of articles
919. Complete binary tree inserter
Code implementation ( Self solution )
/** * 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. Looking for peaks II
Code implementation ( Self solution )
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. Snapshot array
Code implementation ( Self solution )
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); */
边栏推荐
- 哈夫曼树以及哈夫曼编码在文件压缩上的应用
- C语言用指向指针的指针对n个整数排序
- SM integration is as simple as before, and the steps are clear (detailed)
- The road to success in R & D efficiency of 1000 person Internet companies
- 6.3 references
- Modification of annotation based three-tier project and the way of adding package scanning
- ARFoundation从零开始9-AR锚点(AR Anchor)
- 小白高薪捷径-Qt开发游戏—贪吃蛇
- OCCT学习002-----环境搭建
- 167. 两数之和 II - 输入有序数组
猜你喜欢
GPIO的输入输出详解
直播预告|如何节省30%人工成本,缩短80%商标办理周期?
C语言用指向指针的指针对n个整数排序
最新坦克大战2022-全程开发笔记-1
[event preview] cloud development, efficient and intelligent - the second Alibaba cloud ECS cloudbuild developer competition is about to start
OCCT学习002-----环境搭建
The latest tank battle 2022 - Notes on the whole development -2
法线可视化
C语言连连看秒杀辅助
C language handwritten qq-ai version
随机推荐
C how to realize simple factory mode
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
osgSimplegl3例子分析
WDDM学习
NVIDIA Zhou Xijian: the last mile from design to digital marketing
直播预告|如何通过“智能边缘安全”提升企业免疫力?
6.2 function-parameters
Soft link & hard link
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
CMake 设置vs启动运行环境路径
ARFoundation从零开始8-Geospatial API(地理空间)开发
CryEngine3 调试Shader方法
365天挑战LeetCode1000题——Day 040 设计跳表 + 避免洪水泛滥 + 查找大小为 M 的最新分组 + 销售价值减少的颜色球
Arfoundation starts from zero 9-ar anchor
Helm chart for Kubernetes
Rimworld通过SteamCMD上传创意工坊的方法
Yangyonglin, vice president of Rushi Technology: when traditional industries encounter "digital space"
On AspectJ framework
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
Helm chart for Kubernetes