Test case missing


  • 1
    Z

    The following ill-code has passed all the existing cases and was accepted.
    But it will output the wrong answer false when input is [1, 1, 2], when the expected answer is true.
    I would suggest add a new test [1, 1, 2] in system. Thanks.

    // Copyright 2016 Qi Wang
    // Date: 2016-11-13
    class Solution {
     public:
      bool circularArrayLoop(vector<int>& nums) {
        for (int i = 0; i < nums.size(); ++i) {
          if (nums[i] == 0) continue;
          int begin = i;
          bool forward = nums[i] > 0;
          int cnt = 0;
          for (int j = i; nums[j] && forward == nums[j] > 0; ++cnt) {
            int next = (j + nums[j]) % static_cast<int>(nums.size());
            if (next < 0) next += nums.size();
            if (next == begin) {
              if (cnt > 0) {
                return true;
              } else {
                break;
              }
            } else {
              nums[j] = 0;
              j = next;
            }
          }
        }
        return false;
      }
    };
    

Log in to reply
 

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