Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 523. Continuous Subarray Sum in F#

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

1 min read

URL

leetcode.com/problems/continuous-subarray-sum

Code

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

let checkSubarrayNum (nums: int list) (k: int) : bool =
    let rec checkSubarrayNum' nums i sum k m =
        match nums with
        | [] -> false
        | h :: t ->
            let sum' = h + sum
            let modulo = sum' % k

            match Map.tryFind modulo m with
            | Some (j) ->
                if j < i then
                    true
                else
                    checkSubarrayNum' t (i + 1) sum' k m
            | None -> checkSubarrayNum' t (i + 1) sum' k (Map.add modulo (i + 1) m)

    checkSubarrayNum' nums 0 0 k (Map.add 0 0 Map.empty)
 
Share this