Leetcode 657. Robot Return to Origin | | Leetcode 657. Robot Return to Origin 文章作者:Tyan博客:noahsnail.com | CSDN | 简书 1. Description 2. Solution Version 1 12345678910111213141516171819202122class Solution {public: bool judgeCircle(string moves) { int vertical = 0; int horizontal = 0; for(char ch : moves) { if(ch == 'L') { horizontal--; } else if(ch == 'R') { horizontal++; } else if(ch == 'U') { vertical++; } else { vertical--; } } return vertical == 0 && horizontal == 0; }}; Version 2 123456789101112131415161718192021222324class Solution {public: bool judgeCircle(string moves) { int vertical = 0; int horizontal = 0; for(char ch : moves) { switch(ch) { case 'L': horizontal--; break; case 'R': horizontal++; break; case 'U': vertical++; break; case 'D': vertical--; break; } } return vertical == 0 && horizontal == 0; }}; Reference https://leetcode.com/problems/robot-return-to-origin/description/ 如果有收获,可以请我喝杯咖啡! 赏 微信打赏 支付宝打赏