LeetCode 657. Robot Return to Origin

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:

1Input: "UD" 2Output: true 3Explanation: 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:

1Input: "LL" 2Output: false 3Explanation: 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.

**题目描述:**大概意思是求一个点经过若干次移动能否回到原来的位置。

**题目分析:**我们只需要去判断两个条件:

  • 向左移动的次数和向右移动的次数是否相等
  • 向上移动的次数是否和向下移动的次数相等

于是我们只需要计算出向左、向右、向上、向下分别走了多少步就行了。

python 代码:

1class Solution(object): 2 def judgeCircle(self, moves): 3 """ 4 :type moves: str 5 :rtype: bool 6 """ 7 moves_length = len(moves) 8 L_count = 0 9 R_count = 0 10 U_count = 0 11 D_count = 0 12 for i in range(moves_length): 13 if moves[i] == 'L': 14 L_count = L_count + 1 15 elif moves[i] == 'R': 16 R_count = R_count + 1 17 elif moves[i] == 'U': 18 U_count = U_count + 1 19 elif moves[i] == 'D': 20 D_count = D_count + 1 21 22 if L_count == R_count and U_count == D_count: 23 return True 24 else: 25 return False

C++ 代码:

1class Solution { 2public: 3 bool judgeCircle(string moves) { 4 int moves_length = moves.length(); 5 int L_count = 0; 6 int R_count = 0; 7 int U_count = 0; 8 int D_count = 0; 9 for(int i = 0; i < moves_length; i++){ 10 if(moves[i] == 'L'){ 11 L_count++; 12 } 13 else if(moves[i] == 'R'){ 14 R_count++; 15 } 16 else if(moves[i] == 'U'){ 17 U_count++; 18 } 19 else if(moves[i] == 'D'){ 20 D_count++; 21 } 22 } 23 if(L_count == R_count && U_count == D_count){ 24 return true; 25 } 26 return false; 27 } 28};
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )