Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions solution/0000-0099/0016.3Sum Closest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,36 @@ var threeSumClosest = function (nums, target) {
};
```

#### C#

```cs
public class Solution {
public int ThreeSumClosest(int[] nums, int target) {
Array.Sort(nums);
int ans = 1 << 30;
int n = nums.Length;
for (int i = 0; i < n; ++i) {
int j = i + 1, k = n - 1;
while (j < k) {
int t = nums[i] + nums[j] + nums[k];
if (t == target) {
return t;
}
if (Math.Abs(t - target) < Math.Abs(ans - target)) {
ans = t;
}
if (t > target) {
--k;
} else {
++j;
}
}
}
return ans;
}
}
```

#### PHP

```php
Expand Down
30 changes: 30 additions & 0 deletions solution/0000-0099/0016.3Sum Closest/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,36 @@ var threeSumClosest = function (nums, target) {
};
```

#### C#

```cs
public class Solution {
public int ThreeSumClosest(int[] nums, int target) {
Array.Sort(nums);
int ans = 1 << 30;
int n = nums.Length;
for (int i = 0; i < n; ++i) {
int j = i + 1, k = n - 1;
while (j < k) {
int t = nums[i] + nums[j] + nums[k];
if (t == target) {
return t;
}
if (Math.Abs(t - target) < Math.Abs(ans - target)) {
ans = t;
}
if (t > target) {
--k;
} else {
++j;
}
}
}
return ans;
}
}
```

#### PHP

```php
Expand Down
25 changes: 25 additions & 0 deletions solution/0000-0099/0016.3Sum Closest/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Solution {
public int ThreeSumClosest(int[] nums, int target) {
Array.Sort(nums);
int ans = 1 << 30;
int n = nums.Length;
for (int i = 0; i < n; ++i) {
int j = i + 1, k = n - 1;
while (j < k) {
int t = nums[i] + nums[j] + nums[k];
if (t == target) {
return t;
}
if (Math.Abs(t - target) < Math.Abs(ans - target)) {
ans = t;
}
if (t > target) {
--k;
} else {
++j;
}
}
}
return ans;
}
}