problem 1
This commit is contained in:
parent
394eb78247
commit
35a2375f7e
26
TwoSum.cs
Normal file
26
TwoSum.cs
Normal file
@ -0,0 +1,26 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user