---
# System prepended metadata

title: 657. Robot Return to Origin
tags: [LeetCode]

---

---
tags: LeetCode
---

# 657. Robot Return to Origin

There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.

Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:    
Input: "UD"    
Output: true     
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
 
Example 2:    
Input: "LL"    
Output: false    
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.

輸入範本如下
```C#
public class Solution {
    public bool JudgeCircle(string moves) {
        
    }
}
```
限制使用 LinQ Select , Aggregate , C# 8.0.語法 switch 運算式
```C#
88 ms	25.7 MB
You are here!
Your runtime beats 28.57 % of csharp submissions.
public bool JudgeCircle(string moves)
{
    var (Vertical, Horizontal) = moves.Aggregate((Vertical: 0, Horizontal: 0), (result, c) =>
    {
           if (c == 'U') result.Vertical++;
           else if (c == 'D') result.Vertical--;
           else if (c == 'L') result.Horizontal--;
           else if (c == 'R') result.Horizontal++;
           return result;
    });
    return Vertical == 0 & Horizontal == 0;
}
```
```C#
100 ms	26.1 MB
You are here!
Your runtime beats 9.77 % of csharp submissions
public bool JudgeCircle(string moves)
{
    var (Vertical, Horizontal) = moves.Select(c => c switch
    {
       'U' => (Vertical: 1, Horizontal: 0),
       'D' => (Vertical: -1, Horizontal: 0),
       'L' => (Vertical: 0, Horizontal: -1),
       'R' => (Vertical: 0, Horizontal: 1),
       _ => (Vertical: 0, Horizontal: 0),
    }).Aggregate((result, item) => (result.Vertical + item.Vertical, result.Horizontal + item.Horizontal));
    return Vertical == 0 & Horizontal == 0;
}
```






### 直覺想法
題目好長. 這題最難的是看完題目 Orz.
題目限制機器人移動上下左右四個方向的位移是相同的. 因此看機器人是否走回原點只要看左與右和上與下的移動次數是否相同.

```C#
72 ms	26 MB
You are here!
Your runtime beats 96.62 % of csharp submissions.
public bool JudgeCircle(string moves)
{
    int updown = 0, leftRight = 0;
    foreach (var c in moves)
    {
        if (c == 'U')
            updown++;
        else if (c == 'D')
            updown--;
        else if (c == 'L')
            leftRight--;
        else
            leftRight++;
    }
    return updown == 0 && leftRight == 0;
}
```











### Thank you! 

You can find me on

- [GitHub](https://github.com/s0920832252)
- [Facebook](https://www.facebook.com/fourtune.chen)

若有謬誤 , 煩請告知 , 新手發帖請多包涵

# :100: :muscle: :tada: :sheep: 
