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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lovefengruoqing/article/details/79084562

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。

有木有感觉很坑。^_^

猜你喜欢

转载自blog.csdn.net/lovefengruoqing/article/details/79084562