Plus One

Solution in Go

func plusOne(digits []int) []int {
    for i := len(digits) - 1; i >= 0; i-- {
        if digits[i] < 9 {
            digits[i]++
            return digits
        }

        digits[i] = 0
    }

    digits = make([]int, len(digits)+1)
    digits[0] = 1
    
    return digits
}
Subscribe via email

Get notified once/twice per month when new articles are published.

Copyright © 2022 - 2024 TheDeveloperCafe.
The Go gopher was designed by Renee French.