-
Notifications
You must be signed in to change notification settings - Fork 0
/
TwoSum.cs
53 lines (46 loc) · 1.49 KB
/
TwoSum.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
namespace LeetCodeTests;
public class Solution_TwoSum
{
public int[] TwoSum(int[] nums, int target)
{
var result = new int[2];
//var withIndices = nums.Select((item, i) => new { number = item, index = i }).ToArray();
//var substracted = withIndices.Select(x => new { number = target - x.number, index = x.index}).ToArray();
var withIndices = nums.Select((item, i) => new { number = item, index = i });
var substracted = withIndices.Select(x => new { number = target - x.number, index = x.index});
foreach (var item in withIndices)
{
var found = -1;
foreach (var x in substracted){
if(x.number == item.number && x.index != item.index){
found = x.index;
break;
}
}
if (found > -1)
{
result[0] = item.index;
result[1] = found;
break;
}
}
/*
for (int i = 0; i < withIndices.Length; i++)
{
var found = -1;
foreach (var x in substracted){
if(x.number == withIndices[i].number && x.index != withIndices[i].index){
found = x.index;
break;
}
}
if (found > -1)
{
result[0] = withIndices[i].index;
result[1] = found;
break;
}
}*/
return result;
}
}