当前位置:网站首页>Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
Leetcode force buckle (Sword finger offer 36-39) 36 Binary search tree and bidirectional linked list 37 Serialize binary tree 38 Arrangement of strings 39 Numbers that appear more than half of the tim
2022-07-07 19:57:00 【Wood White CPP】
The finger of the sword Offer 36. Binary search tree and double linked list
Answer key :
The idea is simple , It is a medium order traversal , Connect nodes while traversing .
Code :
class Solution {
public:
Node *pre,*head;
void dfs(Node* root){
if(root==nullptr) return;
dfs(root->left);
if(pre==nullptr) head=root;// This is a header node
else
pre->right=root;
root->left=pre;
pre=root;
dfs(root->right);
}
Node* treeToDoublyList(Node* root) {
if(root == nullptr) return root;
dfs(root);
head->left=pre;
pre->right=head;
return head;
}
};
result :
The finger of the sword Offer 37. Serialize binary tree
Answer key :
Serialization generally adopts pre order , Middle preface , In the following order , Here we use the preface .
serialize , Use preorder traversal to save the nodes of the binary tree into a string , Between each node ’,‘ separate . It is worth noting that , In the process of traversal, we need to treat the binary tree as a full binary tree , Blank nodes with ’null‘ Express .
Deserialization , Put the string in the queue que in , use ’,‘ Judge and push into the queue . Traverse through the preamble , While traversing the preorder, establish a binary tree node .
Code :
class Codec {
public:
// Encodes a tree to a single string.
string serialize(TreeNode* root) {
if(root==nullptr) return "null,";
string str=to_string(root->val)+',' ;
str+=serialize(root->left);
str+=serialize(root->right);
return str;
}
// Decodes your encoded data to tree.
TreeNode* deserialize(string data) {
queue<string> que;
string str;
for(auto i:data){
if(i==','){
que.push(str);
str.clear();
}
else
str.push_back(i);
}
return rdeserialize(que);
}
TreeNode* rdeserialize(queue<string> &que){
if (que.front() == "null") {
que.pop();
return nullptr;
}
TreeNode* root = new TreeNode(stoi(que.front()));
que.pop();
root->left = rdeserialize(que);
root->right = rdeserialize(que);
return root;
}
};
result :
The finger of the sword Offer 38. Arrangement of strings
Answer key :
The key is to repeat , Because there are repeated characters . You can use a flag bit , Judge whether the current character appears in the previous string .
Code :
class Solution {
public:
vector<string> res;
void dfs(string &s,int sz,int pos){
if(pos==sz) res.push_back(s);
for(int i=pos;i<sz;++i){
bool flag=true;
for(int j=pos;j<i;++j){
if(s[j]==s[i]) flag=false;
}
if(flag){
swap(s[i],s[pos]);
dfs(s,sz,pos+1);
swap(s[i],s[pos]);
}
}
}
vector<string> permutation(string s) {
dfs(s,s.size(),0);
return res;
}
};
result :
The finger of the sword Offer 39. A number that appears more than half the times in an array
There is a number in an array that appears more than half the length of the array , Please find out the number .
You can assume that the array is not empty , And there are always many elements in a given array .
Answer key :
Method 1 : Sort
If there are more than half of the repeated characters , After sorting, it must be in the middle of the array .
Code :
class Solution {
public:
int majorityElement(vector<int>& nums) {
sort(nums.begin(),nums.end());
return nums[nums.size()/2];
}
};
result :
Method 2 : Hash
Store with hash , Once the value is greater than half the length of the array, the value is returned .
Code :
class Solution {
public:
int majorityElement(vector<int>& nums) {
unordered_map<int,int>map;
for(auto i:nums){
++map[i];
if(map[i]>nums.size()/2) return i;
}
return -1;
}
};
result :
边栏推荐
- Introduction to bit operation
- tp6 实现佣金排行榜
- mock.js从对象数组中任选数据返回一个数组
- String - string (Lua)
- Jerry's headphones with the same channel are not allowed to pair [article]
- Automatic classification of defective photovoltaic module cells in electroluminescence images-论文阅读笔记
- [RT thread env tool installation]
- 凌云出海记 | 赛盒&华为云:共助跨境电商行业可持续发展
- R语言dplyr包select函数、group_by函数、filter函数和do函数获取dataframe中指定因子变量中指定水平中特定数值数据列的值第三大的值
- Notes...
猜你喜欢
杰理之相同声道的耳机不允许配对【篇】
小试牛刀之NunJucks模板引擎
CMD command enters MySQL times service name or command error (fool teaching)
el-upload上传组件的动态添加;el-upload动态上传文件;el-upload区分文件是哪个组件上传的。
Jerry's headphones with the same channel are not allowed to pair [article]
2022.07.05
剑指 Offer II 013. 二维子矩阵的和
开源OA开发平台:合同管理使用手册
PMP对工作有益吗?怎么选择靠谱平台让备考更省心省力!!!
杰理之关于 TWS 配对方式配置【篇】
随机推荐
Nunjuks template engine
9 atomic operation class 18 Rohan enhancement
Empowering smart power construction | Kirin Xin'an high availability cluster management system to ensure the continuity of users' key businesses
IP 工具类
Dynamic addition of El upload upload component; El upload dynamically uploads files; El upload distinguishes which component uploads the file.
J ü rgen schmidhub reviews the 25th anniversary of LSTM papers: long short term memory All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversar
[confluence] JVM memory adjustment
9 原子操作类之18罗汉增强
杰理之快速配对,不支持取消配对【篇】
[Verilog advanced challenge of Niuke network question brushing series] ~ multi bit MUX synchronizer
Semantic SLAM源码解析
L1-028 judging prime number (Lua)
MySQL、sqlserver oracle数据库连接方式
杰理之相同声道的耳机不允许配对【篇】
【RT-Thread env 工具安装】
R语言ggplot2可视化:使用ggpubr包的ggqqplot函数可视化QQ图(Quantile-Quantile plot)
Make insurance more "safe"! Kirin Xin'an one cloud multi-core cloud desktop won the bid of China Life Insurance, helping the innovation and development of financial and insurance information technolog
华南X99平台打鸡血教程
国家网信办公布《数据出境安全评估办法》:累计向境外提供10万人信息需申报
让这个 CRMEB 单商户微信商城系统火起来,太好用了!