-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0001_Two_Sum.cs
32 lines (32 loc) · 887 Bytes
/
0001_Two_Sum.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
public class Solution {
public int[] TwoSum(int[] nums, int target) {
int n = nums.Length;
int[] ss = new int[n];
Array.Copy(nums, ss, n);
Array.Sort(ss);
int j = nums.Length - 1;
int i;
for (i = 0; i < n; i++) {
while (ss[i] + ss[j] > target)
j--;
if (ss[i] + ss[j] == target)
break;
}
//Console.WriteLine($"ss[{i}] == {ss[i]}, ss[{j}] == {ss[j]}");
int[] ret = new int[] {-1, -1};
for (int k = 0; k < nums.Length; k++)
{
if (nums[k] == ss[i] || nums[k] == ss[j])
{
if (ret[0] == -1)
ret[0] = k;
else
{
ret[1] = k;
return ret;
}
}
}
return null;
}
}