26 lines
735 B
C#
26 lines
735 B
C#
using System;
|
|
|
|
public class Solution {
|
|
public int[] TwoSum(int[] nums, int target) {
|
|
int[] sorted = new int[nums.Length];
|
|
nums.CopyTo(sorted, 0);
|
|
Array.Sort(sorted);
|
|
int i = 0, j = sorted.Length - 1;
|
|
while (sorted[i] + sorted[j] != target) {
|
|
while (sorted[i] + sorted[j] > target) {
|
|
j--;
|
|
}
|
|
while (sorted[i] + sorted[j] < target) {
|
|
i++;
|
|
}
|
|
}
|
|
int[] result = new int[2];
|
|
int r = 0;
|
|
for (int index = 0; r < 2; index++) {
|
|
if (nums[index] == sorted[i] || nums[index] == sorted[j]) {
|
|
result[r++] = index;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
} |