leetcode-pp / 91alg-5-daily-check

91 天学算法第五期打卡
55 stars 14 forks source link

【Day 32 】2021-10-11 - 657. 机器人能否返回原点 #49

Open azl397985856 opened 2 years ago

azl397985856 commented 2 years ago

657. 机器人能否返回原点

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/robot-return-to-origin/

前置知识

移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。

注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。

 

示例 1:

输入: "UD" 输出: true 解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。

示例 2:

输入: "LL" 输出: false 解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。

xulli1996 commented 2 years ago

思路

模拟上下左右 两个变量记录

代码

class Solution {
    public boolean judgeCircle(String moves) {
        int x=0,y=0;
        for (char c : moves.toCharArray()) {
            if(c == 'R') x++;
            if(c == 'L') x--;
            if(c == 'U') y++;
            if(c == 'D') y--;
        }
        return x == 0 && y== 0;
    }
}
lihuiwen commented 2 years ago

代码

/*
 * @lc app=leetcode.cn id=657 lang=javascript
 *
 * [657] 机器人能否返回原点
 */

// @lc code=start
/**
 * @param {string} moves
 * @return {boolean}
 */
var judgeCircle = function(moves) {
  let x = 0
  let y = 0
  for (let i = 0; i < moves.length; i++) {
    const move = moves[i];
    if (move === 'U') {
      y++
    }
    if (move === 'D') {
      y--
    }
    if (move === 'R') {
      x++
    }
    if (move === 'L') {
      x--
    }
  }

  return x === 0 && y === 0
};
Kirito1017 commented 2 years ago

class Solution {
    public boolean isRobotBounded(String instructions) {
        // north 0, east 1, south 2, west = 3
        // index 0
        // R -> (status + 1) / 4
        // l -> (status + 3) / 4

        int[][] directions = new int[][] {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};
        int x = 0, y = 0, index = 0;
        for (char i : instructions.toCharArray()) {
            if (i == 'L') index = (index + 3) % 4;
            else if (i == 'R') index = (index + 1) % 4;
            else {
                x += directions[index][0];
                y += directions[index][1];
            }
        }

        return (x == 0 && y == 0) || index != 0;
    }
}
Bingbinxu commented 2 years ago

代码


class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0;
        int y = 0;
        for (const auto& move : moves)
        {
            switch (move)
            {
                case 'U':
                    y--;
                    break;
                case 'D':
                    y++;
                    break;
                case 'R':
                    x++;
                    break;
                case 'L':
                    x--;
                    break;
                }
        }
        return x == 0 && y == 0;
    }
};
复杂度分析
时间复杂度:O(n)
空间复杂度:O(1)
JianXinyu commented 2 years ago
class Solution {
public:
    bool judgeCircle(string moves) {
        int left = 0, up = 0;
        for(auto& m : moves){
            if (m == 'U')
                up++;
            else if (m == 'D')
                up--;
            else if (m == 'L')
                left++;
            else
                left--;
        }

        if(!left && !up)
            return true;
        return false;
    }
};
Auto-SK commented 2 years ago

思路

模拟

程序

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        x = y = 0
        for move in moves:
            if move == 'U': y -= 1
            elif move == 'D': y += 1
            elif move == 'L': x -= 1
            elif move == 'R': x += 1
        return x == y == 0

复杂度

Bochengwan commented 2 years ago

思路

简单模拟

代码

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        move_map = {'R':(1,0),'L':(-1,0),'U':(0,1),'D':(0,-1)}
        start = (0,0)
        for move in moves:
            start = (start[0]+move_map[move][0],start[1]+move_map[move][1])
        if start[0]!=0 or start[1]!=0:
            return False
        return True

复杂度分析

ChenJingjing85 commented 2 years ago

思路

模拟

代码

class Solution {
    public boolean judgeCircle(String moves) {
        char[] chars = moves.toCharArray();
        int ud = 0;
        int lr = 0;
        for (int i = 0; i < moves.length(); i++) {
            char c = moves.charAt(i);
            switch(c){
                case 'U':
                    ud=ud+1;
                    break;
                case 'D':
                    ud--;
                    break;
                case 'L':
                    lr--;
                    break;
                case 'R':
                    lr++;
                    break;
            }
        }
        return ud==0 && lr==0;

    }
}

复杂度分析

vaqua commented 2 years ago

思路

模拟

/**
 * @param {string} moves
 * @return {boolean}
 */
var judgeCircle = function(moves) {
    let x=0,y=0
    for(let i=0;i<moves.length;i++) {
        let move = moves[i]
        if(move === 'U') y++
        if(move === 'D') y--
        if(move === 'R') x++
        if(move === 'L') x--
    }
    return x === 0 && y=== 0
};

