首页 > 其他分享 >leetcode-657-easy

leetcode-657-easy

时间:2022-11-04 19:13:13浏览次数:53  
标签:origin up move robot its 657 easy moves leetcode

Robot Return to Origin

There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.

You are given a string moves that represents the move sequence of the robot where moves[i] represents its ith move. Valid moves are 'R' (right), 'L' (left), 'U' (up), and 'D' (down).

Return true if the robot returns to the origin after it finishes all of its moves, or false otherwise.

Note: The way that the robot is "facing" is irrelevant. 'R' will always make the robot move to the right once, 'L' will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.

Example 1:

Input: moves = "UD"
Output: true
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
Example 2:

Input: moves = "LL"
Output: false
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
Constraints:

1 <= moves.length <= 2 * 104
moves only contains the characters 'U', 'D', 'L' and 'R'.

思路一:分别记录水平和垂直两个方向,+1 或者 -1

public boolean judgeCircle(String moves) {
    int ver = 0;
    int hor = 0;
    for (int i = 0; i < moves.length(); i++) {
        if (moves.charAt(i) == 'U') {
            hor++;
        } else if (moves.charAt(i) == 'D') {
            hor--;
        } else if (moves.charAt(i) == 'L') {
            ver++;
        } else {
            ver--;
        }
    }

    return ver == 0 && hor == 0;
}

标签:origin,up,move,robot,its,657,easy,moves,leetcode
From: https://www.cnblogs.com/iyiluo/p/16858843.html

相关文章

  • leetcode-1417-easy
    ReformatTheStringYouaregivenanalphanumericstrings.(AlphanumericstringisastringconsistingoflowercaseEnglishlettersanddigits).Youhaveto......
  • leetcode-771-easy
    JewelsandStonesYou'regivenstringsjewelsrepresentingthetypesofstonesthatarejewels,andstonesrepresentingthestonesyouhave.Eachcharacterin......
  • leetcode-1200-easy
    MinimumAbsoluteDifferenceGivenanarrayofdistinctintegersarr,findallpairsofelementswiththeminimumabsolutedifferenceofanytwoelements.Retu......
  • leetcode-1984-easy
    MinimumDifferenceBetweenHighestandLowestofKScoresYouaregivena0-indexedintegerarraynums,wherenums[i]representsthescoreoftheithstudent.......
  • EasyCVR国标GB28181协议接入下的TCP和UDP模式说明及差异
    有用户在使用我们的平台时,经常会出现对于端口的疑问,同时也不了解端口的差别。今天我们来解释说明下EasyCVR平台关于国标GB28181协议接入下的TCP和UDP模式的说明及差异。......
  • 视频融合平台EasyCVR各项数据正常,却无法用海康NVR接入是什么原因?
    EasyCVR视频融合云平台开放度高、兼容性强、可支持灵活拓展与第三方集成,目前已经成为安防市场主流的视频能力层服务平台。平台可支持多协议、多类型的设备接入,包括国标GB28......
  • 我用EasyExcel优化了公司的导出(附踩坑记录)
    背景介绍最近要改一个导出的功能,在原有的基础上,在导出一份明细数据,要求导出内容加在原有excel的第二个sheet上。考虑到数据量还比较大,干脆引入阿里的EasyExcel来做......
  • leetcode 160. 相交链表 js 实现
    给你两个单链表的头节点 headA 和 headB ,请你找出并返回两个单链表相交的起始节点。如果两个链表不存在相交节点,返回 null 。图示两个链表在节点 c1 开始相交: ......
  • leetcode-136. 只出现一次的数字
    题目描述给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。说明说明:你的算法应该具有线性时间复杂度。你可以不......
  • LeetCode刷题记录.Day5
    反转链表题目链接206.反转链表-力扣(LeetCode)classSolution{public:ListNode*reverseList(ListNode*head){ListNode*temp;ListNode*c......