机器人能否回到原点 - 简单

*************

C++

topic:657. 机器人能否返回原点 - 力扣(LeetCode)

*************

inspect the topic very first.

|----------------------------------------------------------------------------|
| |

It is letters to decide which side the robot moves. And my thought is quite sample. Assumeing the robot can move back to the origin, then the numbers of L have to be the same with the numbers of R. So are U and D.

Then the code can be easy to write. Travel through the string and count the letters.

cpp 复制代码
class Solution {
public:
    bool judgeCircle(string moves) {
        
        int n = moves.size(); // to figure out the size at first
        int countR = 0;
        int countL = 0;
        int countU = 0;
        int countD = 0;

        for (int i = 0; i < n; i++)
        {
            if (moves[i] == 'R')
            {
                countR = countR + 1;
            }
            else if (moves[i] == 'L')
            {
                countL = countL + 1;
            }
            else if (moves[i] == 'U')
            {
                countU = countU + 1;
            }
            else
            {
                countD++;
            }
        }

        if (countR == countL && countD == countU)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
};

|----------------------------------------------------------------------------|
| |

However, I donot think it is a elegent code. If you gays finish the Nine-year compulsory education,you will know coordinate system.

|-----------------------------------------------------------------------------|
| |

↑ y = y + 1;

↓ y = y - 1;

← x = x - 1;

→ x = x + 1;

cpp 复制代码
class Solution {
public:
    bool judgeCircle(string moves) {
        int x = 0;
        int y = 0;

        for (char c : moves) 
        {
            if (c == 'U') y++;
            else if (c == 'D') y--;
            else if (c == 'R') x++;
            else if (c == 'L') x--;
        }
        
        return x == 0 && y == 0;
    }
};

|----------------------------------------------------------------------------|
| |

It is do elegent.

Maybe apple would change the design of ios in the near future. I donot care whether the ios system is fluently or not, I just want to see what desigh looks.

|----------------------------------------------------------------------------|
| |

相关推荐
t198751282 分钟前
基于MATLAB的线性判别分析(LDA)降维算法实现方案
开发语言·算法·matlab
仰泳的熊猫11 分钟前
1108 Finding Average
数据结构·c++·算法·pat考试
老赵聊算法、大模型备案18 分钟前
2025 年 12 月北京市生成式人工智能服务备案分析:政务场景再扩容,合规生态更聚焦
人工智能·算法·microsoft·aigc·政务
liuyao_xianhui18 分钟前
山脉数组的峰顶索引_优选算法_二分查找法
算法
炽烈小老头22 分钟前
【每天学习一点算法 2025/12/15】环形链表
学习·算法·链表
点云SLAM24 分钟前
算法复杂度分析之——空间复杂度分析和标准库算法与容器操作的复杂度实际案例分析(3)
算法·深度优先·空间复杂度·算法性能分析·标准库算法
Liangwei Lin27 分钟前
洛谷 P5788 【模板】单调栈
算法
京东零售技术28 分钟前
ACL 2025 | 一种用于电子商务query意图分类的半监督可扩展统一框架
算法
AA陈超1 小时前
虚幻引擎5 GAS开发俯视角RPG游戏 P07-18.生成火球术
c++·游戏·ue5·游戏引擎·虚幻
wxin_VXbishe1 小时前
springboot居家养老管理系统-计算机毕业设计源码55953
java·c++·spring boot·python·spring·django·php