My C# submission which beats 97.12% once


  • 3
    Q
    public int[] TwoSum(int[] nums, int target) 
    {
        int[] RetIndecis = {0,0};
        Hashtable hsNums = new Hashtable();
        hsNums.Clear();
        //i is the latter index
        for(int i=0;i<nums.Length;i++)
        {
            int targetKey = target - nums[i];
            //if targetKey exist
            if(hsNums.ContainsKey(targetKey))
            {
                RetIndecis[1] = i + 1;
                RetIndecis[0] = (int)hsNums[targetKey];
                return RetIndecis;
            }
            //key is the number and value is the index,filter the number which has existed
            if(!hsNums.ContainsKey(nums[i]))
                hsNums.Add(nums[i],i + 1);
        }
        return(RetIndecis);
    }

Log in to reply
 

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