leetCode题解 寻找运动环
1、题目描述
Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.
The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down).
The output should be true or false representing whether the robot makes a circle.
题目的意思是,如果一个可以上下左右运动,分别记做 ‘U’ ,'D',‘L’,‘R’。将一个机器人的运动序列记做一个string ,根据这个string 判断机器人是否回到了原点。
2、问题分析
特例是机器人没有动,即string的长度是0,那么机器人就在原位。
机器人回到原位的条件是,上下运动的步数相等 并且 左右运动的步数相等。遍历一次string ,分别记录上下左右运动的次数,然后比较每个方向上运动的次数。如果 R == L && U == D ,那么机器人回到了原点。
3、代码
if(moves.size() == )
return true; int u = ;
int d = ;
int l = ;
int r = ; int i = ;
while(i < moves.size())
{
switch (moves[i])
{
case 'U':
++u;
break;
case 'D':
++d;
break;
case 'L':
++l;
break;
case 'R':
++r;
break;
default:
break;
}
i++;
} if(u == d && l == r)
return true; return false;
leetCode题解 寻找运动环的更多相关文章
- leetCode题解寻找最短字符路径
1.题目描述 2.分析 最简单的方案,对每一个字符,向两边寻找. 3.代码 vector<int> shortestToChar(string S, char C) { vector< ...
- C#版(击败100.00%的提交) - Leetcode 744. 寻找比目标字母大的最小字母 - 题解
C#版 - Leetcode 744. 寻找比目标字母大的最小字母 - 题解 744.Find Smallest Letter Greater Than Target 在线提交: https://le ...
- [LeetCode 题解]:Intersection of Two Linked Lists
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- LeetCode:寻找重复数【287】
LeetCode:寻找重复数[287] 题目描述 给定一个包含 n + 1 个整数的数组 nums,其数字都在 1 到 n 之间(包括 1 和 n),可知至少存在一个重复的整数.假设只有一个重复的整数 ...
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 github. 描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回 ...
- [LeetCode 题解] Search in Rotated Sorted Array
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...
- [LeetCode 题解]: Triangle
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a tr ...
- LeetCode:寻找数组的中心索引【668】
LeetCode:寻找数组的中心索引[668] 题目描述 给定一个整数类型的数组 nums,请编写一个能够返回数组“中心索引”的方法. 我们是这样定义数组中心索引的:数组中心索引的左侧所有元素相加的和 ...
- LeetCode:寻找旋转排序数组中的最小值【153】
LeetCode:寻找旋转排序数组中的最小值[153] 题目描述 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0 ...
随机推荐
- sublime text3在交互时解决input()函数无法使用的问题
1,打开sublime text3工具栏,依次点击View->Show Console菜单打开命令行, 2,在命令行里 输入代码 import urllib.request,o ...
- 再学Java 之 解决No enclosing instance of type * is accessible
深夜,临睡前写了个小程序,出了点小问题 public class Test_drive { public static void main(String[] args){ A a = new A(); ...
- xampp启动失败 Apache shutdown unexpectedly
我知道是端口被我的虚拟机占用,但是我的虚拟机也要同时打开,所以 我需要修改xampp的端口 1, 2修改二个配置文件 修改httpd.conf 修改httpd-ssl.conf 4,重启xampp
- Android 开发工具类 20_DOMPersonService
xml 文件 <?xml version="1.0" encoding="UTF-8"?> <persons> <person i ...
- #pragma的一些用法
1.#pragma message message 参数:Message参数能够在编译信息输出窗口输出相应的信息,这对于源代码的信息控制特别重要,其使用方法为: #pragma message(&qu ...
- 使用subgit进行svn迁移至git(branch,tags)
前言: 最近公司需要将整体项目从svn迁移至gitlab上,经过几天的研究,现记录一下流程 整体思路是进行一次导入: 先通过subgit将svn整个import至本地,在与git上的项目进行合并. 1 ...
- java中的复制数组arraycopy()
System.arraycopy();//静态方法,在System类中定义,注意copy首字母是小写的 例子: int[] a = {1,2,3,4,5}; int[] b = {9,8,7,6}; ...
- Ionic3 UI组件之 ImageViewer
组件特性: 轻触图片可全屏查看 手势上下滑动可关闭全屏查看 点击导航箭头可关闭视图 双击查看全图,并可放大 参考地址:https://github.com/Riron/ionic-img-viewer ...
- 下拉框多选实现回显及sql
<td class="tabTd"><label>客户来源:</label></td> <td><select c ...
- CSS 通过使用Important覆盖所有其他样式
在许多情况下,您将使用CSS库.这些可能会意外覆盖您自己的CSS.所以当你绝对需要确定一个元素具有特定的CSS时,可以使用 !important. 让我们回到之前的 pink-text class 声 ...