657. Judge Route Circle (字符串)

来源:互联网 发布:网络摄像机ip设置 编辑:程序博客网 时间:2024/05/16 14:30

https://leetcode.com/problems/judge-route-circle/description/

题目: 判断机器人是否可以回到原点。

思路: 位置只与移动方向有关,与移动顺序无关。比如 LLR 与 RLL的效果是一样的。

class Solution {public:    bool judgeCircle(string moves) {        int l=0,u=0,len=moves.length();        for(int x=0;x<len;x++)        {            if(moves[x]=='L') l++;            if(moves[x]=='R') l--;            if(moves[x]=='U') u++;            if(moves[x]=='D') u--;        }        return (l==0&&u==0)?1:0;    }};