C# solution


  • 0
    A
    public static int[] PlusOne(int[] digits)
    {
        int sum = 0;
        int carry = 1;
    
        for (int i = digits.Length - 1; i >= 0; i--)
        {
            int result = digits[i] + carry;
            sum = result % 10;
            carry = (int) result / 10;
    
            digits[i] = sum;
        }
    
        if (carry == 0)
            return digits;
    
        int[] newDigits = new int[digits.Length + 1];
        newDigits[0] = carry;
    
        return newDigits;
    }
    

Log in to reply
 

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