问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3959 访问。

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

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

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

输入: "UD"

输出: true

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

输入: "LL"

输出: false

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


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.

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.

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.


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3959 访问。

public class Program {public static void Main(string[] args) {var moves = "UD";var res = JudgeCircle(moves);Console.WriteLine(res);moves = "LL";res = JudgeCircle2(moves);Console.WriteLine(res);moves = "ULDR";res = JudgeCircle3(moves);Console.WriteLine(res);moves = "LDURD";res = JudgeCircle4(moves);Console.WriteLine(res);Console.ReadKey();}private static bool JudgeCircle(string moves) {//扫描计数法var countL = 0;var countR = 0;var countU = 0;var countD = 0;foreach(var move in moves) {if(move == 'L') countL++;else if(move == 'R') countR++;else if(move == 'U') countU++;else if(move == 'D') countD++;}return countL == countR && countU == countD;}private static bool JudgeCircle2(string moves) {//哈希计数法var dic = new Dictionary<char, int>() {{'L',0},{'R',0},{'U',0},{'D',0}};foreach(var move in moves) {dic[move]++;}return dic['L'] == dic['R'] && dic['U'] == dic['D'];}private static bool JudgeCircle3(string moves) {//差值计数法var diffL = moves.Length - moves.Replace("L", "").Length;var diffR = moves.Length - moves.Replace("R", "").Length;if(diffL != diffR) return false;var diffU = moves.Length - moves.Replace("U", "").Length;var diffD = moves.Length - moves.Replace("D", "").Length;if(diffU != diffD) return false;return true;}private static bool JudgeCircle4(string moves) {//正则计数法//若想AC,请手动在 public class Solution { 的上一行//添加 using System.Text.RegularExpressions;var countL = Regex.Matches(moves, "L").Count;var countR = Regex.Matches(moves, "R").Count;var countU = Regex.Matches(moves, "U").Count;var countD = Regex.Matches(moves, "D").Count;return countL == countR && countU == countD;}}

以上给出4种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3959 访问。

True
False
True
False

分析:

考虑到部分运行库的使用,以上4种算法的时间复杂度应当均为:  。

C#LeetCode刷题之#657-机器人能否返回原点(Robot Return to Origin)相关推荐

  1. 657. 机器人能否返回原点

    2020-04-25 1.题目描述 机器人能否返回原点 2.题解 无 3.代码 class Solution { public:bool judgeCircle(string moves) {int ...

  2. LeetCode 657. 机器人能否返回原点

    文章目录 1. 题目 2. 解题 1. 题目 在二维平面上,有一个机器人从原点 (0, 0) 开始.给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束. 移动顺序由字符串表示.字 ...

  3. leetcode-4.11[1276. 不浪费原料的汉堡制作方案、237. 删除链表中的节点、657. 机器人能否返回原点](python解法)

    题目1 题解1 class Solution:def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]: ...

  4. C#LeetCode刷题-字符串

    字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) 15.3% 中 ...

  5. C#LeetCode刷题-程序员面试金典

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-程序员面试金典 | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode程序员面试金典 ...

  6. C#LeetCode刷题-剑指Offer

    本文由 比特飞 原创发布,欢迎大家踊跃转载. 转载请注明本文地址:C#LeetCode刷题-剑指Offer | .Net中文网. C#LEETCODE刷题概述 概述 所有LeetCode剑指Offer ...

  7. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C#LeetC ...

  8. LeetCode 刷题之路(python版)

    摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...

  9. Leetcode刷题

    刷题 leetcode 1.两数之和 #哈希表 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int ...

  10. LeetCode刷题(十)----数组-----medium部分(Java、C++)

    LeetCode刷题(十)-----数组-------medium部分(Java.C++) 238. 除自身以外数组的乘积 给定长度为n的整数数组nums,其中n>1,返回输出数组output, ...

最新文章

  1. spring面试重点
  2. Python Matplotlib.pyplot 中文显示异常的简单解决方法
  3. Windbg脚本和扩展工具开篇
  4. POJ 1850 Code(组合数学)
  5. 在CSDN写文章头部生成标题目录
  6. tomcat apr Dockfile
  7. AJAX最大的特点以及作用是什么?经典面试题
  8. Qt6.2.1在线安装教程
  9. java cat_java应用监控之CAT简介
  10. 网络知识之----http七层协议
  11. 凌小宁教授给软件新人的演讲——选择的力量
  12. MacBook Air 2015换硬盘
  13. 云虚拟主机bch 和 云服务器bcc,虚拟主机bch和云服务器bcc
  14. PyCharm选择性忽略PEP8代码风格警告信息
  15. 安卓设备TF卡概率性无法识别问题
  16. Your build settings specify a provisioning profile with the UUID, no provisioni(没多大用)
  17. Mac平台安卓模拟器:网易MuMu mac中文免费版(支持12系统)
  18. emui11如何升级鸿蒙os教程,鸿蒙OS2.0系统怎么降级到EMUI11
  19. visibility的常见用法(小白专用)
  20. python 输入学生成绩大于 90为优_c++ 输入学生成绩,打印出该学生成绩等级,大于等于90为A,小于90且大于等于80为B,小...

热门文章

  1. [【转载】 linux进程间通信方式
  2. Leetcode算法题(C语言)7--两个数组的交集 II
  3. SpringBoot—整合log4j2入门和log4j2.xml配置详解
  4. Git—基础知识及常用命令(系列一)
  5. 【算法学习】AVL平衡二叉搜索树原理及各项操作编程实现(C++)
  6. css3 背景属性与边框属性的新增 200303
  7. 2020-python小工能
  8. dj鲜生-07-静态文件的成功加载-用户注册的页面
  9. mysql-数据库操作-连接-创建-删除-修改编码-查询-切换
  10. mysql按月归档日志表