Missing test case [1, 2, -1, 2], got AC with wrong code


  • 0
    W

    My friend got AC with his code as below:

    class Solution {
    public:
        bool circularArrayLoop(vector<int>& nums) {
            int size = nums.size();
            for (auto& n : nums) {
                n = n % size;
            }
            for (int i = 0; i < size; i++) {
                int sum = 0;
                int j = i;
                while (nums[j] != 0) {
                    if (sum * nums[j] < 0) {
                        sum = nums[j];
                    } else {
                        sum += nums[j];
                    }
                    int k = j;
                    j = (size + (j + nums[j]) % size) % size;
                    nums[k] = 0;
                }
                if (sum != 0 && sum % size  == 0) {
                    return true;
                }
            }
            return false;
        }
    };
    

    But I construct a bad case: [1, 2, -1, 2], which has a tailed circle instead of a full circle. I think cases like that should be added.

    Thanks!


Log in to reply
 

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