Skip to content

Commit 0c13d17

Browse files
committed
fix: solutions not shown
No.0015.3Sum
1 parent 21ad4e4 commit 0c13d17

File tree

2 files changed

+258
-0
lines changed

2 files changed

+258
-0
lines changed

solution/0000-0099/0015.3Sum/README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,135 @@ var threeSum = function (nums) {
228228
};
229229
```
230230

231+
### **C#**
232+
233+
```cs
234+
using System;
235+
using System.Collections.Generic;
236+
using System.Linq;
237+
238+
public class ThreeSumComparer : IEqualityComparer<IList<int>>
239+
{
240+
public bool Equals(IList<int> left, IList<int> right)
241+
{
242+
return left[0] == right[0] && left[1] == right[1] && left[2] == right[2];
243+
}
244+
245+
public int GetHashCode(IList<int> obj)
246+
{
247+
return (obj[0] ^ obj[1] ^ obj[2]).GetHashCode();
248+
}
249+
}
250+
251+
public class Solution {
252+
public IList<IList<int>> ThreeSum(int[] nums) {
253+
Array.Sort(nums);
254+
var results = new HashSet<IList<int>>(new ThreeSumComparer());
255+
256+
var cIndex = Array.BinarySearch(nums, 0);
257+
if (cIndex < 0) cIndex = ~cIndex;
258+
while (cIndex < nums.Length)
259+
{
260+
var c = nums[cIndex];
261+
var aIndex = 0;
262+
var bIndex = cIndex - 1;
263+
while (aIndex < bIndex)
264+
{
265+
if (nums[aIndex] + nums[bIndex] + c < 0)
266+
{
267+
var step = 1;
268+
while (aIndex + step < bIndex && nums[aIndex + step] + nums[bIndex] + c < 0)
269+
{
270+
aIndex += step;
271+
step *= 2;
272+
}
273+
step /= 2;
274+
while (step > 0)
275+
{
276+
if (aIndex + step < bIndex && nums[aIndex + step] + nums[bIndex] + c < 0)
277+
{
278+
aIndex += step;
279+
}
280+
step /= 2;
281+
}
282+
}
283+
284+
if (nums[aIndex] + nums[bIndex] + c > 0)
285+
{
286+
var step = 1;
287+
while (aIndex < bIndex - step && nums[aIndex] + nums[bIndex - step] + c > 0)
288+
{
289+
bIndex -= step;
290+
step *= 2;
291+
}
292+
step /= 2;
293+
while (step > 0)
294+
{
295+
if (aIndex < bIndex - step && nums[aIndex] + nums[bIndex - step] + c > 0)
296+
{
297+
bIndex -= step;
298+
}
299+
step /= 2;
300+
}
301+
}
302+
303+
if (nums[aIndex] + nums[bIndex] + c == 0)
304+
{
305+
var list = new List<int> { nums[aIndex], nums[bIndex], c };
306+
results.Add(list);
307+
++aIndex;
308+
--bIndex;
309+
}
310+
else if (nums[aIndex] + nums[bIndex] + c < 0)
311+
{
312+
++aIndex;
313+
}
314+
else
315+
{
316+
--bIndex;
317+
}
318+
}
319+
++cIndex;
320+
}
321+
322+
return results.ToList();
323+
}
324+
}
325+
```
326+
327+
### **Ruby**
328+
329+
```rb
330+
# @param {Integer[]} nums
331+
# @return {Integer[][]}
332+
def three_sum(nums)
333+
res = []
334+
nums.sort!
335+
336+
for i in 0..(nums.length - 3)
337+
next if i > 0 && nums[i - 1] == nums[i]
338+
j = i + 1
339+
k = nums.length - 1
340+
while j < k do
341+
sum = nums[i] + nums[j] + nums[k]
342+
if sum < 0
343+
j += 1
344+
elsif sum > 0
345+
k -= 1
346+
else
347+
res += [[nums[i], nums[j], nums[k]]]
348+
j += 1
349+
k -= 1
350+
j += 1 while nums[j] == nums[j - 1]
351+
k -= 1 while nums[k] == nums[k + 1]
352+
end
353+
end
354+
end
355+
356+
res
357+
end
358+
```
359+
231360
### **...**
232361

233362
```

