< Summary

Information
Class: LeetCode.Algorithms.Permutations.PermutationsBacktracking
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\Permutations\PermutationsBacktracking.cs
Line coverage
100%
Covered lines: 19
Uncovered lines: 0
Coverable lines: 19
Total lines: 50
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Permute(...)100%11100%
Backtrack(...)100%44100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\Permutations\PermutationsBacktracking.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.Permutations;
 13
 14/// <inheritdoc />
 15public class PermutationsBacktracking : IPermutations
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n * n!)
 19    ///     Space complexity - O(n * n!)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public IList<IList<int>> Permute(int[] nums)
 324    {
 325        var result = new List<IList<int>>();
 26
 327        Backtrack(nums, 0, nums.Length, result);
 28
 329        return result;
 330    }
 31
 32    private static void Backtrack(IList<int> nums, int start, int end, ICollection<IList<int>> result)
 2333    {
 2334        if (start == end)
 935        {
 936            result.Add(new List<int>(nums));
 937        }
 38        else
 1439        {
 6840            for (var i = start; i < end; i++)
 2041            {
 2042                (nums[i], nums[start]) = (nums[start], nums[i]);
 43
 2044                Backtrack(nums, start + 1, end, result);
 45
 2046                (nums[i], nums[start]) = (nums[start], nums[i]);
 2047            }
 1448        }
 2349    }
 50}