摘要:
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.此题目有两种解决思路:1)递归...
阅读全文
posted @ 2014-07-02 21:23
OpenSoucre
阅读(180)
推荐(0)
摘要:
Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution stil...
阅读全文
posted @ 2014-07-02 20:53
OpenSoucre
阅读(196)
推荐(0)
摘要:
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened t...
阅读全文
posted @ 2014-07-02 20:00
OpenSoucre
阅读(216)
推荐(0)
摘要:
Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to...
阅读全文
posted @ 2014-07-02 18:25
OpenSoucre
阅读(163)
推荐(0)
摘要:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value t...
阅读全文
posted @ 2014-07-02 18:14
OpenSoucre
阅读(168)
推荐(0)
摘要:
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.int key[]={1000, 900, 500, 400, 100,90, 50, ...
阅读全文
posted @ 2014-07-02 17:54
OpenSoucre
阅读(155)
推荐(0)
摘要:
给定一颗二叉树,和两个给定的结点,求出这两个结点之间的距离拿到题目时不要认为是求出二叉树的结点之间的最大距离,题目是求两个结点的之间的距离题目有几种情况两个结点分布在根节点的左子树或者右子树一个结点分布在根节点的左子树,一个结点分布在根节点的右子树这两个结点是兄弟结点一个结点是另外结点的祖先结点本题...
阅读全文
posted @ 2014-07-02 17:33
OpenSoucre
阅读(1617)
推荐(0)
摘要:
Mergeksorted linked lists and return it as one sorted list. Analyze and describe its complexity.合并k个排序列表解题思路是:取出 k个元素进行堆排序,每次取出最小的元素,插入链表中即可注意本题利用了c++...
阅读全文
posted @ 2014-07-01 23:17
OpenSoucre
阅读(613)
推荐(0)
摘要:
题目大致意思是给出n个排序list,每个list只有两个方法(1)bool goNext(); 判断是否有下一个元素,没有元素返回false, 有元素返回true(2)int next(); 返回下一个链表的值求这个n个排序链表的交集,也就是每个链表都有的元素本题的基本思路是
阅读全文
posted @ 2014-07-01 22:24
OpenSoucre
阅读(343)
推荐(0)
摘要:
Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the origi...
阅读全文
posted @ 2014-07-01 21:41
OpenSoucre
阅读(155)
推荐(0)
摘要:
Given a binary tree, find the maximum path sum.The path may start and end at any node in the tree.For example:Given the below binary tree, 1 ...
阅读全文
posted @ 2014-07-01 21:20
OpenSoucre
阅读(174)
推荐(0)
摘要:
Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1...
阅读全文
posted @ 2014-07-01 20:52
OpenSoucre
阅读(208)
推荐(0)
摘要:
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum =...
阅读全文
posted @ 2014-07-01 20:12
OpenSoucre
阅读(175)
推荐(0)
摘要:
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.Fo...
阅读全文
posted @ 2014-07-01 19:41
OpenSoucre
阅读(214)
推荐(0)
摘要:
Given a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note...
阅读全文
posted @ 2014-07-01 18:46
OpenSoucre
阅读(127)
推荐(0)
摘要:
Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ],...
阅读全文
posted @ 2014-06-29 17:53
OpenSoucre
阅读(203)
推荐(0)
摘要:
参考Babylonian method(x0 越接近S的平方根越好)class Solution {public: int sqrt(double x) { if(x == 0) return 0; double root = x/2, tolerance = 1....
阅读全文
posted @ 2014-06-29 17:30
OpenSoucre
阅读(205)
推荐(0)
摘要:
Implement pow(x,n).明显的二分解决由于n不可能总是偶数, 如果n是正奇数,那么n个x的乘积等于两部分的乘积再乘以x 如果n是负奇数,那么n个x的乘积等于两部分乘积再乘以1/xclass Solution {public: double pow(double x, int ...
阅读全文
posted @ 2014-06-29 16:45
OpenSoucre
阅读(176)
推荐(0)
摘要:
You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?矩阵的旋转如1 2 34 5 67 8...
阅读全文
posted @ 2014-06-29 16:34
OpenSoucre
阅读(169)
推荐(0)
摘要:
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-nega...
阅读全文
posted @ 2014-06-29 16:14
OpenSoucre
阅读(132)
推荐(0)
摘要:
Given a stringsconsists of upper/lower-case alphabets and empty space characters' ', return the length of last word in the string.If the last word doe...
阅读全文
posted @ 2014-06-29 15:21
OpenSoucre
阅读(179)
推荐(0)
摘要:
本题就是求所有连续子数列的和开始拿到题目还以为求的时数列子集的和,认真看到题目才知道是连续子数列循环遍历即可 int findSum(vector array) { int sum = 0; for(int i = 0 ; i < array.size(); ++...
阅读全文
posted @ 2014-06-29 14:47
OpenSoucre
阅读(193)
推荐(0)
摘要:
典型的条件概率题目。事件A在另外一个事件B已经发生条件下的发生概率。条件概率表示为P(A|B),读作“在B条件下A的概率”。若只有两个事件A,B,那么,P(A|B)=P(AB)/P(B)本题的设事件Alice赢为B,事件Alice投掷数字x为A,则事件Alice投掷数字x且赢为AB则求在Alice赢...
阅读全文
posted @ 2014-06-29 14:44
OpenSoucre
阅读(205)
推荐(0)
摘要:
Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histog...
阅读全文
posted @ 2014-06-26 23:57
OpenSoucre
阅读(206)
推荐(0)
摘要:
Given amxnmatrix, if an element is 0, set its entire row and column to 0. Do it in place.click to show follow up.Follow up:Did you use extra space?A s...
阅读全文
posted @ 2014-06-26 22:55
OpenSoucre
阅读(190)
推荐(0)
摘要:
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest le...
阅读全文
posted @ 2014-06-26 22:16
OpenSoucre
阅读(153)
推荐(0)
摘要:
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth...
阅读全文
posted @ 2014-06-26 21:43
OpenSoucre
阅读(150)
推荐(0)
摘要:
【转载】【啊哈!算法】系列8:巧妙的邻接表(数组实现) http://www.cnblogs.com/ahalei/p/3651334.html 之前我们介绍过图的邻接矩阵存储法,它的空间和时间复杂度都是N2,现在我来介绍另外一种存储图的方法:邻接表,这样空间和时间复杂度就都是M。对于稀疏图来说...
阅读全文
posted @ 2014-06-26 17:56
OpenSoucre
阅读(228)
推荐(0)
摘要:
第一节 镖局运镖-图的最小生成树所谓最小生成树,就是在一个具有N个顶点的带权连通图G中,如果存在某个子图G',其包含了图G中的所有顶点和一部分边,且不形成回路,并且子图G'的各边权值之和最小,则称G'为图G的最小生成树。最小生成树的三个性质最小生成树不能有回路最小生成树可能是一个,也可能有多个最小...
阅读全文
posted @ 2014-06-26 16:11
OpenSoucre
阅读(580)
推荐(0)
摘要:
第3节 堆排序把n个元素建立一个堆,首先将这n个结点以自顶向下、从左到右的方式从1到n编码,这样可以把n个结点转换成一颗完全二叉树紧接着从最后一个非叶子结点(结点编号为n/2)开始到根节点(结点编号为1),逐个扫描所有结点,根据需要将当前结点向下调整,直到以当前结点为根结点的子树符合堆的特性。#in...
阅读全文
posted @ 2014-06-26 12:53
OpenSoucre
阅读(294)
推荐(0)
摘要:
Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from l...
阅读全文
posted @ 2014-06-26 00:23
OpenSoucre
阅读(195)
推荐(0)
摘要:
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array[−2,1,−3,4,−1,2,1,...
阅读全文
posted @ 2014-06-25 23:38
OpenSoucre
阅读(116)
推荐(0)
摘要:
Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.时间复杂度O(n^3),最大全1子矩阵,利用直方图求解,可以参考对...
阅读全文
posted @ 2014-06-25 23:10
OpenSoucre
阅读(147)
推荐(0)
摘要:
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number ...
阅读全文
posted @ 2014-06-25 22:10
OpenSoucre
阅读(170)
推荐(0)
摘要:
Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated num...
阅读全文
posted @ 2014-06-25 21:58
OpenSoucre
阅读(148)
推荐(0)
摘要:
Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matri...
阅读全文
posted @ 2014-06-25 21:40
OpenSoucre
阅读(154)
推荐(0)
摘要:
Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".class Solution {public: string addBinary(str...
阅读全文
posted @ 2014-06-25 21:07
OpenSoucre
阅读(172)
推荐(0)
摘要:
题目的意思是给一个01的字符串数组,让你去求解满足棋盘条件的最大棋盘棋盘的条件是: 相邻元素的值不能相同此题有点像求全1的最大子矩阵,当时求全1的最大子矩阵是用直方图求解的本题可以利用直方图求解首先找到子矩阵的两个顶点坐标(x0,y0),(x1,y1)我们能遍历开始和结束列,y0=i, y1=j,...
阅读全文
posted @ 2014-06-25 18:44
OpenSoucre
阅读(255)
推荐(0)
摘要:
题目的意思是给你一组数,然后不断的进行除法(注意是大数除以小数),然后将得到的结果加入这组数种然后继续进行除法,直到没有新添加的数为止此题按照提议模拟即可注意要保持元素的不同 int CountNumbers(vector numbers) { set ss(numbers.b...
阅读全文
posted @ 2014-06-25 16:44
OpenSoucre
阅读(208)
推荐(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"tw...
阅读全文
posted @ 2014-06-24 22:41
OpenSoucre
阅读(144)
推荐(0)