C#LeetCode刷题之#657-机器人能否返回原点(Robot Return to Origin)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- [Swift]LeetCode657. 机器人能否返回原点 | Robot Return to Origin
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its mov ...
- Java实现 LeetCode 657 机器人能否返回原点(暴力大法)
657. 机器人能否返回原点 在二维平面上,有一个机器人从原点 (0, 0) 开始.给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束. 移动顺序由字符串表示.字符 move[i ...
- 力扣 (LeetCode)657. 机器人能否返回原点
在二维平面上,有一个机器人从原点 (0, 0) 开始.给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束. 移动顺序由字符串表示.字符 move[i] 表示其第 i 次移动.机器 ...
- C#LeetCode刷题-字符串
字符串篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.6% 中等 5 最长回文子串 22.4% 中等 6 Z字形变换 35.8% 中等 8 字符串转整数 (atoi) ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
随机推荐
- 用前端姿势玩docker【一】Docker通俗理解常用功能汇总与操作埋坑
前言 首先一句话表达个人对docker的理解:与传统虚拟技术基于硬件及物理资源的虚拟化相比,Docker更加轻量化,docker为基于操作系统或内核级别的虚拟化,并且提供了从各种机制与操作以满足从开发 ...
- 集训作业 洛谷P1010 幂次方
这个…… 这个题看上去有点难的样子. 仔细看看,感觉有点简单.啊,是递归啊,正经的看一看,好像是把一个数分成2的几次方的和. 然后余数和比他小的最大的2的次方数如果不是2的一次方或者2的0次方,就继续 ...
- python环境搭建及配置
我选择的是pycharm,这个对新手比较友好 我目前正在自学周志华的西瓜书,在做练习题3.3时需要用到python来实现,做这个练习需要numpy库和matplot库,最开始的时候忘了anaconda ...
- p73_万维网和HTTP协议
一.URL形式 URL不区分大小写 <协议>://<主机>:<端口>/<路径> 二.HTTP协议 HTTP协议定义了浏览器(万维网客户进程)怎样向万维网 ...
- 题解 洛谷 P4189 【[CTSC2010]星际旅行】
一个比较直接的想法就是对每个点进行拆点,拆成入点和出点,限制放在入点和出点相连的边上,然后跑最大费用最大流即可. 但是这样复杂度无法接受,所以考虑模拟费用流来解决本题. 发现 \(H\) 都大于等于该 ...
- WebView in ScrollView:View not displayed because it is too large to fit into a software layer
报错信息 W/View: WebView not displayed because it is too large to fit into a software layer (or drawing ...
- Windows电脑多个SSH Key管理.md
笔者偏在阿里云,Github,开源中国上均存放一些私有项目代码,因此需要再Windows电脑上配置多个SSH Key 环境 操作系统:windows 7 Git 提示:Git 安装后就可以使用 Git ...
- npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
版本的问题 重新输入 npm install 再输入 npm run serve重启,如果还是不可以的话,在把之前装的都清空 依次输入以下命令 rm -rf node_modulesrm packag ...
- e的存在性证明和计算公式的证明
\(\quad\quad前言\quad\quad\\\) \(此证明,改编自中科大数分教材,史济怀版\\\) \(中科大教材,用的是先固定m,再放大m,跟菲赫金哥尔茨的方法一样.\\\) \(而我这里 ...
- 《吊打面试官》系列-Redis基础知识
前言Redis在互联网技术存储方面使用如此广泛,几乎所有的后端技术面试官都要在Redis的使用和原理方面对小伙伴们进行360°的刁难.作为一个在互联网公司面一次拿一次offer的面霸(请允许我使用一下 ...