Input
#+begin_src
.NET Data type | F# Declaration |
---|---|
Int | let i = 0 or let i = 0l |
Uint | let i = 1u or let i = 1ul |
Decimal | let d = 1m or let d = 1M |
Short | let c = 2s |
Long | let l = 5L |
from __future__ import division | |
def binary_search(xs, target): | |
""" | |
type : Listof[Val] * Val -> Int | |
input : xs :: Sorted list | |
target :: Search target | |
desp : Return the index of the element in the list, -1 if the | |
element is not in the list |
Input
#+begin_src
.NET Data type | F# Declaration |
---|---|
Int | let i = 0 or let i = 0l |
Uint | let i = 1u or let i = 1ul |
Decimal | let d = 1m or let d = 1M |
Short | let c = 2s |
Long | let l = 5L |
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1) | |
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1) | |
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1) | |
(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1) | |
(1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 0 1 1 1 0) | |
(0 0 0 1 1 0 1 1 1 1 1 1 1 1 0 1 1 1 1 1) | |
(1 1 1 1 1 1 0 0 0 0 0 1 0 1 1 1 1 1 1 1) | |
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1) | |
(0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1) | |
(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0) |
import kotlin.math.abs | |
// https://leetcode.com/problems/target-sum/ | |
// dp function: dp[i][j] = dp[i - 1][j - nums[i]] + dp[i - 1][j + nums[i]] | |
class TargetSum { | |
fun findTargetSumWays(nums: IntArray, target: Int): Int { | |
val dp = Array(nums.size + 1) { mutableMapOf<Int, Int>().withDefault { 0 } } | |
dp[0][0] = 1 | |
for (i in 1..nums.size) { |