< Summary

Information
Class: LeetCode.Algorithms.ThreeSum.ThreeSumTwoPointers
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ThreeSum\ThreeSumTwoPointers.cs
Line coverage
86%
Covered lines: 32
Uncovered lines: 5
Coverable lines: 37
Total lines: 79
Line coverage: 86.4%
Branch coverage
90%
Covered branches: 20
Total branches: 22
Branch coverage: 90.9%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
ThreeSum(...)90.9%232286.48%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\ThreeSum\ThreeSumTwoPointers.cs

#LineLine coverage
 1// --------------------------------------------------------------------------------
 2// Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv).
 3// All Rights Reserved.
 4// --------------------------------------------------------------------------------
 5// This software is the confidential and proprietary information of Eugene Eremeev
 6// (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not
 7// disclose such Confidential Information and shall use it only in accordance with
 8// the terms of the license agreement you entered into with Eugene Eremeev (also
 9// known as Yevhenii Yeriemeieiv).
 10// --------------------------------------------------------------------------------
 11
 12namespace LeetCode.Algorithms.ThreeSum;
 13
 14/// <inheritdoc />
 15public class ThreeSumTwoPointers : IThreeSum
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n^2)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public IList<IList<int>> ThreeSum(int[] nums)
 524    {
 525        if (nums.Length < 3)
 026        {
 027            return new List<IList<int>>();
 28        }
 29
 530        Array.Sort(nums);
 31
 532        List<IList<int>> result = [];
 33
 3634        for (var i = 0; i < nums.Length - 2; i++)
 1335        {
 1336            if (i > 0 && nums[i] == nums[i - 1])
 137            {
 138                continue;
 39            }
 40
 1241            var left = i + 1;
 1242            var right = nums.Length - 1;
 43
 3444            while (left < right)
 2245            {
 2246                var sum = nums[i] + nums[left] + nums[right];
 47
 2248                switch (sum)
 49                {
 50                    case 0:
 851                        result.Add(new List<int> { nums[i], nums[left], nums[right] });
 52
 1153                        while (left < right && nums[left] == nums[left + 1])
 354                        {
 355                            left++;
 356                        }
 57
 858                        while (left < right && nums[right] == nums[right - 1])
 059                        {
 060                            right--;
 061                        }
 62
 863                        left++;
 864                        right--;
 65
 866                        break;
 67                    case < 0:
 568                        left++;
 569                        break;
 70                    default:
 971                        right--;
 972                        break;
 73                }
 2274            }
 1275        }
 76
 577        return result;
 578    }
 79}

Methods/Properties

ThreeSum(System.Int32[])