C# O(n)


  • 0
    H
    public int[] TwoSum(int[] nums, int target) {
            Dictionary<int, int> dict = new Dictionary<int, int>();
            
            for(int i = 0; i < nums.Length; i++) {
                if (dict.ContainsKey(target - nums[i])) return new int[] { dict[target - nums[i]], i };
                dict[nums[i]] = i;
            }
            
            return null;
        }
    

  • 0
    T

    Why dict[nums[i]] = i is OK while dict.Add(nums[i],i) is not


Log in to reply
 

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