LeetCode 989. Add to Array-Form of Integer in F#
URL
Add to Array-Form of Integer - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/problems/0989/main.fsx
let addToArrayForm (num: int list) (k: int) : int list =
let rec addToArrayForm' nums ks carry acc =
match nums, ks with
| [], [] -> if carry = 1 then 1 :: acc else acc
| h :: t, [] ->
let v = h + carry
if v >= 10 then
addToArrayForm' t [] 1 ((v - 10) :: acc)
else
addToArrayForm' t [] 0 (v :: acc)
| [], h :: t ->
let v = h + carry
if v >= 10 then
addToArrayForm' [] t 1 ((v - 10) :: acc)
else
addToArrayForm' [] t 0 (v :: acc)
| h1 :: t1, h2 :: t2 ->
let v = h1 + h2 + carry
if v >= 10 then
addToArrayForm' t1 t2 1 ((v - 10) :: acc)
else
addToArrayForm' t1 t2 0 (v :: acc)
let ks = k |> string |> Seq.map (fun c -> int c - int '0') |> Seq.rev |> Seq.toList
addToArrayForm' (num |> List.rev) ks 0 []