< Summary

Information
Class: LeetCode.Algorithms.SummaryRanges.SummaryRangesIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SummaryRanges\SummaryRangesIterative.cs
Line coverage
92%
Covered lines: 23
Uncovered lines: 2
Coverable lines: 25
Total lines: 59
Line coverage: 92%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
SummaryRanges(...)83.33%6690.9%
GetRangeString(...)100%22100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\SummaryRanges\SummaryRangesIterative.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.SummaryRanges;
 13
 14/// <inheritdoc />
 15public class SummaryRangesIterative : ISummaryRanges
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(n)
 20    /// </summary>
 21    /// <param name="nums"></param>
 22    /// <returns></returns>
 23    public IList<string> SummaryRanges(int[] nums)
 224    {
 225        if (nums.Length == 0)
 026        {
 027            return new List<string>();
 28        }
 29
 230        var ranges = new List<string>();
 31
 232        var start = nums[0];
 233        var end = start;
 34
 2635        for (var i = 1; i < nums.Length; i++)
 1136        {
 1137            if (end + 1 == nums[i])
 638            {
 639                end = nums[i];
 640            }
 41            else
 542            {
 543                ranges.Add(GetRangeString(start, end));
 44
 545                start = nums[i];
 546                end = start;
 547            }
 1148        }
 49
 250        ranges.Add(GetRangeString(start, end));
 51
 252        return ranges;
 253    }
 54
 55    private static string GetRangeString(int start, int end)
 756    {
 757        return start == end ? start.ToString() : $"{start}->{end}";
 758    }
 59}