摘要: 1. A2. Ctrl+A3. Ctrl+C4. Ctrl+VIf you can only press the keyboard for N times (with the above four keys), please write a program to produce maximum numbers of A. If possible, please also print out the sequence of keys.So the input parameter is N (No. of keys that you can press), the output is M (No. 阅读全文
posted @ 2012-11-04 22:17 chkkch 阅读(446) 评论(1) 推荐(0) 编辑
摘要: Implement a queue in which push_rear(), pop_front() and get_min() are all constant time operations.http://www.careercup.com/question?id=7263132给出了很精彩的解答。主要的思想是维护一个min_queue,保存当前队列最小值,当新加入一个元素时,min_queue遵照这样的规则从min_queue的尾部开始和新加入元素(key)比较,当min_queue.back() > key时,弹出back(),直到back()<= key,或者min_q 阅读全文
posted @ 2012-11-04 20:06 chkkch 阅读(499) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, find out number of ways in which you can select increasing subsequences of length k(k<=n).for eg array is 1 4 6 2 5 & k=3then the answer is :1 4 5, 1 2 5,1 4 6,so ways are 5he first made me to write a recurrence then asked me to memoize that这题可以用dfs做,但显然耗时是指数级的。这种类 阅读全文
posted @ 2012-11-04 12:02 chkkch 阅读(439) 评论(0) 推荐(0) 编辑
摘要: If the Fibonacci series is 1,2,3,5,8,13,….. then 10 can be written as 8 + 2 ==> 10010 and 17 can be written as 13 + 3 + 1 ==> 100101. Got it?? The Question was, given n, I need to get all possible representations of n in Fibonacci Binary Number System. as 10 = 8 + 2 ==> 10010 also 10 = 5 + 阅读全文
posted @ 2012-11-01 23:16 chkkch 阅读(430) 评论(0) 推荐(1) 编辑
摘要: You have two BST, you have to merge them into a single BST, inplace and linear time.http://www.careercup.com/question?id=8255895其中,一位仁兄给出了一个非常漂亮的解答:we could do it in O(m+n) (m, n sizes of both bsts)1) Covert Tree1 in to circular doubly linked list. O(m)Search for "tree list recursion problem&qu 阅读全文
posted @ 2012-11-01 22:27 chkkch 阅读(435) 评论(0) 推荐(0) 编辑
摘要: 接上题排列,稍作修改就可以做一个非递归的组合数了。#include <iostream>#include <vector>#include <stack>using namespace std;void permutation(int a[], int n, int k){ vector<int> b(k); stack<int> s; s.push(-1); while(!s.empty()) { if (s.size() > k) { for(int i = 0; i < k; i++) ... 阅读全文
posted @ 2012-10-31 23:37 chkkch 阅读(755) 评论(1) 推荐(0) 编辑
摘要: 打印一个数组的全排列,要求非递归。模拟递归当然会想到用栈模拟,最主要的是考虑初始的压入栈中的值,这里首先想到递归调用是用一个for循环遍历0到n-1的索引,看哪个可以用(比如k)就递归调用函数,然后返回的时候在k+1继续循环查找下去。这里的关键是如何能模拟循环的一个过程,于是我考虑到用出栈的值的下一个值来继续for循环的下一个值,于是初始从0开始,我们就可以先压入-1,然后再把它弹出栈,取下一个值作为for循环的初始值。然后当栈大小>n时打印。 1 #include <iostream> 2 #include <vector> 3 #include <sta 阅读全文
posted @ 2012-10-31 23:28 chkkch 阅读(667) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.参考这里的解答:http://dl.dropbox.com/u/19732851/LeetCode/FirstMissingPositive.html主要的思想就是把对应的数放到对应的索引上,例如1放到A[1]上,这样只需要 阅读全文
posted @ 2012-10-31 11:34 chkkch 阅读(2545) 评论(2) 推荐(0) 编辑
摘要: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, ...1is read off as"one 1"or11.11is read off as"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence.Note: The sequence of inte 阅读全文
posted @ 2012-10-31 11:12 chkkch 阅读(2443) 评论(0) 推荐(0) 编辑
摘要: A message containing letters fromA-Zis being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of ways to decode it.For example,Given encoded message"12", it cou 阅读全文
posted @ 2012-10-30 18:13 chkkch 阅读(1399) 评论(0) 推荐(0) 编辑