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

Поделиться
HTML-код
  • Опубликовано: 29 окт 2024

Комментарии • 2

  • @Wjdveiwlslfbeb
    @Wjdveiwlslfbeb 2 года назад

    thank you so much! very clear and easy solution!

  • @jeroenvandenheuvel3869
    @jeroenvandenheuvel3869 2 года назад

    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
    ```