C# solution by checking 0s in array


  • 0
    Y
    //unit test [],[1],[0],[1,0,1],[1,0]
    
    public class Solution {
        public bool CanJump(int[] nums) {
            //find all the 0s, and check whether can cross this 0
            int n=nums.Length;
            if(n==0)
                return false;
            if(n==1)
                return true;
            List<int> zInd=new List<int>();
            for(int i=0;i<n;i++)
            {
                if(nums[i]==0)
                    zInd.Add(i);
            }
            for(int i=0;i<zInd.Count;i++)
            {
                if(!canCross(nums,zInd[i]))
                    return false;
            }
            return true;
        }
        private bool canCross(int[] nums, int ind)
        {
            for(int i=ind-1;i>=0;i--)
            {
                if(nums[i]>ind-i||((nums[i]>=ind-i)&&ind==nums.Length-1))
                    return true;
            }
            return false;
        }
    }

Log in to reply
 

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