【Lintcode】1104. Judge Route Circle
题目地址:
https://www.lintcode.com/problem/judge-route-circle/description
在平面直角坐标系里,以方向给一个出发点在(0,0)(0,0)(0,0)的机器人发指令。问它的路径是否构成一个环。其实就是判断是否回到了原点。代码如下:
public class Solution {/*** @param moves: a sequence of its moves* @return: if this robot makes a circle*/public boolean judgeCircle(String moves) {// Write your code hereint U = 0, R = 0;for (int i = 0; i < moves.length(); i++) {char m = moves.charAt(i);if (m == 'U') {U++;} else if (m == 'D') {U--;} else if (m == 'R') {R++;} else {R--;}}return U == 0 && R == 0;}
}
时间复杂度O(n)O(n)O(n),空间O(1)O(1)O(1)。
【Lintcode】1104. Judge Route Circle相关推荐
- 【HDU】5343 MZL's Circle Zhou【后缀自动机】
传送门:[HDU]5343 MZL's Circle Zhou 对于a串可能和b串重复的部分,我们总能找到一个位置,使得a串达到最长,即a串的后继为空,所以我们只要预处理以字符x为开头的b串的个数即可 ...
- 【HUSTOJ】Online Judge
HUSTOJ Online Judge [环境] 操作系统:Ubuntu16 更换阿里云源 sudo cp /etc/apt/sources.list /etc/apt/sources.list.ba ...
- 【LintCode】算法题 1443. 最长AB子串
描述 给你一个只由字母'A'和'B'组成的字符串s,找一个最长的子串,要求这个子串里面'A'和'B'的数目相等,输出该子串的长度. 这个子串可以为空. s的长度n满足 2<=n<=1000 ...
- 【lintcode】树形数据结构之Maxtree, Tree iterator, remove bst node, 优先队列之动态中位数Median, 矩阵dfs之word search II,最大连
解析 max ksubarray sum: 最大和 of 连续子序列 => 最大和 of k份连续子序列 属于dp,30行代码搞定,注意一些边界. substr diff: 无queu ...
- LeetCode 657. Judge Route Circle
题目: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this rob ...
- [LeetCode]657.Judge Route Circle
题目描述: Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this r ...
- 【项目】Online Judge(在线评判系统)
Online Judge(在线评判系统) 文章目录 Online Judge(在线评判系统) 前言 一.Online Judge是什么? 二.通过这个项目能够学到什么? 1.C++ 2.网络编程 3. ...
- 【Lintcode】1413. Tree
题目地址: https://www.lintcode.com/problem/tree/description 给定一个无向图,其有树性质,并且顶点编号为111的为树根(也就是这个图是个有根树).要求 ...
- 【Lintcode】1645. Least Subsequences
题目地址: https://www.lintcode.com/problem/1645/ 给定一个长nnn的数组AAA,问AAA最少能分解为多少个严格递减的子序列之并. 在AAA上定义偏序关系< ...
最新文章
- HTML-加速、再加速
- 关于多人操作数据的处理策略
- mybatis-MyBatis-Plus
- MySQL 出现 The table is full 的解决方法
- 【BZOJ1188】分裂游戏(博弈论)
- Collections.min()和Collections.max()的使用
- sql express 无法启动服务_在Windows2012下安装SQL Server 2005无法启动服务的解决办法...
- 微信小程序 界面从右边滑出_微信小程序MUI侧滑导航菜单示例(Popup弹出式,左侧不动,右侧滑动)...
- Codeforces Round #205 (Div. 2) : D
- Java如何读指定行
- gin c.Next()方法
- FD.io VPP 20.09版本正式发布:往期VPP文章回顾+下载地址+相关链接
- HTML5与CSS3权威指南.pdf7
- vscode 搭建SSM项目环境
- Tableau可视化---Tableau简介
- C# 如何批量删除Excel单元格中的公式只保留数据
- Java 中status意思_struts的status属性
- 移动硬盘如何合并磁盘
- caj转word免费转换怎么操作?
- 码云上开源JAVA项目收藏