Interviews
文章平均质量分 66
YMHH
Enjoy the coding, enjoy the life together
展开
-
Traverse binary tree by level
二叉树按层次遍历,很简单,队列即可。假定是满二叉树,空间复杂度是多少呢?应该是O(n),因为最后的队列包含N/2个叶节点。如果要求O(lgn)空间怎么实现呢?卡住了。。。。原来可以用递归的方式实现O(lgn)的按层次遍历,这时候用的是栈空间。void printLevel(TreeNode *root, int level) { if (root == NULL || level <原创 2014-02-12 23:32:48 · 893 阅读 · 0 评论 -
多线程常见面试题目 - 写给HHM
多线程是基本的面试内容,面过很多人,也被面过。changjaing原创 2014-11-02 16:53:34 · 606 阅读 · 0 评论 -
Shortest unique prefix
Use the shortest unique prefix to represent each word in the array. For example:input: ["zebra", "dog", "duck",”dot”] output: {zebra: z, dog: do, duck: du} [zebra, dog, duck, dove] {原创 2014-11-01 19:39:02 · 1275 阅读 · 0 评论 -
Convert number
Convert integer to column name like Excel. For example:1 -> A2 -> B25原创 2014-04-13 11:38:16 · 993 阅读 · 0 评论 -
Longest common subsequence / substring
最长公共子序列 - 可以不连续最长公共子串 - 需要lia原创 2014-04-24 23:52:36 · 775 阅读 · 0 评论 -
Find minimal subtree
给出一棵二叉树, 每个节点的值不相等。现给出几个值,如(2, 7, 8),找出从根节点开始包含这些值的最小子树。递归来解,原创 2014-04-23 22:44:30 · 717 阅读 · 0 评论 -
打印重复的元素
来自网上,据说是Amazon的面试题。数组有N+M个数字,数字的范围为1 ... N, 打印重复的元素, 要求O(M + N), 不可以用额外的空间。原创 2014-04-07 15:15:29 · 863 阅读 · 1 评论 -
逆序数对
在一个数组中,如果一对数的前后位置与大小顺序相反,即前面的数大于后面的数,那么它们就称为一个逆序数对。统计数组中的逆序数对。原创 2014-04-07 20:55:31 · 951 阅读 · 0 评论 -
Longest Increasing Subsequence
很经典的题目,用简单的DP可以O(n^2)。再进一步可以O(nlgn),这时lis[i]存的是长度为i的最长递增子序列中最大值的最小值;查找更新位置的时候需要用二分查找。二分查找总是写半天,有什么技巧不?// dp.cpp : Defines the entry point for the console application.//#include "stdafx.h"#includ原创 2014-03-23 23:13:19 · 706 阅读 · 0 评论 -
BST to Double Linked List
Convert a binary search to a double linked list. 题意很简单,方法也j原创 2014-05-04 23:38:41 · 1000 阅读 · 0 评论 -
Count zero segments in matrix
Given a n x n matrix consisting of 0 and 1, count the number of segments with 0. A segment is connected cells which are directly adjacent on same row or column.For example, in below matrix, the zero原创 2014-03-16 18:12:32 · 977 阅读 · 1 评论 -
Print tree paths
Given a binary tree, print all the paths from root to leaves.For example:Given the below binary tree, 1 / \ 2 3Print:1 21 3递归的方式很简单,如果要求非递归呢?可以从树的遍历考虑原创 2014-03-16 15:23:29 · 924 阅读 · 0 评论 -
Sort stack
以前写过时间复杂度O(n^2)的,怎么能更快一些呢?可以换个思路,哪些排序算法时间复杂度快过O(n^2)呢?快排,归并,就是这样了,关键是能立马反应过来。// sort.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include #include using name原创 2014-03-15 15:04:47 · 919 阅读 · 0 评论 -
多线程之读写锁
读写锁,也叫共享-独占锁,简单的说就是可以同时有多个读线程,但同一时间只能一个写线程,并且读线程和写线程不能同时存在。怎么自己来实现一个读写锁呢?直接上代码吧,用临界区实现简单的读写锁。也可以用互斥体、信号量等来实现。class ReadWriteLock{public: ReadWriteLock(void); ~ReadWriteLock(void); void readL原创 2014-11-02 19:17:33 · 682 阅读 · 0 评论