qiaoxu123
1/17/2019 - 1:06 AM

657. Robot Return to Origin

很简单的一道字符串题目

  • Approach 1 : switch case

使用switch case 实现,遇见条件就执行相应的操作

//Runtime: 20 ms, faster than 82.01%

class Solution {
public:
    bool judgeCircle(string moves) {
        int num1 = 0,num2 = 0;
        
        for(int i = 0;i < moves.length();++i){
            switch(moves[i]){
                case 'U' : num1++; break;
                case 'D' : num1--; break;
                case 'L' : num2++; break;
                case 'R' : num2--; break;
                default  : num1 = 0,num2 = 0;
            }
        }
        return num1 == 0 && num2 == 0;
    }
};