leetcode-contest-94

本文提供LeetCode周赛94两道题目的详细解答思路与代码实现。第一题涉及二叉树叶子节点序列相似性的判断;第二题则为模拟机器人依据指令行走并返回最大距离的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

contest 链接:

题目1:判断两个tree的所有leaf组成的sequence数组是否相等。

     解法:找到所有的leaf

题目链接:https://leetcode.com/contest/weekly-contest-94/problems/leaf-similar-trees/

/**
 * 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:有障碍的点会使得机器人不能通过,并留在原地

题目链接:

https://leetcode.com/contest/weekly-contest-94/problems/walking-robot-simulation/

/*
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;
        
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值