How to improve this C# code? it takes 500 ms


  • 0
    S

    public class Solution {
    public int[] TwoSum(int[] nums, int target) {
    int[] result = new int[2];

        if(nums.Count() >= 2)
        {
            Hashtable hash = new Hashtable();
            for(int k = 0; k < nums.Count(); k++)
            {
                if(hash.ContainsKey(target - nums[k])) 
                {
                    result[0] = (int)hash[target - nums[k]];
                    result[1] = k + 1;
                    return result;
                }
                else
                {
                    hash[nums[k]] = k + 1;
                }
                
            }
           
        }
         return result;
    }
    

    }


Log in to reply
 

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