LeetCode657-机器人能否返回原点(智力题)

只要向左走的步数和向右走,向上和向下的步数一样就可以了。抵消

/**
 * @param {string} moves
 * @return {boolean}
 */
var judgeCircle = function(moves) {
    
    let R = 0;
    let L = 0;
    let U = 0;
    let D = 0;
    
    for(let item of moves){
        if(item==='R'){
            R++;
        }else if(item==='L'){
            L++;
        }else if(item==='U'){
            U++;
        }else{
            D++;
        }
    }
    
    if(R===L&&U===D){
        return true;
    }else{
        return false;
    }
    
};
原文地址:https://www.cnblogs.com/weizhibin1996/p/9974140.html