博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Judge Route Circle --判断圆路线
阅读量:4708 次
发布时间:2019-06-10

本文共 975 字,大约阅读时间需要 3 分钟。

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.

Example 1:

Input: "UD"Output: true

 

Example 2:

Input: "LL"Output: false 思路:      1.这是一个模拟横纵坐标移动的问题,向左移动x--,向右x++,向上y++,向下y--,最后判断 x == 0 && y == 0即可。     2.直接判断'L','R','U','D'出现的次数是否相等 实现代码:
class Solution {public:    bool judgeCircle(string moves) {        int x=0,y=0;          for (int i = 0; i < moves.length(); i++){              if (moves[i] == 'L') x--;              else if (moves[i] == 'R') x++;              else if (moves[i] == 'U') y++;              else y--;          }          return x == 0 && y == 0;      }};

 

转载于:https://www.cnblogs.com/linwx/p/7746101.html

你可能感兴趣的文章
Lambda表达式效率问题
查看>>
【转载】iOS 设置Launch Image 启动图片(适用iOS9)
查看>>
最快得到MYSQL两个表的差集
查看>>
KVC中setValuesForKeysWithDictionary: (转载)
查看>>
UML类图关系
查看>>
清理Visual Studio打开的项目和文件、查找和最近引用组件痕迹
查看>>
10.11NOIP模拟题(3)
查看>>
正则表达式速查表
查看>>
项目开源-基于ASP.NET Core和EF Core的快速开发框架
查看>>
写的挺好 placeholder 的模拟用法
查看>>
Vue02 -- 生命周期
查看>>
搭建NFS服务器
查看>>
Linux 下配置多路径及SCSI扫描磁盘重新发现大小
查看>>
(3)SpringMVC的Json数据交互
查看>>
多继承的越级继承
查看>>
poj 2891 扩展欧几里得迭代解同余方程组
查看>>
帮你免于失业的十大软件技术!
查看>>
localhost访问卡顿的解决方法
查看>>
Windows系统安装Mysql前运行库依赖
查看>>
最近公共祖先LCA(Tarjan算法)的思考和算法实现——转载自Vendetta Blogs
查看>>