10 2014 档案

摘要:1. BST只保存preorder或者postorder就够了,递归有O(n^2)和O(n)算法。非递归有利用栈的O(n)算法。2. Complete binary treelevel traversal就行了。3. Full binary tree用一个bit来保存该结点是internal nod... 阅读全文
posted @ 2014-10-31 21:31 linyx 阅读(468) 评论(0) 推荐(0)
摘要:What is a high-performance data structure? To answer that question, we're used to applying normal considerations like Big-Oh complexity, and memory ov... 阅读全文
posted @ 2014-10-31 19:05 linyx 阅读(477) 评论(0) 推荐(0)
摘要:在线求第k个数做得多了,在线求中位数也是用堆,一个最大堆,一个最小堆。思想大概是这样子的:一个最大堆,一个最小堆,最大堆对应于前n/(n+1)个数,最小堆对应于后n/n+1个数;假设最大堆堆项元素为n1, 最小堆堆顶为n2, 则n1 s2, 那么:如果m >= n1, m插入到最小堆,s2=s2+... 阅读全文
posted @ 2014-10-30 15:54 linyx 阅读(552) 评论(0) 推荐(0)
摘要:不能对自己期望太大,但总是要拼一拼。只求能够发挥自己最好的状态,打一场酣畅淋漓的仗。bless!最后两场重要的战斗,good luck!update:昨天其实整体来说发挥也算正常,早上遇到leetcode原题,但因为是英语面,所以一直紧张,这一点觉得略有问题,code还是不够clean,有点小bug... 阅读全文
posted @ 2014-10-28 12:40 linyx 阅读(206) 评论(0) 推荐(0)
摘要:Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below a... 阅读全文
posted @ 2014-10-23 16:22 linyx 阅读(170) 评论(0) 推荐(0)
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"Corner Cases:Did y... 阅读全文
posted @ 2014-10-23 16:11 linyx 阅读(147) 评论(0) 推荐(0)
摘要:Write a function to find the longest common prefix string amongst an array of strings. 1 class Solution { 2 public: 3 string longestCommonPrefix(v... 阅读全文
posted @ 2014-10-22 16:24 linyx 阅读(138) 评论(0) 推荐(0)
摘要:Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and ... 阅读全文
posted @ 2014-10-21 19:53 linyx 阅读(155) 评论(0) 推荐(0)
摘要:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font ... 阅读全文
posted @ 2014-10-20 18:37 linyx 阅读(184) 评论(0) 推荐(0)
摘要:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->... 阅读全文
posted @ 2014-10-20 09:57 linyx 阅读(162) 评论(0) 推荐(0)
摘要:Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your alg... 阅读全文
posted @ 2014-10-20 09:41 linyx 阅读(136) 评论(0) 推荐(0)
摘要:时间限制:10000ms单点时限:1000ms内存限制:256MB描述Finally, you come to the interview room. You know that a Microsoft interviewer is in the room though the door is lo... 阅读全文
posted @ 2014-10-19 22:59 linyx 阅读(425) 评论(0) 推荐(0)
摘要:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.Yo... 阅读全文
posted @ 2014-10-19 16:12 linyx 阅读(155) 评论(0) 推荐(0)
摘要:Leetcode 加新题了Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array ... 阅读全文
posted @ 2014-10-19 15:00 linyx 阅读(136) 评论(0) 推荐(0)
摘要:谢谢网易! 阅读全文
posted @ 2014-10-19 14:58 linyx 阅读(130) 评论(0) 推荐(0)
摘要:谢谢!希望明天一切顺利! 阅读全文
posted @ 2014-10-17 23:16 linyx 阅读(118) 评论(0) 推荐(0)
摘要:1. 给定rand3()能随机生成整数1到3的函数,写出能随机生成整数1到7的函数rand7();用3*(rand3() - 1) + rand3()生成1-9的数。然后再从1-9中生成1到7.这种思想是基于,rand()产生[0,N-1],把rand()视为N进制的一位数产生器,那么可以使用ran... 阅读全文
posted @ 2014-10-16 22:24 linyx 阅读(580) 评论(0) 推荐(0)
摘要:buddy算法是用来做内存管理的经典算法,目的是为了解决内存的外碎片。避免外碎片的方法有两种:1,利用分页单元把一组非连续的空闲页框映射到非连续的线性地址区间。2,开发适当的技术来记录现存的空闲连续页框块的情况,以尽量避免为满足对小块的请求而把大块的空闲块进行分割。基于下面三种原因,内核选择第二种避... 阅读全文
posted @ 2014-10-16 12:58 linyx 阅读(1025) 评论(0) 推荐(0)
摘要:1. 基本概念1.1 地址(1)逻辑地址:指由程序产生的与段相关的偏移地址部分。在C语言指针中,读取指针变量本身值(&操作),实际上这个值就是逻辑地址,它是相对于你当前进程数据段的地址。(2)线性地址:段中的偏移地址(逻辑地址),加上相应段的基地址就生成了一个线性地址。(3)物理地址: 放在寻址总线... 阅读全文
posted @ 2014-10-16 10:54 linyx 阅读(514) 评论(0) 推荐(0)
摘要:BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器。在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型。BLOB是一个大文件,典型的BLOB是一张图片或一个声音文件,由于它们的尺寸,必须使用特殊的方式来处理(例如:上传、下载或者存放到一个数... 阅读全文
posted @ 2014-10-15 16:52 linyx 阅读(280) 评论(0) 推荐(0)
摘要:现在不敢说懂了,纯给自己科普。wordcount1)将文件拆分成splits,由于测试用的文件较小,所以每个文件为一个split,并将文件按行分割形成对,如图所示。这一步由MapReduce框架自动完成,其中偏移量(即key值)包括了回车所占的字符数(Windows和Linux环境会不同)。2)将分... 阅读全文
posted @ 2014-10-14 23:34 linyx 阅读(169) 评论(0) 推荐(0)
摘要:A*搜索算法,俗称A星算法。这是一种在图形平面上,有多个节点的路径,求出最低通过成本的算法。常用于游戏中的NPC的移动计算,或在线游戏的BOT的移动计算上。该算法像Dijkstra算法一样,可以找到一条最短路径;也像BFS一样,进行启发式的搜索。在此算法中,如果以 g(n)表示从起点到任意顶点n的实... 阅读全文
posted @ 2014-10-14 00:29 linyx 阅读(306) 评论(0) 推荐(0)
摘要:只会写递归的,应该学学非递归的。 也是O(lgn)。比如要计算$a^b$,把b写成二进制,假设$b=11_{(10)}=1011_{(2)}=2^3+2^1+2^0$;所以$a^{11}=a^{2^3+2^1+2^0}=a^{2^3}*a^{2^3}*a^{2^0}$。这样我也就可以把每一位的乘积项... 阅读全文
posted @ 2014-10-12 10:31 linyx 阅读(176) 评论(0) 推荐(0)
摘要:静态库在程序编译时会被连接到目标代码中,程序运行时将不再需要该静态库。编译之后程序文件大,但加载快,隔离性也好。动态库在程序编译时并不会被连接到目标代码中,而是在程序运行是才被载入,因此在程序运行时还需要动态库存在。多个应用程序可以使用同一个动态库,启动多个应用程序的时候,只需要将动态库加载到内存一... 阅读全文
posted @ 2014-10-11 21:11 linyx 阅读(7623) 评论(0) 推荐(0)
摘要:The geometric median of a discrete set of sample points in a Euclidean space is the point minimizing the sum of distances to the sample points. This g... 阅读全文
posted @ 2014-10-11 19:33 linyx 阅读(771) 评论(0) 推荐(0)
摘要:1.一个有10亿条记录的文本文件,已按照关键字排好序存储,设计算法,可以快速的从文件中查找指定关键字的记录。$10亿=10^9\approx2^{30}$,每行记录如果是1kB的话,总共是1TB。将文件分割成1000份,每份1G,load进内存作二分查找即可。2. 设计一个分布式爬虫系统。配置参数:... 阅读全文
posted @ 2014-10-11 15:51 linyx 阅读(301) 评论(0) 推荐(0)
摘要:1. 一种字母游戏这样的给定四个位置 _,_,_,_然后每个位置可以选5个candidates,然后问这些candidates最多可以组成多少个有效的词,字典是给定的。 比如,如果字典是 [cake, bike, fake]我们可以这样选candidates第一个位置可以选 b,c,f,e,d第二个 阅读全文
posted @ 2014-10-11 15:41 linyx 阅读(244) 评论(0) 推荐(0)
摘要:设计一个长连接手机云推送服务。要求:1. 稳定包括两个部分一个是服务器端的稳定性,一个是手机端的稳定性。服务端稳定性,因为使用长连接方案,对服务器的开销和要求很大,推送方案对服务器开发要求很高,海量线程连接下的服务器稳定性是非常具有挑战性的。一般的评判标准包括:同时在线时峰值 (一般按照百万并发连接... 阅读全文
posted @ 2014-10-11 15:40 linyx 阅读(520) 评论(0) 推荐(1)
摘要:1. 在一个m*n二维数组中,每一行都按照从左到右的递增顺排序,每一列都按照从上到下的顺序排序,请完成一个函数,输入这样一个二维数组和一个整数,判断数组中是否含有该整数。1 2 8 92 4 9 124 7 9 126 8 11 15杨氏矩阵。这题有更好的做法吗?O(m+n)是最好的吗?网上说的分治... 阅读全文
posted @ 2014-10-10 23:16 linyx 阅读(214) 评论(0) 推荐(0)
摘要:memcpy的原型:SYNOPSIS #include void *memcpy(void *dest, const void *src, size_t n);DESCRIPTION The memcpy() function copies n bytes fr... 阅读全文
posted @ 2014-10-10 15:11 linyx 阅读(796) 评论(0) 推荐(0)
摘要:static变量(1)static全局变量与普通的全局变量有什么区别?答:全局变量的说明之前再加以static 就构成了静态的全局变量。全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式。这两者在存储方式上并无不同。这两者的区别虽在于非静态全局变量的作用域是整个源程序,当一个源程序由多个... 阅读全文
posted @ 2014-10-10 13:09 linyx 阅读(271) 评论(0) 推荐(0)
摘要:创建定时任务: 1 root@xxj-VirtualBox:~# crontab -l 2 # Edit this file to introduce tasks to be run by cron. 3 # 4 # Each task to run has to be defined throu... 阅读全文
posted @ 2014-10-10 00:45 linyx 阅读(298) 评论(0) 推荐(0)
摘要:Passwords are widely used in our lives: for ATMs, online forum logins, mobile device unlock and door access. Everyone cares about password security. H... 阅读全文
posted @ 2014-10-08 16:13 linyx 阅读(381) 评论(0) 推荐(0)
摘要:类型1有N堆石子,现要将石子有序的合并成一堆,规定如下:每次只能移动任意的2堆石子合并,合并花费为新的一堆石子的数量。设计一个算法,将这N堆石子合并成一堆的总花费最小(或最大)。 此类问题比较简单,就是哈夫曼编码的变形,用贪心算法即可求得最优解。即每次选两堆最少的,合并成新的一堆,直到只剩一堆为止。... 阅读全文
posted @ 2014-10-07 11:21 linyx 阅读(680) 评论(1) 推荐(1)
摘要:由0-9表示的数字串,现使其中4个数字不可用,用余下的数字进行重编码。写出编码和解码函数。前缀编码,前5个剩下的编码用一个数字编码。最后一个以及不可用的4个数字,用两个数字进行编码。 1 class Coding { 2 public: 3 void init(int unc... 阅读全文
posted @ 2014-10-06 11:59 linyx 阅读(328) 评论(0) 推荐(0)
摘要:拓扑排序要用list,不能用vector,确保删除边的开销为O(1)。因为主循环中,总共要从队列中pop掉n个数(点),然后总共要删e条边,删点和边的开销都是O(1)。所以整个时间复杂度就是O(n+e)。如果最终还剩下边,证明存在环,sort失败。 1 bool sort(list > &graph... 阅读全文
posted @ 2014-10-05 23:58 linyx 阅读(207) 评论(0) 推荐(0)
摘要:之前有转过一篇:http://www.cnblogs.com/linyx/p/3638222.html这里按wiki的实现写一遍。解一下下面这道题。假如已知有n个人和m对好友关系(存于数字r)。如果两个人是直接或间接的好友(好友的好友的好友…),则认为他们属于同一个朋友圈,请写程序求出这n个人里一共... 阅读全文
posted @ 2014-10-05 22:23 linyx 阅读(197) 评论(0) 推荐(0)
摘要:波兰式,操作符放在操作数前。逆波兰式,操作符放在操作数后。中序的话,用两个栈,一个存操作符, 一个存操作数,根据操作符优先级来操作。为了处理边界情况,在操作符的栈底和栈顶都放了一个"#"。伪代码如下: 1 stack ops; 2 stack nums; 3 ops.push('#'); 4 5 ... 阅读全文
posted @ 2014-10-05 15:38 linyx 阅读(161) 评论(0) 推荐(0)
摘要:C++异常当然可以通过try...catch处理,不过没有finally关键词。C++资源的释放可以通过RAII实现。RAII,也称为“资源获取就是初始化”,是c++等编程语言常用的管理资源、避免内存泄露的方法。它保证在任何情况下,使用对象时先构造对象,最后析构对象。Destructors shou... 阅读全文
posted @ 2014-10-03 23:41 linyx 阅读(158) 评论(0) 推荐(0)
摘要:1. 动态规划是自底向上 ,备忘录方法是自顶向下。2、动态规划每个子问题都要解一次,但不会求解重复子问题;备忘录方法只解哪些确实需要解的子问题;递归方法每个子问题都要解一次,包括重复子问题。 阅读全文
posted @ 2014-10-01 17:07 linyx 阅读(1476) 评论(0) 推荐(0)
摘要:对齐。 1 #include 2 using namespace std; 3 4 struct S1 { 5 int a; 6 char b; 7 char c; 8 }; 9 10 struct S2 {11 int a;12 char b;13 ... 阅读全文
posted @ 2014-10-01 16:59 linyx 阅读(179) 评论(0) 推荐(0)
摘要:注意空域。unsigned默认是4个字节。 1 #include 2 3 using namespace std; 4 struct B1{ 5 unsigned a:4; 6 unsigned b:4; 7 }; 8 9 struct B2{10 unsigned a... 阅读全文
posted @ 2014-10-01 16:49 linyx 阅读(182) 评论(0) 推荐(0)
摘要:后面两种写法很巧妙。一种利用位域,一种利用取余。这里如果不用unsigned char来做位域的话,大小就为4. unsigned char才符合题目要求。 1 void chess1() { 2 struct { 3 unsigned char a:4; 4 ... 阅读全文
posted @ 2014-10-01 16:25 linyx 阅读(197) 评论(0) 推荐(0)
摘要:有一根长为L的平行于x轴的细木杆,其左端点的x坐标为0(故右端点的x坐标为L)。刚开始时,上面有N只蚂蚁,第i(1≤i≤N)只蚂蚁的横坐标为xi(假设xi已经按照递增顺序排列),方向为di(0表示向左,1表示向右),每个蚂蚁都以速度v向前走,当任意两只蚂蚁碰头时,它们会同时调头朝相反方向走,速度不变... 阅读全文
posted @ 2014-10-01 01:17 linyx 阅读(848) 评论(0) 推荐(0)