657. Judge Route Circle

来源:互联网 发布:如何删除mac版office 编辑:程序博客网 时间:2024/05/21 00:19

题意:判断机器人在经过UDLR操作之后是否回到原点?

解决思路:构建坐标系,Up就x+1,Down就x-1,Left就y+1,Right就y-1,最后判断x和y是不是都是0。

代码:

bool judgeCircle(string moves) {int x = 0;int y = 0;for (char c : moves) {if (c == 'U') {x++;} else if (c == 'D') {x--;} else if (c == 'L') {y++;} else if (c == 'R') {y--;}}if (x == 0 && y == 0) {return true;}else {return false;}}


原创粉丝点击