< Summary

Information
Class: LeetCode.Algorithms.StringToInteger.StringToIntegerIterative
Assembly: LeetCode
File(s): D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\StringToInteger\StringToIntegerIterative.cs
Line coverage
100%
Covered lines: 29
Uncovered lines: 0
Coverable lines: 29
Total lines: 64
Line coverage: 100%
Branch coverage
87%
Covered branches: 21
Total branches: 24
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
MyAtoi(...)87.5%2424100%

File(s)

D:\a\LeetCode-CS\LeetCode-CS\source\LeetCode\Algorithms\StringToInteger\StringToIntegerIterative.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.StringToInteger;
 13
 14/// <inheritdoc />
 15public class StringToIntegerIterative : IStringToInteger
 16{
 17    /// <summary>
 18    ///     Time complexity - O(n)
 19    ///     Space complexity - O(1)
 20    /// </summary>
 21    /// <param name="s"></param>
 22    /// <returns></returns>
 23    public int MyAtoi(string s)
 724    {
 725        var i = 0;
 26
 827        while (i < s.Length && s[i] == ' ')
 128        {
 129            i++;
 130        }
 31
 732        var sign = 1;
 33
 734        if (i < s.Length && (s[i] == '+' || s[i] == '-'))
 235        {
 236            if (s[i] == '-')
 237            {
 238                sign = -1;
 239            }
 40
 241            i++;
 242        }
 43
 744        long result = 0;
 45
 3546        while (i < s.Length && char.IsDigit(s[i]))
 3047        {
 3048            result = (result * 10) + (s[i] - '0');
 49
 3050            switch (sign)
 51            {
 1752                case 1 when result > int.MaxValue:
 153                    return int.MaxValue;
 1354                case -1 when -result < int.MinValue:
 155                    return int.MinValue;
 56                default:
 2857                    i++;
 2858                    break;
 59            }
 2860        }
 61
 562        return (int)(sign * result);
 763    }
 64}

Methods/Properties

MyAtoi(System.String)