]]>[Leetcode] Permutation Sequencehttp://www.tkk7.com/menglee/archive/2014/01/07/408642.htmlMeng LeeMeng LeeTue, 07 Jan 2014 08:06:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/07/408642.htmlhttp://www.tkk7.com/menglee/comments/408642.htmlhttp://www.tkk7.com/menglee/archive/2014/01/07/408642.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408642.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408642.htmlThe set [1,2,3,…,n] contains a total of n! unique permutations.
By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):
"123"
"132"
"213"
"231"
"312"
"321"
Given n and k, return the kth permutation sequence.
Note: Given n will be between 1 and 9 inclusive.
榪欓亾棰樺叾瀹炴湁寰堝己鐨勮寰嬪彲寰傞鍏堬紝n涓厓绱犵殑鎺掑垪鎬繪暟鏄痭!銆傚湪涓嬮潰鐨勫垎鏋愪腑錛岃k鐨勮寖鍥存槸0 <= k < n!銆傦紙棰樼洰浠g爜瀹為檯涓婃槸1<=k<=n!) 鍙互鐪嬪埌涓涓寰嬶紝灝辨槸榪檔錛佷釜鎺掑垪涓紝絎竴浣嶇殑鍏冪礌鎬繪槸(n-1)!涓緇勫嚭鐜扮殑錛屼篃灝辮濡傛灉p = k / (n-1)!錛岄偅涔堟帓鍒楃殑鏈寮濮嬩竴涓厓绱犱竴瀹氭槸arr[p]銆?/div>
1/** 2 * The set [1,2,3,…,n] contains a total of n! unique permutations. 3 * 4 * By listing and labeling all of the permutations in order, We get the 5 * following sequence (ie, for n = 3): 6 * 7 * "123" "132" "213" "231" "312" "321" Given n and k, return the kth permutation 8 * sequence. 9 * 10 * Note: Given n will be between 1 and 9 inclusive. 11 * 12*/ 13 14publicclass PermutationSequence { 15public String getPermutation(int n, int k) { 16char[] arr = newchar[n]; 17int pro = 1; 18for (int i = 0; i < n; ++i) { 19 arr[i] = (char) ('1' + i); 20 pro *= (i + 1); 21 } 22 k = k - 1; 23 k %= pro; 24 pro /= n; 25for (int i = 0; i < n - 1; ++i) { 26int selectI = k / pro; 27 k = k % pro; 28 pro /= (n - i - 1); 29int temp = arr[selectI + i]; 30for (int j = selectI; j > 0; --j) { 31 arr[i + j] = arr[i + j - 1]; 32 } 33 arr[i] = (char) temp; 34 } 35return String.valueOf(arr); 36 } 37 } 38
]]>[Leetcode] Lowest Common Ancestor of a Binary Search Tree (BST) && Lowest Common Ancestor of Binary Treehttp://www.tkk7.com/menglee/archive/2014/01/06/408544.htmlMeng LeeMeng LeeMon, 06 Jan 2014 01:32:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/06/408544.htmlhttp://www.tkk7.com/menglee/comments/408544.htmlhttp://www.tkk7.com/menglee/archive/2014/01/06/408544.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408544.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408544.html
Lowest Common Ancestor of a Binary Search Tree (BST)
Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.
_______6______
/ \
___2__ ___8__
/ \ / \
0 _4 7 9
/ \
3 5
Using the above tree as an example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6.
But how about LCA of nodes 2 and 4? Should it be 6 or 2?
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between
two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a
node to be a descendant of itself).” Since a node can be a descendant of itself, the LCA of 2 and
4 should be 2, according to this definition.
Hint:
A top-down walk from the root of the tree is enough.
]]>[Leetcode] Scramble Stringhttp://www.tkk7.com/menglee/archive/2014/01/05/408521.htmlMeng LeeMeng LeeSun, 05 Jan 2014 04:33:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/05/408521.htmlhttp://www.tkk7.com/menglee/comments/408521.htmlhttp://www.tkk7.com/menglee/archive/2014/01/05/408521.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408521.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408521.htmlGiven a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.
Below is one possible representation of s1 = "great":
great
/ \
gr eat
/ \ / \
g r e at
/ \
a t
To scramble the string, we may choose any non-leaf node and swap its two children.
For example, if we choose the node "gr" and swap its two children, it produces a scrambled string "rgeat".
rgeat
/ \
rg eat
/ \ / \
r g e at
/ \
a t
We say that "rgeat" is a scrambled string of "great".
Similarly, if we continue to swap the children of nodes "eat" and "at", it produces a scrambled string "rgtae".
rgtae
/ \
rg tae
/ \ / \
r g ta e
/ \
t a
We say that "rgtae" is a scrambled string of "great".
Given two strings s1 and s2 of the same length, determine if s2 is a scrambled string of s1.
]]>[Leetcode] Largest Rectangle in Histogramhttp://www.tkk7.com/menglee/archive/2014/01/05/408520.htmlMeng LeeMeng LeeSun, 05 Jan 2014 04:31:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/05/408520.htmlhttp://www.tkk7.com/menglee/comments/408520.htmlhttp://www.tkk7.com/menglee/archive/2014/01/05/408520.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408520.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408520.htmlGiven n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.
Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].
The largest rectangle is shown in the shaded area, which has area = 10 unit.
]]>[Leetcode] Binary Tree Inorder Traversalhttp://www.tkk7.com/menglee/archive/2014/01/04/408477.htmlMeng LeeMeng LeeSat, 04 Jan 2014 03:17:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/04/408477.htmlhttp://www.tkk7.com/menglee/comments/408477.htmlhttp://www.tkk7.com/menglee/archive/2014/01/04/408477.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408477.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408477.htmlGiven a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
]]>[Leetcode] Subsets IIhttp://www.tkk7.com/menglee/archive/2014/01/03/408444.htmlMeng LeeMeng LeeFri, 03 Jan 2014 08:40:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/03/408444.htmlhttp://www.tkk7.com/menglee/comments/408444.htmlhttp://www.tkk7.com/menglee/archive/2014/01/03/408444.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408444.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408444.htmlGiven a collection of integers that might contain duplicates, S, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
]]>[Leetcode] Word Ladder IIhttp://www.tkk7.com/menglee/archive/2014/01/02/408381.htmlMeng LeeMeng LeeThu, 02 Jan 2014 05:59:00 GMThttp://www.tkk7.com/menglee/archive/2014/01/02/408381.htmlhttp://www.tkk7.com/menglee/comments/408381.htmlhttp://www.tkk7.com/menglee/archive/2014/01/02/408381.html#Feedback0http://www.tkk7.com/menglee/comments/commentRss/408381.htmlhttp://www.tkk7.com/menglee/services/trackbacks/408381.htmlGiven two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:
Only one letter can be changed at a time
Each intermediate word must exist in the dictionary
For example,
Given:
start = "hit"
end = "cog"
dict = ["hot","dot","dog","lot","log"]
Return
[
["hit","hot","dot","dog","cog"],
["hit","hot","lot","log","cog"]
]
Note:
All words have the same length.
All words contain only lowercase alphabetic characters.