Skip to content

Instantly share code, notes, and snippets.

@jbasinger
Created January 27, 2021 01:28
Show Gist options
  • Save jbasinger/e67b70cb504773c5cda387e41bee3f9f to your computer and use it in GitHub Desktop.
Save jbasinger/e67b70cb504773c5cda387e41bee3f9f to your computer and use it in GitHub Desktop.
Leet Code Problem 1 Two Sum
namespace LeetCode
{
public class TwoSum_Problem
{
public static int[] TwoSum(int[] nums, int target)
{
for (int i = 0; i < nums.Length; i++)
{
for (int j = i+1; j < nums.Length; j++)
{
if ((nums[i] + nums[j]) == target)
{
return new int[] {i, j};
}
}
}
return new int[] { };
}
}
}
namespace LeetCode.Tests
{
public class Tests
{
[Test]
public void ShouldReturnSumOfTarget()
{
var res = TwoSum_Problem.TwoSum(new int[] {2, 7, 11, 15}, 9);
res[0].ShouldBe(0);
res[1].ShouldBe(1);
res = TwoSum_Problem.TwoSum(new int[] {3,2,4}, 6);
res[0].ShouldBe(1);
res[1].ShouldBe(2);
res = TwoSum_Problem.TwoSum(new int[] {3,3}, 6);
res[0].ShouldBe(0);
res[1].ShouldBe(1);
res = TwoSum_Problem.TwoSum(new int[] {3,2,3}, 6);
res[0].ShouldBe(0);
res[1].ShouldBe(2);
res = TwoSum_Problem.TwoSum(new int[] {0,2,3,8,5,22,11,6}, 9);
res[0].ShouldBe(2);
res[1].ShouldBe(7);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment