`
bcyy
  • 浏览: 1824683 次
文章分类
社区版块
存档分类
最新评论
文章列表
原题链接:http://oj.leetcode.com/problems/recover-binary-search-tree/这道题是要求恢复一颗有两个元素调换错了的二叉查找树。一开始拿到可能会觉得比较复杂,其实观察出规律了就比较简单。主要还是利用二叉查找树的主要性质,就是中序遍历是有序的性质。那么如果其中有元素被调换了,意味着中序遍历中必然出现违背有序的情况。那么会出现几次呢?有两种情况,如果是中序遍历相邻的两个元素被调换了,很容易想到就只需会出现一次违反情况,只需要把这个两个节点记录下来最后调换值就可以;如果是不相邻的两个元素被调换了,举个例子很容易可以发现,会发生两次逆序的情况,那么这时 ...
Background In one of the countries of Caribbean basin all decisions were accepted by the simple majority of votes at the general meeting of citizens (fortunately, there were no lots of them). One of the local parties, aspiring to come to power as lawfully as possible, got its way in putting ...
Müller tried to catch Stierlitz red-handed many times, but always failed because Stierlitz could ever find some excuse. Once Stierlitz was looking through his email messages. At that moment, Müller entered secretly and watched a meaningless sequence of symbols appear on the screen. “A cipher me ...
Let us consider four disks intersecting as in the figure. Each of the three shapes formed by the intersection of three disks will be called apetal. Write zero or one on each of the disks. Then write on each petal the remainder in the division by two of the sum of integers on the disks that c ...
A new educating program was received by the kindergarten. Of course, children have discovered it immediately and want to play with it as soon as possible. In order to let them do it the program has to be copied to all theNcomputers that the kindergarten had bought just before the default of 199 ...
Vladislav Isenbaev is a two-time champion of Ural, vice champion of TopCoder Open 2009, and absolute champion of ACM ICPC 2009. In the time you will spend reading this problem statement Vladislav would have solved a problem. Maybe, even two… Since Vladislav Isenbaev graduated from the Spec ...
原题链接:http://oj.leetcode.com/problems/gray-code/这道题要求求出n位的格雷码对应的二进制数,主要在于找到一种格雷码的递增方法(格雷码并不是唯一的,可以有多种)。我们来看看有了n-1位的格雷码如何得到n位的
原题链接:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/这道题其实还是树的层序遍历Binary Tree Level Order Traversal,如果不熟悉的朋友可以先看看哈。不过这里稍微做了一点变体,就是在遍历的时候偶数层自左向右,而奇 ...
原题链接:http://oj.leetcode.com/problems/scramble-string/这道题看起来是比较复杂的,如果用brute force,每次做切割,然后递归求解,是一个非多项式的复杂度,一般来说这不是面试官想要的答案。这其实是一道三维动态规划的题目,我们提出维护量res[i][j][n],其中i是s1的起始字符,j是s2的起始字符,而n是当前的字符串长度,res[i][j][len]表示的是以i和j分别为s1和s2起点的长度为len的字符串是不是互为scramble。有了维护量我们接下来看看递推式,也就是怎么根据历史信息来得到res[i]
It's been quite a number of years since Lich Sandro retired. Sometimes in the evenings, when he feels especially lonely, he takes a book that was presented to him by his student magicians on the occasion of his retirement. This evening the great magician is also reading the book. One of th ...
就是打印下面这两个复杂的式子: LetAn= sin(1–sin(2+sin(3–sin(4+…sin(n))…) LetSn= (…(A1+n)A2+n–1)A3+…+2)An+1 For givenNprintSN Input One integerN. 1 ≤N≤ 200 Output Line containingSN Sample input output
前面学了Trie,那么就即学即用,运用Trie数据结构来解决这道题目。 本题目比较简单,当然可以不使用Trie,不过多用高级数据结构还是很有好处的。 题目: Vova is fond of anime. He is so enthusiastic about this art that he learned to communicate with his Japanese friends using their native language. However, for writing email messages Vova has to use Latin letters. ...
Trie是非常高效的信息检索数据结构, 时间效率会是O(m),其中m是需要搜索的关键字的长度。缺点就是需要的存储空间大。 Trie的特点: 1. 每个Trie的节点都由多个分支构成 2. 每个分支代表可能的关键字的一个字符 3. 需要mark(标志)每个关键字的最后一个字符为leaf node(叶子节点) 英文字母的节点数据结构可以表示如下: struct TrieNode { int value; /* Used to mark leaf nodes */ TrieNode *children[ALPHABET_SIZE]; }; 插入关键字: 1. 关键字的每个字符都作为 ...
原题链接:http://oj.leetcode.com/problems/partition-list/这是一道链表操作的题目,要求把小于x的元素按顺序放到链表前面。我们仍然是使用链表最常用的双指针大法,一个指向当前小于x的最后一个元素,一个进行往前扫描。如果元素大于x,那么继续前进,否则,要把元素移到前面,并更新第一个指针。这里有一个小细节,就是如果不需要移动(也就是已经是接在小于x的最后元素的后面了),那么只需要继续前进即可。算法时间复杂度是O(n),空间只需要几个辅助变量,是O(1)。代码如下:public ListNode partition(ListNode head, int x) ...
原题链接:http://oj.leetcode.com/problems/maximal-rectangle/这是一道非常综合的题目,要求在0-1矩阵中找出面积最大的全1矩阵。刚看到这道题会比较无从下手,brute force就是对于每个矩阵都看一下,总共有m(m+1)/2*n(n+1)/2个子矩阵(原理跟字符串子串类似,字符串的子串数有n(n+1)/2,只是这里是二维情形,所以是两个相乘),复杂度相当高,肯定不是面试官想要的答案,就不继续想下去了。这道题的解法灵感来自于Largest Rectangle in Histogram
Global site tag (gtag.js) - Google Analytics