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

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

LeetCode刷题:657. Judge Route Circle

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

算法设计

package com.bean.basic;public class JudgeRouteCircle {    public static boolean judgeCircle(String moves) {        //设定两个变量x,y;分别表示平面图形的横坐标和纵坐标        int x = 0;        int y = 0;        //将字符串转化为字符数组后,进行迭代        for (char ch : moves.toCharArray()) {            /*             * 向上移动的方向,纵坐标 y--             * 向下移动的方向,纵坐标 y++             * 向左移动的方向,横坐标 x--             * 向右移动的方向,横坐标 x++             */            if (ch == 'U') y++;            else if (ch == 'D') y--;            else if (ch == 'R') x++;            else if (ch == 'L') x--;        }        // 路径形成一个环路的条件是:x==0 && y==0        return x == 0 && y == 0;    }    public static void main(String[] args) {        // TODO Auto-generated method stub        //String str="UD";        String str="LL";        if(judgeCircle(str)) {            System.out.println("True");        }else {            System.out.println("False");        }    }}

(完)

转载地址:http://uztdi.baihongyu.com/

你可能感兴趣的文章
VMware Workstation Pro虚拟机不可用解决方法
查看>>
最简单的使用redis自带程序实现c程序远程访问redis服务
查看>>
redis学习总结-- 内部数据 字符串 链表 字典 跳跃表
查看>>
iOS 对象序列化与反序列化
查看>>
iOS 序列化与反序列化(runtime) 01
查看>>
iOS AFN 3.0版本前后区别 01
查看>>
iOS ASI和AFN有什么区别
查看>>
iOS QQ侧滑菜单(高仿)
查看>>
iOS 扫一扫功能开发
查看>>
iOS app之间的跳转以及传参数
查看>>
iOS __block和__weak的区别
查看>>
Android(三)数据存储之XML解析技术
查看>>
Spring JTA应用之JOTM配置
查看>>
spring JdbcTemplate 的若干问题
查看>>
Servlet和JSP的线程安全问题
查看>>
GBK编码下jQuery Ajax中文乱码终极暴力解决方案
查看>>
Oracle 物化视图
查看>>
PHP那点小事--三元运算符
查看>>
解决国内NPM安装依赖速度慢问题
查看>>
Brackets安装及常用插件安装
查看>>