cft
Become a CreatorSign inGet Started

LeetCode 39. Combination Sum in F#

LeetCode Daily Problem at 2022/02/17


user

Shohei Yoshida

3 months ago | 1 min read
Follow

leetcode-39-combination-sum-f-wsdki

URL

Combination Sum - LeetCode

Code

https://github.com/syohex/dotnet-study/blob/master/fsharp/leetcode/problems/0039/main.fsx

let combinationSum (nums: int list) (target: int) : int list list =

let rec combinationSum' i (nums: int list) target ys ret =

let sum = ys |> List.sum

if sum = target then

(List.rev ys) :: ret

else if sum > target || i >= (List.length nums) then

ret

else

nums

|> List.mapi (fun i num -> (i, num))

|> List.skip i

|> List.fold (fun acc (i, num) -> combinationSum' i nums target (num :: ys) acc) ret

combinationSum' 0 (nums |> List.sort) target [] []

|> List.rev

Upvote


user
Created by

Shohei Yoshida

Follow

A programmer of DeNA

A Programmer


people
Post

Upvote

Downvote

Comment

Bookmark

Share


Related Articles