http://blog.csdn.net/unhappypeople/article/details/17199951
杨辉三角
- 边界1
- 其他是上一行的两个数的和
def pascal(c:Int,r:Int): Int ={if (c == 0 || c == r|| r==0) 1else pascal(c-1,r-1) + pascal(c,r-1)}
- 括号平衡
- 遇到左括号+1
- 遇到右括号-1
- count小于零false,chars为空且count为0 true
def balance(chars: String): Boolean ={def isMatch(chars: String,count:Int):Boolean={if (count < 0) falseelse if(chars.isEmpty) count == 0else if(chars.head == '('){isMatch(chars.tail,count+1)}else if (chars.head == ')'){isMatch(chars.tail,count -1)}else{isMatch(chars.tail,count)}}isMatch(chars,0)
}
println(balance("()()!"))
- 硬币组合
- 假设有三个硬币ABC,只考虑A,
1.不用A 2.用一次A 3.用多次A
- 假设有三个硬币ABC,只考虑A,
def count(vs: List[Int],all:Int):Int={if (all == 0) 1else if(vs.isEmpty) 0else if(all < 0) 0elsecount(vs.tail,all)+ count(vs,all-vs.head)
}