Accepted C# O(n) Solution


  • 0
    F
    public class Solution {
        public int[] TwoSum(int[] nums, int target) {
            int[] result = new int[2];
            Dictionary<int, int> tmp = new Dictionary<int, int>();
            int i;
            for (i = 0; i < nums.Length && !tmp.ContainsKey(target - nums[i]); i++) {
                if (!tmp.ContainsKey(nums[i])) {
                    tmp.Add(nums[i], i);   
                }
            }
            result[0] = tmp[target - nums[i]];
            result[1] = i;
            return result;
        }
    }
    

Log in to reply
 

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