C# code for Two Sum


  • 0
    C
    public class Solution {
        public int[] TwoSum(int[] nums, int target) {
            int[] result = new int[2];
            Dictionary<int,int> id = new Dictionary<int,int>();
            
            for(int i =0; i< nums.Length; i++)
            {
                if(id.ContainsValue(target - nums[i]))
                {
                    result[1] = i;
                    foreach (var key in id.Keys)
                        {
                            if (id[key] == target - nums[i])
                                result[0] = key;
                        }
                    
                }
                id[i] = nums[i];
            }
            return result;
                }
                }
            
    
    My C# code fails for a very huge dataset, the for loops shouldnt be effecting much as I iterate over the keys. what could the potential problem be.

Log in to reply
 

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