Missing Test Case [2, 2, -1, 1]


  • 0
    G

    The test case [2, 2, -1, 1] should be included, otherwise the following solution would pass the OJ but actually failed on this missing one.

    // Wrong at test case [2, 2, -1, 1], return true but actually should return false
    class Solution {
    public:
        bool circularArrayLoop(vector<int>& nums) {
            unordered_map<int, int> m;
            int n = nums.size();
            vector<bool> visited(n, false);
            for (int i = 0; i < n; ++i) {
                if (visited[i]) continue;
                int cur = i;
                while (true) {
                    visited[cur] = true;
                    int next = (cur + nums[cur]) % n;
                    if (next < 0) next += n;
                    if (next == cur) break;
                    if (m.count(next) && nums[next] * nums[cur] > 0) return true;
                    if (visited[next]) break;
                    m[cur] = next;
                    cur = next;
                }
            }
            return false;
        }
    };
    

Log in to reply
 

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