How about use two for loop?


  • 0
    E

    ...

        public int[] SearchRange(int[] nums, int target)
        {
            int begin = -1, end = -1;
    
            for (int i = 0; i < nums.Length; i++)
            {
                if (nums[i] == target)
                {
                    begin = i;
                    break;
                }
            }
    
            for (int i = nums.Length - 1; i >= 0; i--)
            {
                if (nums[i] == target)
                {
                    end = i;
                    break;
                }
            }
    
            return new int[] { begin, end };
        }
    

    ...


Log in to reply
 

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