657. Judge Route Circle

网友投稿 523 2022-11-11

657. Judge Route Circle

657. Judge Route Circle

Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place.

The move sequence is represented by a string. And each move is represent by a character. The valid robot moves are R (Right), L (Left), U (Up) and D (down). The output should be true or false representing whether the robot makes a circle.

Example 1:

Input: "UD"Output: true

Example 2:

Input: "LL"Output: false

class Solution { public boolean judgeCircle(String moves) { int v = 0, h = 0; for (char move : moves.toCharArray()) { switch (move) { case 'U': v++; break; case 'D': v--; break; case 'R': h++; break; case 'L': h--; break; } } return v == 0 && h == 0; }}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:661. Image Smoother
下一篇:669. Trim a Binary Search Tree
相关文章

 发表评论

暂时没有评论,来抢沙发吧~