leetcode 657. Judge Route Circle

来源:互联网 发布:大野克夫的水平知乎 编辑:程序博客网 时间:2024/05/21 13:58

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

本题题意很简单,直接遍历即可

代码如下:

#include <iostream>#include <vector>#include <map>#include <set>#include <queue>#include <stack>#include <string>#include <climits>#include <algorithm>#include <sstream>#include <functional>#include <bitset>#include <numeric>#include <cmath>#include <regex>using namespace std;class Solution {public:    bool judgeCircle(string moves)     {        int x = 0, y = 0;        for (char c : moves)        {            if (c == 'R')                x++;            if (c == 'L')                x--;            if (c == 'U')                y++;            if (c == 'D')                y--;        }        return x == 0 && y == 0;    }};
原创粉丝点击