My C# code which defeats 67.8% csharp submissions


  • 0
    E
    public class Solution
    {
        public int[] TwoSum(int[] nums, int target)
        {
            Dictionary<int, int> test = new Dictionary<int, int>();
            for (int i = 0; i < nums.Length; i++)
            {
                if (test.ContainsKey(nums[i]))
                    return new int[] { test[nums[i]] + 1, i + 1 };
                else
                {
                    if(!test.ContainsKey(target - nums[i]))
                        test.Add(target - nums[i], i);
                }
            }
            return new int[2];
        }
    }

Log in to reply
 

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