Day i do not know

This commit is contained in:
Electrominch
2022-10-18 00:45:31 +03:00
parent 01c5720116
commit 284b6ff10c
72 changed files with 1256 additions and 12 deletions

View File

@@ -4,10 +4,27 @@ public class Solution
{
public 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 };
Dictionary<int, int> dict = new Dictionary<int, int>();
int[] halfIndexes = new int[] { -1, -1 };
int curHalfIndex = 0;
for(int i = 0; i < nums.Length; i++)
{
if (dict.ContainsKey(nums[i]) == false)
dict.Add(nums[i], i);
if (nums[i] == target / 2)
{
halfIndexes[curHalfIndex++] = i;
if (target % 2 == 0 && curHalfIndex == 2)
return halfIndexes;
}
}
foreach (var kp in dict)
{
int need = target - kp.Key;
if (dict.ContainsKey(need) && (target % 2 == 1 || need != target/2))
return new int[] { kp.Value, dict[need] };
}
return null;
}
}