复杂度分析:

时间复杂度:O(n)
空间复杂度: O(1)

zjsuper commented 2 years ago
class Solution:
    def judgeCircle(self, moves: str) -> bool:
        dic= {"R": 0,"L":0,"U":0,"D":0}

        for i in moves:
            dic[i] += 1

        if dic['R'] == dic['L'] and dic['U'] == dic['D']:
            return True
        else:
            return False
chakochako commented 2 years ago
class Solution:
    def judgeCircle(self, moves: str) -> bool:
        x, y = 0, 0
        dct = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}
        for i in moves:
            a, b = dct[i]
            x, y = x+a, y+b
        return x == 0 and y == 0
zhangyalei1026 commented 2 years ago

思路

如果机器人L和R方向走的步数一样,U和D走的步数一样,机器人回到原点

代码

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        l, r, u, d = 0, 0, 0, 0
        for chara in moves:
            if chara == "L":
                l += 1
            elif chara == "R":
                r += 1
            elif chara == "U":
                u += 1
            else:
                d += 1
        return l == r and u == d

复杂度

时间复杂度:O(n) 空间复杂度:O(1)

SunStrongChina commented 2 years ago

657. 机器人能否返回原点

入选理由

暂无

题目地址

https://leetcode-cn.com/problems/robot-return-to-origin/

前置知识

  • 模拟

题目描述

在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。

移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。

注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。

 

示例 1:

输入: "UD"
输出: true
解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。

示例 2:

输入: "LL"
输出: false
解释:机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。

它怎么走我怎么干

class Solution:
def judgeCircle(self, moves: str) -> bool:
pos=[0,0]
for i in range(len(moves)):
if moves[i]=='U':
pos[0]-=1
elif moves[i]=='D':
pos[0]+=1
elif moves[i]=='L':
pos[1]-=1
elif moves[i]=='R':
pos[1]+=1
if pos[0]==0 and pos[1]==0:
return True
else:
return False

时间复杂度:O(n) 空间复杂度:O(1)

zhiyuanpeng commented 2 years ago
class Solution(object):
    def judgeCircle(self, moves):
        x = y = 0
        for move in moves:
            if move == 'U': y -= 1
            elif move == 'D': y += 1
            elif move == 'L': x -= 1
            elif move == 'R': x += 1

        return x == y == 0
chakochako commented 2 years ago
class Solution:
    def judgeCircle(self, moves: str) -> bool:
        x, y = 0, 0
        dct = {"U": (-1, 0), "D": (1, 0), "L": (0, -1), "R": (0, 1)}
        for i in moves:
            a, b = dct[i]
            x, y = x+a, y+b
        return x == 0 and y == 0
Toms-BigData commented 2 years ago

【Day 32】657. 机器人能否返回原点

思路

上下y轴+-左右x轴+-看最终位置

Python3代码

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        if moves == None:
            return True
        row = 0
        col = 0
        for move in moves:
            if move == 'U':
                col += 1
            if move == 'D':
                col -= 1
            if move == 'L':
                row -= 1
            if move == 'R':
                row += 1
        if row == 0 and col == 0:
            return True
        return False

复杂度 时间:O(n) 空间:O(1)

jerry9926 commented 2 years ago

思路

(mark)

代码

const judgeCircle = function (moves) {
  const map = new Map();
  const movesArr = moves.split("");
  for (let el of movesArr) {
    map.has(el) ? map.set(el, map.get(el) + 1) : map.set(el, 1);
  }
  let udEval = map.get("U") === map.get("D") ? true : false;
  let lrEval = map.get("L") === map.get("R") ? true : false;
  return udEval && lrEval;
};

复杂度分析

saltychess commented 2 years ago

代码

class Solution {
    public boolean judgeCircle(String moves) {
        if(moves==null||moves.length()==0) {
            return true;
        }
        int right=0,down=0;
        for(int i=0;i<moves.length();i++) {
            char c = moves.charAt(i);
            if(c=='U') {
                down--;
            }else if(c=='D'){
                down++;
            }else if(c=='L') {
                right--;
            }else {
                right++;
            }
        }
        return right==0&&down==0;
    }
}
hellowxwworld commented 2 years ago
    bool judgeCircle(string moves) {
        int x = 0;
        int y = 0;
        for (const auto& move : moves) {
            switch (move) {
                case 'U':
                    y--;
                    break;
                case 'D':
                    y++;
                    break;
                case 'R':
                    x++;
                    break;
                case 'L':
                    x--;
                    break;
                }
        }
        return x == 0 && y == 0;
    }