09 2015 档案
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie...
阅读全文
摘要:本文转自:http://blog.163.com/xychenbaihu@yeah/blog/static/132229655201210975312473/如何查看进程发生缺页中断的次数?用ps -o majflt,minflt -C program命令查看。majflt代表major fault...
阅读全文
摘要:众所周知,HTTP 是一个无状态协议,所以客户端每次发出请求时,下一次请求无法得知上一次请求所包含的状态数据,如何能把一个用户的状态数据关联起来呢? 比如在淘宝的某个页面中,你进行了登陆操作。当你跳转到商品页时,服务端如何知道你是已经登陆的状态? Cookie 首先产生了 cookie 这门技术来解
阅读全文
摘要:过河问题,ABCD四个人,单独过分别需要1,2,5,10分钟,最多两人同时过,并且只有一个手电筒,每次都需要电筒,两人一起按过河慢的时间算,时间在17分钟内。具体步骤。答案: AB-----> 2min A10min B2min
阅读全文
摘要:有时我们希望显式地将对象强制类型转换成另外一种类型。例如,如果想在下面的代码中执行浮点数除法: int i, j; double slope = i / j; 就要使用某种方法将i和/或j显式地转换成double,这种方法称作强制类型转换。命名的强制类型转换 一个命名的强制类型转换具有如...
阅读全文
摘要:f(n)=n(n+1)/2+1原理:第N条直线可以被前N-1条直线分为N段,对于 每1段则将平面分为两份,所以对于前f(n)=f(n-1)+n。f(n-1)=f(n-2)+n-1......f(1)=f(0)+1;f(0)=1;等式左右相加可以得到:f(n)+f(n-1).....+f(0)=f(0...
阅读全文
摘要:按所有的灯都按一下,因为每个灯按一下会引起左右两边的灯状态的改变,所以每个灯杯改变3次,这样所有的灯在按下一千次之后全亮
阅读全文
摘要:解答:#define MIN(A,B) ((A) <= (B) ? (A) : (B)) MIN(*p++, b)会产生宏副作用剖析:这个面试题主要考察宏定义可以实现函数的功能。但是它终归不是函数,宏定义中括弧的”参数“也不是真的参数,在宏展开的时候对”参数“进行的是一对一的替换。C++中另一个进行...
阅读全文
摘要:本文转自:http://i.cnblogs.com/EditPosts.aspx?opt=1时常在cpp的代码之中看到这样的代码:#ifdef __cplusplus extern "C" { #endif //一段代码 #ifdef __cplusplus } #endif这样的代码到底是什么意思...
阅读全文
摘要:在C++中,类的对象建立分为两种,一种是静态建立,如A a;另一种是动态建立,如A* ptr=new A;这两种方式是有区别的。 静态建立一个类对象,是由编译器为对象在栈空间中分配内存,是通过直接移动栈顶指针,挪出适当的空间,然后在这片内存空间上调用构造函数形成一个栈对象。使用这种方法,直接调...
阅读全文
摘要:我们使用一个非常简单的模型,并假设它是最简单的HTTP请求,不使用代理并且使用的是IPv4 1. 浏览器首先判断使用的是什么协议(ftp/http),然后对URL进行安全检查。最后浏览器查看缓存,如果请求的对象在缓存中并且是比较新的。那么直接跳到步骤9 2. 浏览器请求OS返回服务器的IP地...
阅读全文
摘要:第一次,操作门1,2,3,4,...,100 第二次,操作门2,4,6,8,...,100 第三次,操作门3,6,9,12,...,99 第4次,操作门4,8,12,16,...,100 ... 第100次,操作门100 上面操作的意思是:如果原来门市open的,就关掉它;如果原来是...
阅读全文
摘要:题目来源:《剑指offer》面试题21 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数。在该栈中,调用min,push以及pop的时间复杂度都是O(1)。 分析:假设用于存储主要数据的栈称为数据栈。我们增加一个辅助栈,它的栈顶元素永远是当前数据栈中元素的最小值。...
阅读全文
摘要:IP地址由三类:单播地址(目的端为单个主机),广播地址(目的端为给定网络上的所有主机)以及多播地址(目的端为同一组内的所有主机)。 广播和多播仅应用于UDP,它们需要将报文同时传往多个接收者的应用十分重要。TCP是一个面向连接的协议,它意味着分别运行于两主机(由IP地址确定)内的两进程(由端...
阅读全文
摘要:Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the arr...
阅读全文
摘要:Given an integer array of sizen, find all elements that appear more than⌊ n/3 ⌋times. The algorithm should run in linear time and in O(1) space.分析:和Ma...
阅读全文
摘要:Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.For example, givenn...
阅读全文
摘要:题目来源:《剑指offer》面试题36 题目:在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这两个数组中的逆序对的总数。例如数组{7,5,6,4}中,一共存在5个逆序对,分别是(7,6),(7,5),(7,4),(6,4)和(5,4)。 下面...
阅读全文
摘要:Write a program to find then-th ugly number.Ugly numbers are positive numbers whose prime factors only include2, 3, 5. For example,1, 2, 3, 4, 5, 6, 8...
阅读全文
摘要:题目来源:《剑指offer》面试题45 题目:0,1,。。。,n-1这n个数字排成一个圆圈,从数字0开始每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。 解法一:经典解法,用环形链表模拟圆圈。这种方法每删除一个数字需要m步运算,总共有n个数字,因此总的时间复杂度是O(mn)...
阅读全文
摘要:启动和停止: gdb //使用gdb调试program可执行文件,注意在编译时gcc要加入-g参数。 gdb core //用 gdb 同时调试一个运行程序和 core 文件,core 是程序异常终止后 core dump 后产生的文件。 gdb //如果你的程序是一个服务程序,那...
阅读全文
摘要:本文转自:http://www.cnblogs.com/panfeng412/archive/2011/11/06/segmentation-fault-in-linux.html1. 段错误是什么段错误是指访问的内存地址是一个非法地址或者以非法的方式访问一个合法地址(比如,以写方式访问只读地址)。...
阅读全文
摘要:对于跨越多字节的程序对象,我们必须建立两个规则:这个对象的地址是什么,以及在存储器中如何安排这些字节。在几乎所有的机器上,多字节对象都被存储为连续的字节序列,对象的地址为所使用字节的最小地址。例如,假设一个型为int变量x的地址为0x100,也就是说,地址表达式&x的值为0x100。那么,x的4...
阅读全文
摘要:auto_ptr auto_ptr是当前C++标准库中提供的一种智能指针。 auto_ptr在构造时获取某个对象的所有去(ownership),在析构时释放该对象。我们可以这样使用auto_ptr来提高代码安全性:int* p = new int(0);auto_ptr ap(p); 从此我们...
阅读全文
摘要:Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For exampl...
阅读全文
摘要:内存足够的情况: 可以使⽤用类似quick sort的思想进行,均摊复杂度为O(n),算法思想如下:• 随机选取一个元素,将比它小的元素放在它左边,比它大的元素放在右边• 如果它恰好在中位数的位置,那么它就是中位数,可以直接返回• 如果小于它的数超过一半,那么中位数一定在左半边,递归到左边处理• 否...
阅读全文
摘要:物理内存的管理 Linux管理物理内存是使用分页机制实现的。为了使分页机制在32位和64位体系结构下高效工作,Linux采用了一个四级分页策略。 Linux支持多种内存分配机制。分配物理内存页框的主要机制是页面分配器,它使用了著名的伙伴算法作为物理内存分配机制。 管理一块内存的基本思想如下...
阅读全文
摘要:Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as00000010100101000001111010011100), return ...
阅读全文
摘要:0-1背包问题:有一个贼在偷窃一家商店时,发现有n件物品,第i件物品价值vi元,重wi磅,此处vi与wi都是整数。他希望带走的东西越值钱越好,但他的背包中至多只能装下W磅的东西,W为一整数。应该带走哪几样东西?这个问题之所以称为0-1背包,是因为每件物品或被带走;或被留下;小偷不能只带走某个物品...
阅读全文
摘要:Given a non-negative integernum, repeatedly add all its digits until the result has only one digit.For example:Givennum = 38, the process is like:3 + ...
阅读全文
摘要:Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is1 -> 2 -> 3 -> ...
阅读全文
摘要:Find the contiguous subarray within an array (containing at least one number) which has the largest product.For example, given the array[2,3,-2,4],the...
阅读全文
摘要:答案是7次。1. 首先将25匹马分成5组a、b、c、d、e进行比赛。比赛的次数就是5次。得到每组的第一名,分别编号a1,b1,c1,d1,e1。2. 然后我们将每组的第一名进行比赛,得出结果。假设a1>b1>c1>d1>e1。(大于号表示a1比b1快,1表示第一名)。在这个地方我们可以推断出,a1是...
阅读全文
摘要:Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For exam...
阅读全文
摘要:Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3...
阅读全文
摘要:Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space i...
阅读全文
摘要:A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any po...
阅读全文
摘要:You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping yo...
阅读全文
摘要:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.N...
阅读全文
摘要:Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted fr...
阅读全文
摘要:题目来源:写一个函数,求两个整数之和,要求在函数体内不得使用+、-、×、÷四则运算符号。 分析:对于5+17=22.我们可以分三步进行:第一步只做各位相加不进位,此时相加的结果是12(个位数5和7中有进位,进位的值是10);第二步做进位,5+7中有进位,进位的值是10;第三步把前面两个结果加起...
阅读全文
摘要:题目来源:《编程之美》2.10 题目:同时找出数组中的最大数和最小数 分析:最基本的方法是两次线性扫描数组,分别找出最大数和最小数,时间复杂度为O(n)。其实我们可以一次扫描即可。首先,我们将输入元素相互进行比较,然后把较小的与当前最小值比较,把较大的与当前最大值进行比较。这样,对每两个元素...
阅读全文
摘要:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the ord...
阅读全文
摘要:题目来源:《剑指offer》面试题40 题目:一个整形数组除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。要求时间复杂度O(n),空间复杂度O(1) 分析:运用异或的思想。我们从头到尾一次异或数组中的每一个数字,那么最终得到的结果就是两个只出现一次的数字的异或结...
阅读全文
摘要: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...
阅读全文
摘要:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, ...
阅读全文
摘要:Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1相似题目:《剑指offer》面试题19/** * Definition for...
阅读全文
摘要:题目来源:《剑指offer》面试题30 题目:输入n个整数,找出其中最小的k个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1、2、3、4. 解法一:利用快速排序Partition的思想来解决这个问题。如果基于数组的第k个数字来调整,使得比第k个数字小的所有数字都位...
阅读全文
摘要:题目来源:《剑指offer》面试题31、《编程之美》2.14 题目:输入一个整形数组,数组里有正数也有负数。数组中一个或连续多个整数组成一个子数组。求所有子数组的和的最大值 解法一:假设id代表自序列的一个起点,j代表终点。如果a[i]是负的,那么它不可能代表最优子序列的起点,因为任何包含a...
阅读全文
摘要:Given an array of sizen, find the majority element. The majority element is the element that appears more than⌊ n/2 ⌋times.You may assume that the arr...
阅读全文
摘要:Reverse a singly linked list. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *next; 6 * ...
阅读全文
摘要:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia:...
阅读全文
摘要:Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's ...
阅读全文
摘要:题目来源:《剑指offer》面试题58 题目:给定一个二叉树和其中一个节点,如何找出中序遍历顺序的下一个节点?树中的节点除了有两个分别指向左右子节点的指针以外,还有一个指向父节点的指针。 分析:有如下几种情况: 如果一个节点有右子树,那么它的下一个节点就是它的右子树中的最左子节点。 ...
阅读全文
摘要:题目来源:《剑指offer》面试题27 题目:输入是一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新节点,只能调整树种节点指针的指向。二叉树节点的定义如下:struct BinaryTreeNode { int val; BinaryTreeNode* l...
阅读全文
摘要:题目:输入一个整数数组,判断该数组是不是二叉搜索树的后序遍历序列的结果,如果是,则返回true,如果不是则返回false。假设输入的数组的任意两个数字都互不相同。 分析:在后序遍历得到的序列中,最后一个数字是根节点的值。数组中前面的数字可以分为两个部分:第一部分是左子树节点的值,它们都比根节点...
阅读全文
摘要:从上往下打印二叉树的每个节点,同一层的节点按照从左到右的顺序打印。 从上到下打印二叉树的规律:每一次打印一个节点的时候,如果该节点有子节点,则把该节点的子节点放到一个队列的末尾。接下来到队列的头部取出最早进入队列的节点,重复前面的打印操作,直到队列中所有的节点都被打印出来为止。 struct...
阅读全文
摘要:题目来源:《剑指offer》面试题18 输入两颗二叉树A和B,判断B是不是A的子结构。二叉树的节点定义如下:struct BinaryTreeNode { int value; BinaryTreeNode* left; BinaryTreeNode* ri...
阅读全文
摘要:相关题目:《剑指offer》面试题7templateclass Queue { public: Queue(); ~Queue(); void Push(const T& value) { s1.push(value): } void...
阅读全文
摘要:Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest...
阅读全文
浙公网安备 33010602011771号