1. 程式人生 > >C#LeetCode刷題之#657-機器人能否返回原點(Robot Return to Origin)

C#LeetCode刷題之#657-機器人能否返回原點(Robot Return to Origin)

問題

在二維平面上,有一個機器人從原點 (0, 0) 開始。給出它的移動順序,判斷這個機器人在完成移動後是否在 (0, 0) 處結束。

移動順序由字串表示。字元 move[i] 表示其第 i 次移動。機器人的有效動作有 R(右),L(左),U(上)和 D(下)。如果機器人在完成所有動作後返回原點,則返回 true。否則,返回 false。

注意:機器人“面朝”的方向無關緊要。 “R” 將始終使機器人向右移動一次,“L” 將始終向左移動等。此外,假設每次移動機器人的移動幅度相同。

輸入: "UD"

輸出: true

解釋:機器人向上移動一次,然後向下移動一次。所有動作都具有相同的幅度,因此它最終回到它開始的原點。因此,我們返回 true。

輸入: "LL"

輸出: false

解釋:機器人向左移動兩次。它最終位於原點的左側,距原點有兩次 “移動” 的距離。我們返回 false,因為它在移動結束時沒有返回原點。


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.

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.

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.


示例

public class Program {

    public static void Main(string[] args) {
        var moves = "UD";
        var res = JudgeCircle(moves);
        Console.WriteLine(res);

        moves = "LL";
        res = JudgeCircle2(moves);
        Console.WriteLine(res);

        moves = "ULDR";
        res = JudgeCircle3(moves);
        Console.WriteLine(res);

        moves = "LDURD";
        res = JudgeCircle4(moves);
        Console.WriteLine(res);

        Console.ReadKey();
    }

    private static bool JudgeCircle(string moves) {
        //掃描計數法
        var countL = 0;
        var countR = 0;
        var countU = 0;
        var countD = 0;
        foreach(var move in moves) {
            if(move == 'L') countL++;
            else if(move == 'R') countR++;
            else if(move == 'U') countU++;
            else if(move == 'D') countD++;
        }
        return countL == countR && countU == countD;
    }

    private static bool JudgeCircle2(string moves) {
        //雜湊計數法
        var dic = new Dictionary<char, int>() {
            {'L',0},
            {'R',0},
            {'U',0},
            {'D',0}
        };
        foreach(var move in moves) {
            dic[move]++;
        }
        return dic['L'] == dic['R'] && dic['U'] == dic['D'];
    }

    private static bool JudgeCircle3(string moves) {
        //差值計數法
        var diffL = moves.Length - moves.Replace("L", "").Length;
        var diffR = moves.Length - moves.Replace("R", "").Length;
        if(diffL != diffR) return false;
        var diffU = moves.Length - moves.Replace("U", "").Length;
        var diffD = moves.Length - moves.Replace("D", "").Length;
        if(diffU != diffD) return false;
        return true;
    }

    private static bool JudgeCircle4(string moves) {
        //正則計數法
        //若想AC,請手動在 public class Solution { 的上一行
        //新增 using System.Text.RegularExpressions;
        var countL = Regex.Matches(moves, "L").Count;
        var countR = Regex.Matches(moves, "R").Count;
        var countU = Regex.Matches(moves, "U").Count;
        var countD = Regex.Matches(moves, "D").Count;
        return countL == countR && countU == countD;
    }

}

以上給出4種演算法實現,以下是這個案例的輸出結果:

True
False
True
False

分析:

考慮到部分執行庫的使用,以上4種演算法的時間複雜度應當均為: O(n) 。