Easy Java Solution


  • 0
    R
    public class Solution {
        public boolean circularArrayLoop(int[] nums) {
            if(nums == null || nums.length == 0) return false;
            int len = nums.length;
            
            for(int i = 0; i<nums.length; i++) {
                int k = i;
                boolean forward = (nums[i] >= 0);
                int count = 1;
                
                while(nums[k] % len != 0) {
                    if((nums[k] >= 0 && !forward) || (nums[k] < 0 && forward)) break;
                    int pre = k;
                    k = (k+nums[k]) % len;
                    if(k < 0) k = k+len;
    
                    count++;
                    nums[pre] = 0;
                }
                if(count > 1 && k == 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.