博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
657. Judge Route Circle
阅读量:6877 次
发布时间:2019-06-26

本文共 1034 字,大约阅读时间需要 3 分钟。

题目描述:

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 解题思路: 统计输入字符串中字符U和D的个数是否一样,L和R个数是否一样,若都一样,则返回到起点。 代码:
1 class Solution { 2 public: 3     bool judgeCircle(string moves) { 4          5         int u = 0; 6         int d = 0; 7         int l = 0; 8         int r = 0; 9         u = std::count(moves.begin(), moves.end(), 'U');10         d = std::count(moves.begin(), moves.end(), 'D');11         l = std::count(moves.begin(), moves.end(), 'L');12         r = std::count(moves.begin(), moves.end(), 'R');13         return (u == d) && (l == r);   14     }15 };

 

转载于:https://www.cnblogs.com/gsz-/p/9385508.html

你可能感兴趣的文章
Java8 - 日期和时间实用技巧
查看>>
Java记录 -58- Iterator 迭代器
查看>>
RabbitMQ入门(5)--主题
查看>>
菜鸟如何使用GoEasy实现第一个web实时消息推送
查看>>
LNMMP架构的安装配置和功能的实现
查看>>
几个设置让你的邮箱不会爆满
查看>>
我的友情链接
查看>>
在linux6上安装RAC时多路径的权限设置
查看>>
[转载] 七龙珠第一部——第037话 忍者出现
查看>>
网络数据通信加密系统中加密解密流程
查看>>
PXE+KickStart无人值守安装RHEL
查看>>
十年,站酷已成设计论坛霸主,博客园却成无兵之将
查看>>
ansible安装
查看>>
使用bind搭建DNS服务器
查看>>
Windows server 2008R2 DHCP服务器
查看>>
计算机网络笔记--数据链路层(一)
查看>>
我的友情链接
查看>>
Java方法重载注意事项
查看>>
爱创课堂每日一题第五十九天- javascript继承的6种方法
查看>>
16.1 Tomcat介绍 16.2 安装jdk 16.3 安装Tomcat
查看>>