contest 链接:
题目1:判断两个tree的所有leaf组成的sequence数组是否相等。
解法:找到所有的leaf
/**
* 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 {
private:
void leafVec(TreeNode* root,vector<int>& res){
if(root == nullptr){
return;
}
if(root->left == nullptr && root->right == nullptr) {
res.push_back(root->val);
return;
}
leafVec(root->left,res);
leafVec(root->right,res);
}
private:
bool comp(vector<int>& res1,vector<int>& res2){
if(res1.size() != res2.size()){
return false;
}
else{
for(int i=0 ; i<res1.size() ; i++){
if(res1[i]!=res2[i]) return false;
}
}
return true;
}
public:
bool leafSimilar(TreeNode* root1, TreeNode* root2) {
vector<int> res1, res2;
leafVec(root1,res1);
leafVec(root2,res2);
return comp(res1,res2);
}
};
题目2: 用指令控制机器人行走
指令:-1(右转),-2(左转),(1~9表示朝着现在面对方向行走多少步)
obstacle:有障碍的点会使得机器人不能通过,并留在原地
题目链接:
/*
0: the goal is to return the maximum distance that the robot has been through
1. use function changeFace when the command is -1 or -2
clockwise :face: 0 : north , 1:right , 3: south , 4 :left
2.use function moveForward to move when the command is not -1 or -2
3.record the maximum of the distance
*/
class Solution {
// this brute force function checkObstacles will make time exceed limit, do not use it ,
// instead , we build a set to check if it is obstacle
private:
bool checkObstacles(int x, int y, vector<vector<int>>& obstacles){
for(int i = 0 ; i<obstacles.size() ; i++){
if(obstacles[i][0] == x && obstacles[i][1] == y)
return true;
}
return false;
}
// face: 0 : north , 1:right , 3: south , 4 :left
private:
int changeFace(int old , int faceCommand){
if(faceCommand == -2) { //turen left
if(old == 0 ) return 3;
return old-1;
}
if(faceCommand == -1) { // turen right
if(old == 3 ) return 0;
return old+1;
}
}
private:
vector<int> moveForward(int x, int y ,int steps ,int face , set<vector<int>>& obs){
vector<int> res;
res.push_back(x);
res.push_back(y);
switch(face){
case 0:
while(steps){
if(obs.count({x,y+1})){
break;
}
y++;
steps--;
}
res[1] = y ;
break;
case 1:
while(steps){
if(obs.count({x+1,y})){
break;
}
x++;
steps--;
}
res[0] = x ;
break;
case 2:
while(steps){
if(obs.count({x,y-1})){
break;
}
y--;
steps--;
}
res[1] = y ;
break;
case 3:
while(steps){
if(obs.count({x-1,y})){
break;
}
x--;
steps--;
}
res[0] = x ;
break;
default :
break;
}
return res;
}
public:
int robotSim(vector<int>& commands, vector<vector<int>>& obstacles) {
set<vector<int>> obs(obstacles.begin(), obstacles.end());
int x=0 , y=0 ;
int face = 0 ;
vector<int> position;
position.push_back(x);
position.push_back(y);
int result=0;
for(int i = 0 ; i< commands.size() ; i++){
if(commands[i] < 0 ){
face = changeFace(face,commands[i]); // 1. use function changeFace when the command is -1 or -2
}
else{
position = moveForward(position[0],position[1],commands[i],face,obs);
//2.use function moveForward to move when teh command is not -1 or -2
}
result = max(result, position[1]*position[1] + position[0]*position[0]);
// 3.record the maximum of the distance
}
return result;
}
};