Java O(n) time O(1) space; Marking visited with 0


  • 0
    R
    public class Solution {
        public boolean circularArrayLoop(int[] nums) {
            for(int i=0;i<nums.length;i++){
                int track=i;
                int init=nums[i];
                while(!(((nums.length+nums[track]) % (nums.length) == 0) || ((nums[track]>0)!=(init>0)))) 
                {
                    int track_temp=(nums.length+nums[track]+track)%(nums.length);
                    nums[track]=0;
                    track=track_temp;
                    if(track==i){
                        return true;
                    }
                }
            }
            return false;
        }
    }

Log in to reply
 

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