using System;
using System.Collections.Generic;
namespace MyCompiler {
class Program {
public static void Main(string[] args) {
var sol = new Solution();
int[] nums = new int[] {1,2,3,4,5};
Console.WriteLine("input: [{0}]", string.Join(", ", nums));
for (var target = 0; target < 12; target++) {
int[] output = sol.TwoSum(nums, target);
Console.WriteLine($"target={target} : " + "[{0}]", string.Join(", ", output));
}
}
}
class Solution {
public int[] TwoSum(int[] nums, int target) {
var prevMap = new Dictionary<int, int>();
for (var i = 0; i < nums.Length; i++) {
int diff = target - nums[i];
if (prevMap.ContainsKey(diff)) {
return new int[] {prevMap[diff], i};
}
prevMap[nums[i]] = i;
}
return new int[] {};
}
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: