Why [2, -1, 1, -2, -2] is not a loop?


  • 0
    D

    Why [2, -1, 1, -2, -2] is not a loop? And this is my solution:
    bool circularArrayLoop(vector<int>& nums) {
    if (nums.empty()) {
    return false;
    }
    int currentPos = 0;
    int n = nums.size();
    for (int i = 0; i < n; ++i) {
    // std::cout << "current: " << currentPos << " ";
    currentPos = (currentPos + nums[currentPos] + n) % n;
    // std::cout << " next: " << currentPos << std::endl;
    if (currentPos == 0 && i < n - 1) {
    return true;
    }
    }
    return currentPos == 0;
    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.