solution/0000-0099/0015.3Sum/README_EN.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,135 @@ var threeSum = function (nums) {
205205
};
206206
```
207207

208+
### **C#**
209+
210+
```cs
211+
using System;
212+
using System.Collections.Generic;
213+
using System.Linq;
214+
215+
public class ThreeSumComparer : IEqualityComparer<IList<int>>
216+
{
217+
public bool Equals(IList<int> left, IList<int> right)
218+
{
219+
return left[0] == right[0] && left[1] == right[1] && left[2] == right[2];
220+
}
221+
222+
public int GetHashCode(IList<int> obj)
223+
{
224+
return (obj[0] ^ obj[1] ^ obj[2]).GetHashCode();
225+
}
226+
}
227+
228+
public class Solution {
229+
public IList<IList<int>> ThreeSum(int[] nums) {
230+
Array.Sort(nums);
231+
var results = new HashSet<IList<int>>(new ThreeSumComparer());
232+
233+
var cIndex = Array.BinarySearch(nums, 0);
234+
if (cIndex < 0) cIndex = ~cIndex;
235+
while (cIndex < nums.Length)
236+
{
237+
var c = nums[cIndex];
238+
var aIndex = 0;
239+
var bIndex = cIndex - 1;
240+
while (aIndex < bIndex)
241+
{
242+
if (nums[aIndex] + nums[bIndex] + c < 0)
243+
{
244+
var step = 1;
245+
while (aIndex + step < bIndex && nums[aIndex + step] + nums[bIndex] + c < 0)
246+
{
247+
aIndex += step;
248+
step *= 2;
249+
}
250+
step /= 2;
251+
while (step > 0)
252+
{
253+
if (aIndex + step < bIndex && nums[aIndex + step] + nums[bIndex] + c < 0)
254+
{
255+
aIndex += step;
256+
}
257+
step /= 2;
258+
}
259+
}
260+
261+
if (nums[aIndex] + nums[bIndex] + c > 0)
262+
{
263+
var step = 1;
264+
while (aIndex < bIndex - step && nums[aIndex] + nums[bIndex - step] + c > 0)
265+
{
266+
bIndex -= step;
267+
step *= 2;
268+
}
269+
step /= 2;
270+
while (step > 0)
271+
{
272+
if (aIndex < bIndex - step && nums[aIndex] + nums[bIndex - step] + c > 0)
273+
{
274+
bIndex -= step;
275+
}
276+
step /= 2;
277+
}
278+
}
279+
280+
if (nums[aIndex] + nums[bIndex] + c == 0)
281+
{
282+
var list = new List<int> { nums[aIndex], nums[bIndex], c };
283+
results.Add(list);
284+
++aIndex;
285+
--bIndex;
286+
}
287+
else if (nums[aIndex] + nums[bIndex] + c < 0)
288+
{
289+
++aIndex;
290+
}
291+
else
292+
{
293+
--bIndex;
294+
}
295+
}
296+
++cIndex;
297+
}
298+
299+
return results.ToList();
300+
}
301+
}
302+
```
303+
304+
### **Ruby**
305+
306+
```rb
307+
# @param {Integer[]} nums
308+
# @return {Integer[][]}
309+
def three_sum(nums)
310+
res = []
311+
nums.sort!
312+
313+
for i in 0..(nums.length - 3)
314+
next if i > 0 && nums[i - 1] == nums[i]
315+
j = i + 1
316+
k = nums.length - 1
317+
while j < k do
318+
sum = nums[i] + nums[j] + nums[k]
319+
if sum < 0
320+
j += 1
321+
elsif sum > 0
322+
k -= 1
323+
else
324+
res += [[nums[i], nums[j], nums[k]]]
325+
j += 1
326+
k -= 1
327+
j += 1 while nums[j] == nums[j - 1]
328+
k -= 1 while nums[k] == nums[k + 1]
329+
end
330+
end
331+
end
332+
333+
res
334+
end
335+
```
336+
208337
### **...**
209338

210339
```

0 commit comments

Comments
 (0)