返回介绍

solution / 0400-0499 / 0457.Circular Array Loop / README_EN

发布于 2024-06-17 01:04:00 字数 5901 浏览 0 评论 0 收藏 0

457. Circular Array Loop

中文文档

Description

You are playing a game involving a circular array of non-zero integers nums . Each nums[i] denotes the number of indices forward/backward you must move if you are located at index i :

  • If nums[i] is positive, move nums[i] steps forward, and
  • If nums[i] is negative, move nums[i] steps backward.

Since the array is circular, you may assume that moving forward from the last element puts you on the first element, and moving backwards from the first element puts you on the last element.

A cycle in the array consists of a sequence of indices seq of length k where:

  • Following the movement rules above results in the repeating index sequence seq[0] -> seq[1] -> ... -> seq[k - 1] -> seq[0] -> ...
  • Every nums[seq[j]] is either all positive or all negative.
  • k > 1

Return true _ if there is a cycle in _ nums _, or _ false _ otherwise_.

Example 1:

Input: nums = [2,-1,1,2,2]
Output: true
Explanation: The graph shows how the indices are connected. White nodes are jumping forward, while red is jumping backward.
We can see the cycle 0 --> 2 --> 3 --> 0 --> ..., and all of its nodes are white (jumping in the same direction).

Example 2:

Input: nums = [-1,-2,-3,-4,-5,6]
Output: false
Explanation: The graph shows how the indices are connected. White nodes are jumping forward, while red is jumping backward.
The only cycle is of size 1, so we return false.

 

Constraints:

  • 1 <= nums.length <= 5000
  • -1000 <= nums[i] <= 1000
  • nums[i] != 0

Follow up: Could you solve it in O(n) time complexity and O(1) extra space complexity?

Solutions

Solution 1

class Solution:
  def circularArrayLoop(self, nums: List[int]) -> bool:
    n = len(nums)

    def next(i):
      return (i + nums[i] % n + n) % n

    for i in range(n):
      if nums[i] == 0:
        continue
      slow, fast = i, next(i)
      while nums[slow] * nums[fast] > 0 and nums[slow] * nums[next(fast)] > 0:
        if slow == fast:
          if slow != next(slow):
            return True
          break
        slow, fast = next(slow), next(next(fast))
      j = i
      while nums[j] * nums[next(j)] > 0:
        nums[j] = 0
        j = next(j)
    return False
class Solution {
  private int n;
  private int[] nums;

  public boolean circularArrayLoop(int[] nums) {
    n = nums.length;
    this.nums = nums;
    for (int i = 0; i < n; ++i) {
      if (nums[i] == 0) {
        continue;
      }
      int slow = i, fast = next(i);
      while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(fast)] > 0) {
        if (slow == fast) {
          if (slow != next(slow)) {
            return true;
          }
          break;
        }
        slow = next(slow);
        fast = next(next(fast));
      }
      int j = i;
      while (nums[j] * nums[next(j)] > 0) {
        nums[j] = 0;
        j = next(j);
      }
    }
    return false;
  }

  private int next(int i) {
    return (i + nums[i] % n + n) % n;
  }
}
class Solution {
public:
  bool circularArrayLoop(vector<int>& nums) {
    int n = nums.size();
    for (int i = 0; i < n; ++i) {
      if (!nums[i]) continue;
      int slow = i, fast = next(nums, i);
      while (nums[slow] * nums[fast] > 0 && nums[slow] * nums[next(nums, fast)] > 0) {
        if (slow == fast) {
          if (slow != next(nums, slow)) return true;
          break;
        }
        slow = next(nums, slow);
        fast = next(nums, next(nums, fast));
      }
      int j = i;
      while (nums[j] * nums[next(nums, j)] > 0) {
        nums[j] = 0;
        j = next(nums, j);
      }
    }
    return false;
  }

  int next(vector<int>& nums, int i) {
    int n = nums.size();
    return (i + nums[i] % n + n) % n;
  }
};
func circularArrayLoop(nums []int) bool {
  for i, num := range nums {
    if num == 0 {
      continue
    }
    slow, fast := i, next(nums, i)
    for nums[slow]*nums[fast] > 0 && nums[slow]*nums[next(nums, fast)] > 0 {
      if slow == fast {
        if slow != next(nums, slow) {
          return true
        }
        break
      }
      slow, fast = next(nums, slow), next(nums, next(nums, fast))
    }
    j := i
    for nums[j]*nums[next(nums, j)] > 0 {
      nums[j] = 0
      j = next(nums, j)
    }
  }
  return false
}

func next(nums []int, i int) int {
  n := len(nums)
  return (i + nums[i]%n + n) % n
}

 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文