Skip to main content

LeetCode 165. Compare Version Numbers in F#

Published 1 min readMarkdown

URL

Compare Version Numbers - LeetCode

Code

dotnet-study/fsharp/leetcode/challenge/202509/compare_version_numbers/main.fsx at master · syohex/dotnet-study

let compareVersion (version1: string) (version2: string) : int =
    let toNumber s =
        let rec toNumber' cs acc =
            match cs with
            | [] -> acc
            | h :: t -> toNumber' t (acc * 10 + int h - int '0')

        toNumber' (Seq.toList s) 0

    let rec compareVersion' v1 v2 =
        match v1, v2 with
        | [], [] -> 0
        | h1 :: t1, [] ->
            let n1 = toNumber h1
            if n1 = 0 then compareVersion' t1 [] else compare n1 0
        | [], h2 :: t2 ->
            let n2 = toNumber h2
            if n2 = 0 then compareVersion' t2 [] else compare 0 n2
        | h1 :: t1, h2 :: t2 ->
            let n1, n2 = toNumber h1, toNumber h2
            if n1 = n2 then compareVersion' t1 t2 else compare n1 n2

    let v1 = version1.Split('.') |> Array.toList
    let v2 = version2.Split('.') |> Array.toList
    compareVersion' v1 v2

No comments yet. Be the first to comment.

More from this blog