摘要: As I understand the problem, an entry is is a local minimum (LM) if it is smaller than all of its immediate neighbors. (Note that at the edges there are less neighbors to consider than at interior entries.) In the 2D case the neighbors are defined only in the horizontal and vertical directions, but 阅读全文
posted @ 2013-09-11 01:18 pgu2 阅读(238) 评论(0) 推荐(0)
摘要: bit 和 1 亦或(^)得到与其相反的bitbit 和 1 and (&)得到与其相同的值 阅读全文
posted @ 2013-09-10 04:16 pgu2 阅读(105) 评论(0) 推荐(0)
摘要: Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.Thoughts:1. 两个节点在同一个子树(左子树,右子树), recursive搜索2. 不在同一个子树,当下的根节点就是他们的共同节点Solution:Node* findLCA(Node *tree, Node *n1, Node *n2){ if(!tree || !n1 || !na) return NULL; else if (max(n1->val, n2->val) > 阅读全文
posted @ 2013-09-09 22:12 pgu2 阅读(164) 评论(0) 推荐(0)
摘要: This isFloyd's algorithm for cycle detection. You are asking about the second phase of the algorithm -- once you've found a node that's part of a cycle, how does one find thestartof the cycle?In the first part of Floyd's algorithm, the hare moves two steps for every step of the torto 阅读全文
posted @ 2013-08-29 07:30 pgu2 阅读(233) 评论(0) 推荐(0)
摘要: There arencoins in a line. (Assumenis even). Two players take turns to take a coin from one of the ends of the line until there are no more coins left. The player with the larger amount of money wins.Solution:typedef pair i_pair;int path[100];//define pair here int max(int m, int n, int p, int q){ v 阅读全文
posted @ 2013-08-14 02:38 pgu2 阅读(262) 评论(0) 推荐(0)
摘要: You've been given a list of words to study and memorize. Being a diligent student of language and the arts, you've decided to not study them at all and instead make up pointless games based on them. One game you've come up with is to see how you can concatenate the words to generate the 阅读全文
posted @ 2013-07-31 04:32 pgu2 阅读(216) 评论(0) 推荐(0)
摘要: solution:const int N = 8;int position[N]; // Check if a position is safebool isSafe(int queen_number, int row_position){ // Check each queen before this one for(int i=0; i<queen_number; i++) { // Get another queen's row_position int other_row_pos = ... 阅读全文
posted @ 2013-07-25 10:21 pgu2 阅读(187) 评论(0) 推荐(0)
摘要: A double-square number is an integerXwhich can be expressed as the sum of two perfect squares. For example, 10 is a double-square because 10 = 32+ 12. Your task in this problem is, given X, determine the number of ways in which it can be written as the sum of two squares.For example, 10 can only be 阅读全文
posted @ 2013-07-25 08:20 pgu2 阅读(398) 评论(0) 推荐(0)
摘要: Design a stack that supports push, pop, and retrieving the minimum element in constant time. Can you do this?Solution:struct Node{ int min; int val; Node( int val) {this->min = val; this->val = val;}};class stackNode : public stack{public: void push(Node& sN); void pop(); int getMin(); };v 阅读全文
posted @ 2013-07-25 07:30 pgu2 阅读(327) 评论(0) 推荐(0)
摘要: Given a function which generates a random integer in the range 1 to 7, write a function which generates a random integer in the range 1 to 10 uniformly.solution:int rand10() { int row, col, idx; do { row = rand7(); col = rand7(); idx = col + (row-1)*7; } while (idx > 40... 阅读全文
posted @ 2013-07-25 01:39 pgu2 阅读(271) 评论(0) 推荐(0)