Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 869. Reordered Power of 2 in F#

Shohei Yoshida's photo
Shohei Yoshida
·Aug 26, 2022·

1 min read

URL

leetcode.com/problems/reordered-power-of-2

Code

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

let reorderedPowerOf2 (n: int) : bool =
    let rec toDigitsStr n acc =
        if n = 0 then
            acc |> List.sort |> string
        else
            toDigitsStr (n / 10) ((n % 10) :: acc)

    let s = toDigitsStr n []
    seq { 0..30 }
    |> Seq.map (fun m -> System.Math.Pow(2, m) |> int)
    |> Seq.map (fun m -> toDigitsStr m [])
    |> Seq.tryFind (fun t -> s = t)
    |> Option.isSome
 
Share this