657. Robot Return to Origin

本文介绍了一种算法,用于判断二维平面上的机器人在完成一系列上(U)、下(D)、左(L)、右(R)移动后是否能回到起点(0,0)。通过计算移动次数或使用复数进行方向映射,可以简洁地实现这一目标。

657. Robot Return to Origin

Easy

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is “facing” is irrelevant. “R” will always make the robot move to the right once, “L” will always make it move left, etc. Also, assume that the magnitude of the robot’s movement is the same for each move.

Example 1:

Input: "UD"
Output: true 
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true. 

Example 2:

Input: "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

给一个字符串, 每个字符包含 “U”、”D”、”L”、”R”, 分别表示上下左右, 表示机器人向这个位置走一步, 判断最终是否机器人是否还在原来的位置。

Python

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        i = 0
        j = 0
        for ch in moves:
            if ch == 'U':
                i += 1
            elif ch =='D':
                i -= 1
            elif ch == 'L':
                j += 1
            elif ch == 'R':
                j -= 1
        return i == 0 and j == 0
      
class Solution:
    def judgeCircle(self, moves: str) -> bool:
        directs = {'L':-1, 'R':1, 'U':1j, 'D':-1j}
        return 0 == sum(directs[move] for move in moves)

class Solution:
    def judgeCircle(self, moves: str) -> bool:
        count = collections.Counter(moves)
        return count['U'] == count['D'] and count['L'] == count['R']
function [robot,T_Ln_EE] = DH_Serial2robot(DH_data) % Transforms a description of a multibody system, provided in Denavit-Hartenberg parameters, into the SPART robot model. % % [robot,T_Ln_EE] = DH_Serial2robot(DH_data) % % :parameters: % * DH_data -- Structure containing the DH parameters. (see :doc:`/DH`). % % :return: % * robot -- Robot model (see :doc:`/Tutorial_Robot`). % * T_Ln_EE -- Homogeneous transformation matrix from last link to end-effector --[4x4]. % % **DH descriptions are only supported for serial configurations**. %{ LICENSE This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. %} %=== CODE ===% %Create Robot structure. robot=struct(); %Get name if isfield(DH_data,'name') robot.name=DH.data.name; else robot.name='DH robot'; end %Number of links and joints robot.n_links_joints=DH_data.n; robot.n_q=DH_data.n; %Create links and joints structures robot.links=struct(); robot.joints=struct(); robot.base_link=struct(); %Assign origin robot.origin='DH'; %--- Base link ---% %Get name robot.base_link.name='Base'; %Homogeneous transformation robot.base_link.T=[eye(3),zeros(3,1);zeros(1,3),1]; %Mass and inertia robot.base_link.mass=DH_data.base.mass; robot.base_link.inertia=DH_data.base.I; %--- Iterate through joints and links ---% for i=1:DH_data.n %--- Links ---% %ID robot.links(i).id=i; %Get name
03-19
[1764920049.022789]: Controller Spawner: Loaded controllers: pos_joint_traj_controller, joint_group_vel_controller [INFO] [1764920049.030777]: Loading controller: force_torque_sensor_controller [INFO] [1764920049.062786]: Controller Spawner: Loaded controllers: joint_state_controller, scaled_pos_joint_traj_controller, speed_scaling_state_controller, force_torque_sensor_controller [INFO] [1764920049.070739]: Started controllers: joint_state_controller, scaled_pos_joint_traj_controller, speed_scaling_state_controller, force_torque_sensor_controller [INFO] [1764920049.157379882]: Robot mode is now RUNNING [INFO] [1764920049.157612152]: Robot's safety mode is now NORMAL [INFO] [1764920070.448252993]: Robot requested program [INFO] [1764920070.449193846]: Sent program to robot [INFO] [1764920070.867426635]: Robot connected to reverse interface. Ready to receive control commands. [INFO] [1764920524.946960694]: Connection to reverse interface dropped. [ERROR] [1764920524.982626390]: Sending data through socket failed. [INFO] [1764920524.984212103]: Robot connected to reverse interface. Ready to receive control commands. [INFO] [1764920525.041894751]: Connection to reverse interface dropped. [ERROR] [1764920525.049856852]: Sending data through socket failed. [INFO] [1764920525.049895864]: Robot connected to reverse interface. Ready to receive control commands. [DEBUG] [1764920525.112915369]: Invalid robot package type recieved: 25 [INFO] [1764921139.409715057]: Connection to reverse interface dropped. [ERROR] [1764921139.417127854]: Sending data through socket failed. [INFO] [1764921139.417650014]: Robot connected to reverse interface. Ready to receive control commands. [INFO] [1764921384.895780001]: Connection to reverse interface dropped. [ERROR] [1764921384.897034888]: Sending data through socket failed. [INFO] [1764921384.900863281]: Robot connected to reverse interface. Ready to receive control commands. [DEBUG] [1764921384.980181088]: Invalid robot package type recieved: 25 [INFO] [1764921498.100054229]: Connection to reverse interface dropped. [ERROR] [1764921498.102120385]: Sending data through socket failed. [INFO] [1764921498.105283427]: Robot connected to reverse interface. Re
最新发布
12-06
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值