Java solution - hoping it to be O(n) time, please comment if its not


  • 0
    S

    public class Solution {
    public boolean circularArrayLoop(int[] nums) {
    int len = nums.length;

        if (len == 0)
            return false;
        
        for (int i = 0 ; i < len; i++){
            int start = i;
            int steps = nums[i];
            boolean forward = steps>0;
            while (true ){
                if (nums[start] == 0 )
                    return false;
                nums[start] = 0;
                start = (start+steps+len)%len;
                if (start == i && forward == steps>0)
                    return true;
                steps = nums[start];
            }
        }
        return false;
    }
    

    }


Log in to reply
 

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