Created
October 4, 2015 16:51
-
-
Save shakked/6fc735e8a06345fc91a4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func stairs(n: Int) -> [[Int]] { | |
if n == 0 { | |
return [] | |
} else if n == 1 { | |
let one = [[1]] + stairs(n - 1) | |
let one2 = stairs(n - 1) + [[1]] | |
return one + one2 | |
} else if n == 2 { | |
let one = [[1]] + stairs(n - 1) | |
let two = [[2]] + stairs(n - 2) | |
let one2 = stairs(n - 1) + [[1]] | |
let two2 = stairs(n - 2) + [[2]] | |
return one + one2 + two + two2 | |
} else if n >= 3 { | |
let one = [[1]] + stairs(n - 1) | |
let two = [[2]] + stairs(n - 2) | |
let thr = [[3]] + stairs(n - 3) | |
let one2 = stairs(n - 1) + [[1]] | |
let two2 = stairs(n - 2) + [[2]] | |
let thr2 = stairs(n - 3) + [[3]] | |
let result = one + two | |
let result2 = thr + one2 | |
let result3 = two2 + thr2 | |
return result + result2 + result3 | |
} else { | |
return [] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment