Last active
August 29, 2015 14:23
-
-
Save codetalks-new/b356f673e6de22865c94 to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/unique-binary-search-trees/ Swift solution
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
var cache = [Int:Int]() | |
func numTrees(n:Int) -> Int{ | |
if n < 2{ | |
return 1 | |
} | |
if n == 2{ | |
return 2 | |
} | |
if let value = cache[n]{ | |
return value | |
} | |
var count = 0 | |
for m in 0..<n { | |
count += numTrees(m) * numTrees(n - m - 1) | |
} | |
cache[n] = count | |
return count | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment