[Java] Leetcode 1041. Robot Bounded In Circle [Array #10]

Sdílet
Vložit
  • čas přidán 24. 07. 2024

Komentáře • 2

  • @hyonsokna7244
    @hyonsokna7244 Před 2 lety

    thank you so much! very clear and easy solution!

  • @jeroenvandenheuvel3869

    Thanks for the explanation! Although this solution works well it's a bit more complex than necessary. What about using cosine and sine?
    ```
    x = 0
    y = 0
    rotation = 0
    for instruction in instructions:
    if instruction == "L":
    rotation += 270
    rotation = rotation % 360
    elif instruction == "R":
    rotation += 90
    rotation = rotation % 360
    else:
    y -= int(math.cos(math.radians(rotation)))
    x += int(math.sin(math.radians(rotation)))
    if x == 0 and y == 0:
    return True
    elif rotation != 0:
    return True
    return False
    ```
    Or without these functions the movement can be done like this:
    ```
    if rotation % 180 == 0:
    y += (rotation / 90) - 1
    else:
    x -= (rotation / 90) - 2
    ```