Unique Binary Search Trees
/*** Given n , how many structurally unique BST's (binary search trees) that store values 1... n ? For example, Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1 2 3 ***/ class Solution { public : int getnumTrees( int st , int ed ) { if ( st >= ed ) return 1; int sum = 0; for ( int i= st ; i<= ed ; i++) { sum += getnumTrees( st , i-1) * getnumTrees(i+1, ed ); } return sum; } int numTrees( int n ) { // Start typing your C/C++ solution below // DO NOT write int main() function return getnumTrees(1, n ); } };
declare a list
ReplyDeletelist = []
declare a dictionary
dic = {}
Regular Expressions in Python
ReplyDeleteAn example regex is r"^(From|To|Cc).*?python-list@python.org"
- the caret ^ matches text at the beginning of a line.
- the part with (From|To|Cc) means that the line has to start with one of the words that are separated by the pipe |
- The .*? means to un-greedily match any number of characters, except the newline \n character. The un-greedy part means to match as few repetitions as possible. The . character means any non-newline character, the * means to repeat 0 or more times, and the ? character makes it un-greedy.