首页 > 其他分享 >LeetCode 1041. Robot Bounded In Circle

LeetCode 1041. Robot Bounded In Circle

时间:2024-08-03 12:39:19浏览次数:15  
标签:direction move 1041 robot Direction Bounded Position LeetCode instructions

原题链接在这里:https://leetcode.com/problems/robot-bounded-in-circle/description/

题目:

On an infinite plane, a robot initially stands at (0, 0) and faces north. Note that:

  • The north direction is the positive direction of the y-axis.
  • The south direction is the negative direction of the y-axis.
  • The east direction is the positive direction of the x-axis.
  • The west direction is the negative direction of the x-axis.

The robot can receive one of three instructions:

  • "G": go straight 1 unit.
  • "L": turn 90 degrees to the left (i.e., anti-clockwise direction).
  • "R": turn 90 degrees to the right (i.e., clockwise direction).

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

 

Example 1:

Input: instructions = "GGLLGG"
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (0, 2). Direction: South.
"G": move one step. Position: (0, 1). Direction: South.
"G": move one step. Position: (0, 0). Direction: South.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (0, 2) --> (0, 1) --> (0, 0).
Based on that, we return true.

Example 2:

Input: instructions = "GG"
Output: false
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"G": move one step. Position: (0, 2). Direction: North.
Repeating the instructions, keeps advancing in the north direction and does not go into cycles.
Based on that, we return false.

Example 3:

Input: instructions = "GL"
Output: true
Explanation: The robot is initially at (0, 0) facing the north direction.
"G": move one step. Position: (0, 1). Direction: North.
"L": turn 90 degrees anti-clockwise. Position: (0, 1). Direction: West.
"G": move one step. Position: (-1, 1). Direction: West.
"L": turn 90 degrees anti-clockwise. Position: (-1, 1). Direction: South.
"G": move one step. Position: (-1, 0). Direction: South.
"L": turn 90 degrees anti-clockwise. Position: (-1, 0). Direction: East.
"G": move one step. Position: (0, 0). Direction: East.
"L": turn 90 degrees anti-clockwise. Position: (0, 0). Direction: North.
Repeating the instructions, the robot goes into the cycle: (0, 0) --> (0, 1) --> (-1, 1) --> (-1, 0) --> (0, 0).
Based on that, we return true.

Constraints:

  • 1 <= instructions.length <= 100
  • instructions[i] is 'G''L' or, 'R'.

题解:

If there is a circle, it must be one of the two conditions.

1. The robot comes back to origin, and no matter which directions it is facing. Apparently there is a circle.

2. The robot final diretion is not north, then after another 1 or 3 repeated moves, there is a circle.

Time Complexity: O(n). n = instructions.length().

Space: O(1).

AC Java:

 1 class Solution {
 2     public boolean isRobotBounded(String instructions) {
 3         int[][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
 4         int x = 0;
 5         int y = 0;
 6         int ind = 0;
 7         for(int i = 0; i < instructions.length(); i++){
 8             char c = instructions.charAt(i);
 9             if(c == 'R'){
10                 ind = (ind + 1) % 4;
11             }else if(c == 'L'){
12                 ind = (ind + 3) % 4;
13             }else{
14                 x += dirs[ind][0];
15                 y += dirs[ind][1];
16             }
17         }
18 
19         return (x == 0 && y == 0) || ind != 0;
20     }
21 }

 

标签:direction,move,1041,robot,Direction,Bounded,Position,LeetCode,instructions
From: https://www.cnblogs.com/Dylan-Java-NYC/p/18340326

相关文章

  • LeetCode 1017. Convert to Base -2
    原题链接在这里:https://leetcode.com/problems/convert-to-base-2/description/题目:Givenaninteger n,return abinarystringrepresentingitsrepresentationinbase -2.Note thatthereturnedstringshouldnothaveleadingzerosunlessthestringis "0".......
  • LeetCode | 370 RangeAddition
    https://github.com/dolphinmind/datastructure/tree/datastructure-array-02分析数组本身的递归性,差分数组的不变性和可逆性,在left索引上对当前及后续所有元素产生影响,在right+1索引上进行反操作,消除这种影响,再进行还原即可数组本身具有递归性差分数组性质:对于任何常数c......
  • LeetCode 热题 HOT 100 (015/100)【宇宙最简单版】
    【栈】No.0155最小栈【中等】......
  • leetcode 2181.合并零之间的结点
    1.题目要求:/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*mergeNodes(structListNode*head){structListNode*cur=head;intcount=0;//1.遍历结......
  • leetcode 2181.合并零之间的结点
    1.题目要求:/***Definitionforsingly-linkedlist.*structListNode{*intval;*structListNode*next;*};*/structListNode*mergeNodes(structListNode*head){structListNode*cur=head;intcount=0;//1.遍历结......
  • LeetCode | 303 RangeSumQueryImmutable
    https://github.com/dolphinmind/datastructure/tree/datastructure-array-02分析所求解的区间[left,right]具有连续性,执行常规for循环计算,[0,left-1]的区间元素累加和与[0,right]的区间元素累加和,有重复的运算区间[0,left)。累加和与长跑比赛其实一致,求取[left,right]区......
  • 【代码随想录训练营第42期 Day17打卡 二叉树Part5-LeetCode 654.最大二叉树 617.合并
    目录一、做题心得二、题目与题解题目一:654.最大二叉树题目链接题解:递归题目二:617.合并二叉树题目链接题解:递归(前序遍历)题目三:617.合并二叉树题目链接题解:BFS层序遍历 题目四:98.验证二叉搜索树题目链接题解:递归(中序遍历)三、小结一、做题心得今天是代码随想......
  • LeetCode 152 乘积最大子数组
    题目描述给你一个整数数组nums,请你找出数组中乘积最大的非空连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。测试用例的答案是一个32位整数。思路这一题用普通的连续子数组思路求解时有一个问题:子问题的最优解不一定是总体的最优局部解。也就是不满足最优......
  • 【leetcode232:用栈实现队列】
    leetcode232:用栈实现队列题目:请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):实现MyQueue类:voidpush(intx)将元素x推到队列的末尾intpop()从队列的开头移除并返回元素intpeek()返回队列开头的元素booleanemp......
  • LeetCode | 59 SpiralMatrixII
    主类https://github.com/dolphinmind/datastructure/tree/datastructure-array-02循环不变量原则,保证问题边界的逻辑一致性(从二分法的启发)初始位置旋转圈数奇偶性四条边的边界逻辑offsetpackagecom.github.dolphinmind.array;/***@authordolphinmind*@C......