C# Solution


  • 0
    A

    '''
    public int[] PlusOne(int[] digits)
    {
    if (digits.Length == 0) //If the given input is null we return
    return digits;

            List<int> result = new List<int>(); //Use a list since we are not sure of the size of the result
        
            int sum = 0; // To store the sum
            int carry = 1; //To store carry, initialize this to 1 to add one
    
        
            for (int i = digits.Length - 1; i >= 0;i--) // To start from the least significant digit we start from the end of the list
            {
                sum = digits[i] + carry;
                carry = sum / 10;
                sum = sum % 10;
                result.Add(sum); // Add the sum to list
            }
        
            if(carry != 0)
                result.Add(carry); //Add the last carry if it exists
           
            result.Reverse();  // Reverse the list since we started from the end
           
            return result.ToArray(); //Convert the list to an Array
    
    }
    

    '''


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.