My cpp solution 0ms o(1) space o(n) time


  • -1
    H
    class Solution {
    public:
    	bool circularArrayLoop(vector<int>& nums) {
    		int n = nums.size(), count = 0;
    		if(n==0)
    		    return false;
    		bool isMoreThanN = false;
    		for (int i = 0; i < n; i++) {
    			if (nums[0] < 1)
    				return false;
    			if(nums[i]==0)
    			    return false;
    		}
    		if (n == 1)
    			return true;
    		int i = 0;
    		while (count <= n) {
    			if (i < 0)
    				i = n - i%n;
    			if (i >= n) {
    				i = i%n;
    				isMoreThanN = true;
    			}
    			if (i == 0&&isMoreThanN)
    				return true;
    			i += nums[i];
    			count++;
    		}
    		return false;
    	}
    };
    

  • 0
    A

    @huangweiwei said in My cpp solution 0ms o(1) space o(n) time:

    	for (int i = 0; i < n; i++) {
    		if (nums[0] < 1)
    			return false;
    

    Solution is wrong.

    Your input
    [-1, -1, -1]

    Your answer
    false

    Expected answer
    true


Log in to reply
 

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