Error when using C# Array.FindIndex


  • 0
    A

    Trying to solve with using Array.FindIndex(), but it come out with 'Time Limit Exceeded' error. However, it get the answer correctly.

    public int[] TwoSum(int[] nums, int target)
    {
        for (int i = 0; i <= nums.Length; i++)
        {
            int k = Array.FindIndex(nums, i + 1, index => index == target - nums[i]);
            if (k != -1)
            {
                return new int[2] { i, k };
            }
        }
        throw new Exception();
    }
    

Log in to reply
 

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