Leetcode - Medium - 1041. Robot Bounded In Circle - Javascript

2022年1月10日 星期一

Leetcode - Medium - 1041. Robot Bounded In Circle - Javascript


Medium

On an infinite plane, a robot initially stands at (0, 0) and faces north. The robot can receive one of three instructions:

  • "G": go straight 1 unit;
  • "L": turn 90 degrees to the left;
  • "R": turn 90 degrees to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

 

Example 1:

Input: instructions = "GGLLGG"
Output: true
Explanation: The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.


解題方向:

1. 這個問題要注意的是不止最後要回到原點, 連最後執行時的方向都不要跟一開始相同, 就是代表可以形成一個 circle, 就回傳 true

2. 這邊可以透過紀錄 x, y 軸的方式了解位置

3. 可以透過執行指令時紀錄移動方向


程式碼 :

/**
 * @param {string} instructions
 * @return {boolean}
 */
var isRobotBounded = function(instructions) {
    let position = [0, 0];
    
    function getDirection (instr, direction) {
        const directionList = [
            'down',
            'right',
            'up',
            'left'
        ];
        const directionListLen = 4;
        
        let directionIndex = 0;
       for (let i = 0; i < directionListLen; i++) {
           if (directionList[i] === direction) {
               directionIndex = i;
               break;
           }
       }
        
        let addVal = 0;
        
        if (instr === 'L') {
            addVal = -1;
        } else if (instr === 'R') {
            addVal = 1;   
        }
        
        
        directionIndex += addVal;
        
        if (directionIndex < 0) {
           directionIndex = 4 +  directionIndex;
        } else if (directionIndex >= 4) {
            directionIndex = directionIndex % 4;
        }
        return directionList[directionIndex];
    }
    
    function getPosition(_position, direction) {
        switch (direction) {
            case 'left':
                _position[0] -= 1;
                break;
            case 'right':
                _position[0] += 1;
                break;
            case 'down':
                _position[1] += 1;
                break;
            case 'up':
                _position[1] -= 1;
                break;
        }
        
        return _position;
    }
    
    let direction = 'down';
    for (i in instructions) {
        const _instruction = instructions[i];
        
        
        if (_instruction === 'L' || _instruction === 'R' ) {
            direction = getDirection(_instruction, direction);
        } else if (_instruction === 'G') {
            position = getPosition(position, direction);
            
        } else {
            return false;
        }        
    }
    
    return JSON.stringify(position) == JSON.stringify([0, 0]) || direction !== 'down';
};


執行成果:





0 意見 :

張貼留言