当前位置: 代码迷 >> 综合 >> 657. 机器人能否返回原点----------LeetCode(java实现)
  详细解决方案

657. 机器人能否返回原点----------LeetCode(java实现)

热度:48   发布时间:2024-01-24 14:33:09.0

解题思路:

给定的字符串中字符L和字符R,字符U和字符D的个数分别相等,则可以在 (0, 0) 处结束。

class Solution {public boolean judgeCircle(String moves) {int len=moves.length();int left=0,right=0,up=0,down=0;for(int i=0;i<len;++i){if(moves.charAt(i)=='U'){up++;}else if(moves.charAt(i)=='D'){down++;}else if(moves.charAt(i)=='L'){left++;}else{right++;}}if(left==right&&up==down){return true;}return false;}
}