leetCode(Using C)——657. Judge Route Circle

純愛楓若情發表於2018-01-17

Description:

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

If you want to solve the problem, you can visite the web site.click me

Code:

bool judgeCircle(char* moves) {
    int i,x,y;

    x=0;
    y=0;

    for(i=0;moves[i]!='\0';i++){
        switch(moves[i]){
            case 'R': x++;break;
            case 'L': x--;break;
            case 'U': y++;break;
            case 'D': y--;break;
        }
    }
    return x==y&&x==0?true:false;
}

Comment:

此題很簡單,但是就是題意不是很好理解,特別對於我們這種,英語半吊子的人來說,理解有點困難。

開始我以為,題目的意思是,一旦機器人回到走過的路徑,就判定走了一個circle。後來提交發現並不是,然後我又以為是回到起點才叫circle,後來測試又出錯。

最後才明白,原來要求,當所有指令執行完以後,機器人是否回到原點,如果是才叫circle,不是就不叫circle。

有木有感覺很坑。^_^

相關文章