657. Robot Return to Origin

x,y两个变量,这道问题解决 执行效率很高,以后不用多看这题,贼简单的那种

 bool judgeCircle(string moves) {
        int i = 0;
        int x = 0, y = 0;
        while(moves[i] != '\0'){
            char c = moves[i];
            switch (c){
                case 'U': y++;
                break;
                case 'D': y--;
                break;
                case 'R': x++;
                break;
                case 'L': x--;
                break;
            }
           i++;
        }
        if(x == 0 && y == 0)
            return true;
        else
            return false;
     }

猜你喜欢

转载自blog.csdn.net/Do_not_be_shy/article/details/84565604