C# One Pass O(n) Solution (Beats 90%)


  • 0
    K
    public class Solution {
        public int[] TwoSum(int[] nums, int target) 
        {
            // 10:20 PM - 10:24 PM
            
            var dict = new Dictionary<int,int>();
            for(int i = 0; i < nums.Count(); ++i)
            {
                if (dict.ContainsKey(nums[i])){
                    return new int[]{i,dict[nums[i]]};
                }
                
                dict[target - nums[i]] =  i;
            }
            return null;
        }
    }

  • 0
    P
    This post is deleted!

Log in to reply
 

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