Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 1323. Maximum 69 Number in F#

Shohei Yoshida's photo
Shohei Yoshida
·Nov 7, 2022·

1 min read

URL

leetcode.com/problems/maximum-69-number

Code

github.com/syohex/dotnet-study/blob/master/..

let maximum69Number (num: int) : int =
    let rec toDigits n acc =
        if n <= 0 then
            acc
        else
            toDigits (n / 10) ((n % 10) :: acc)

    toDigits num []
    |> List.fold
        (fun (sum, ok) n ->
            if ok || n = 9 then
                sum * 10 + n, ok
            else
                sum * 10 + 9, true)
        (0, false)
    |> fst
 
Share this