解法一

分别统计水平和竖直方向上移动的距离,均为0则返回原点。

  1. class Solution {
  2. public boolean judgeCircle(String moves) {
  3. int x = 0, y = 0;
  4. for (int i = 0; i < moves.length(); ++i) {
  5. switch (moves.charAt(i)) {
  6. case 'R':
  7. ++x;
  8. break;
  9. case 'L':
  10. --x;
  11. break;
  12. case 'U':
  13. ++y;
  14. break;
  15. case 'D':
  16. --y;
  17. break;
  18. }
  19. }
  20. if ((x == 0) && (y == 0)) {
  21. return true;
  22. } else {
  23. return false;
  24. }
  25. }
  26. }