Kotlin - Completed Pascal's Triangle
This commit is contained in:
parent
b5669c7628
commit
3b643db419
1 changed files with 27 additions and 0 deletions
27
kotlin/pascals-triangle/src/main/kotlin/PascalsTriangle.kt
Normal file
27
kotlin/pascals-triangle/src/main/kotlin/PascalsTriangle.kt
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
class PascalsTriangle {
|
||||||
|
companion object {
|
||||||
|
fun computeTriangle(row: Int): List<List<Int>>{
|
||||||
|
require(row >= 0){}
|
||||||
|
val returnList = mutableListOf<MutableList<Int>>()
|
||||||
|
for (i in 0..row-1){
|
||||||
|
val tempList = mutableListOf<Int>()
|
||||||
|
for (j in 0..i){
|
||||||
|
tempList.add(pascal(i,j))
|
||||||
|
}
|
||||||
|
returnList.add(tempList)
|
||||||
|
}
|
||||||
|
return returnList
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun pascal(i: Int, j: Int): Int {
|
||||||
|
if (j == 0) {
|
||||||
|
return 1
|
||||||
|
} else if (j == i) {
|
||||||
|
return 1
|
||||||
|
} else {
|
||||||
|
return pascal(i - 1, j - 1) + pascal(i - 1, j)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue