C# solution


  • 0
    public class Solution {
        public int[] SearchRange(int[] nums, int target) 
        {
            if(nums.Length == 0)
            {
                return new int[2]{-1,-1};
            }
            List<int> result = new List<int>();
            
            for(int i = 0;i<nums.Length;i++)
            {
                if(nums[i] == target)
                {
                    result.Add(i);
                }
            }
            
            if(result.Count > 1)
            {
              return new int[2] {result[0],result[result.Count -1]};
            }
            else if(result.Count >0)
            {
                 return new int[2]{result[0],result[0]};
            }
            else
            {
                return new int[2]{-1,-1};
            }
        }
    }
    

Log in to reply
 

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