Wrong return
behavior (mix expressions and statements)
Without return
keyword:
def countChange(money: Int, coins: List[Int]): Int = {
def cc(amount: Int, list: List[Int]): Int = {
if (amount == 0) 1
else if (amount < 0 || list.isEmpty) 0
else cc(amount - list.head, list) + cc(amount, list.tail)
}
cc(money, coins)
}
Or with return
keyword:
def countChange1(money: Int, coins: List[Int]): Int = {
def cc(amount: Int, list: List[Int]): Int = {
if (amount == 0) return 1
if (amount < 0 || list.isEmpty) return 0
return cc(amount - list.head, list) + cc(amount, list.tail)
}
cc(money, coins)
}