当前位置: 首页 > article >正文

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

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

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.

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;

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.


http://www.kler.cn/a/612610.html

相关文章:

  • OpenHarmony NativeC++应用开发speexdsp噪声消除案例
  • 基于动态光影融合的缺陷实时检测和材质量化方法,并且整合EventPS、VMNer和EvDiG
  • “十五五”时期航空弹药发展环境分析
  • 【Portainer】Docker可视化组件安装
  • 人工智能与无人机:无人机的进步与应用技术详解
  • 『不废话』之大模型性能评估
  • Springboot 集成 Flowable 6.8.0
  • Linux 上使用 Docker 部署 Kafka 集群
  • Brainstorm绘制功能连接图(matlab)
  • 浅聊web前端性能测试
  • [python]基于yolov10实现热力图可视化支持图像视频和摄像头检测
  • Go 语言常见错误——控制结构
  • Selenium Web自动化如何快速又准确的定位元素路径,强调一遍是元素路径
  • VMware Ubuntu 网络配置全攻略:从断网到畅通无阻
  • (UI自动化测试web端)第二篇:元素定位的方法_css定位之css选择器
  • 什么时候用到 JVM 调优,调优哪些参数
  • Android 计算已安装应用的大小
  • 闲聊IT - 面向服务架构(SOA)的发展历史
  • 北理工计算机考研复试上机2017年真题
  • HTML5 新的 Input 类型学习笔记