摘要: 给定一棵树的先序遍历和中序遍历结果,重新构建这棵树。解决思路:1. 从先序遍历序列中找到root节点2. 在中序遍历序列中找到root出现的下标位置,记为root_iter. root_iter左边的为左子树的中序遍历序列,长度为lTreeSize, 右边为右子树的中序遍历序列。3. 先序遍历序列中... 阅读全文
posted @ 2015-04-17 17:19 Apprentice.Z 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 因为忙着做实验写paper,刷题的进度放慢了一点。爬上来更新一下最近做的几道题目。Given a number represented as an array of digits, plus one to the number.模拟问题。给定输入是一个vector数组,输出加1后的计算结果。将进位carry初始值置为1,从数组的最后一位开始加,注意最高位的进位。class Solution {public: vector plusOne(vector &digits) { // IMPORTANT: Please reset any member data you decl... 阅读全文
posted @ 2013-11-14 19:44 Apprentice.Z 阅读(134) 评论(0) 推荐(0) 编辑
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).今天的最后一题Triangle是经典的动态规划问题,第i行... 阅读全文
posted @ 2013-10-29 16:01 Apprentice.Z 阅读(152) 评论(0) 推荐(0) 编辑
摘要: 今天的练习题是triangle三件套:Triangle, PASCAL Triangle, PASCAL Triangle II.PASCAL Triangle就是小学奥数中的“杨辉三角”, 存在如下关系triangle[i+1][j+1] = triangle[i][j]+triangle[i][j+1]即第i+1行第j+1个元素为第i行第j个元素与第j+1个元素之和PASCAL Triangle的题目要求:输出PASCAL Triangle的前numRows行GivennumRows, generate the firstnumRowsof Pascal's triangle.Fo 阅读全文
posted @ 2013-10-29 15:47 Apprentice.Z 阅读(312) 评论(0) 推荐(0) 编辑
摘要: 又一道模拟题,按照一定规律生成字符串,求第n个字符串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"two 1s"or21.21is read off as"one 2, thenone 1"or1211.Given an integern, generate thenthsequence. 阅读全文
posted @ 2013-10-28 13:23 Apprentice.Z 阅读(527) 评论(0) 推荐(0) 编辑
摘要: 给定字符串a,b,分别表示两个二进制数。将a,b的二进制和以字符串形式返回。Given two binary strings, return their sum (also a binary string).For example,a ="11"b ="1"Return"100".思路:模拟二进制加法过程,从最低位开始难点在于进位的计算。记进位符为carry, 该位的和为numint num = a[i]-'0'+b[j]-'0'+carry;carry = num/2;num %= 2;代码 1 cl 阅读全文
posted @ 2013-10-28 13:12 Apprentice.Z 阅读(166) 评论(0) 推荐(0) 编辑
摘要: Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.给定两个有序数组A和B,将B中的元素插入A(假设A有足够大的空间),保持有序性Tip: A和B的大小已知,即合并完成后的数组大小确定。将指针指向A和B的尾 阅读全文
posted @ 2013-10-27 22:57 Apprentice.Z 阅读(132) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thelevel ordertraversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]这道题目是二叉树的层次遍历。思路:借助于队列结构(queue),保存尚未访问儿子节点... 阅读全文
posted @ 2013-10-27 22:52 Apprentice.Z 阅读(176) 评论(0) 推荐(0) 编辑
摘要: 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 order ofO(logn).If the target is not found in the array, return[-1, -1].For example,Given[5, 7, 7, 8, 8, 10]and target value 8,return[3, 4].题目要求:查找有序 阅读全文
posted @ 2013-10-27 11:23 Apprentice.Z 阅读(145) 评论(0) 推荐(0) 编辑
摘要: 求输入数组的全排列输出。我是用经典的递归思路求解的:1. 当输入数组num只有一个元素,将num压入ret, 返回;(递归终止条件)2. 假设num中的元素个数为N,取出num的最后一个元素,记为b求出num[0..N-2]的全排列,将b插入所有可能的位置,得到完整的全排列结果这道题还有其它思路,已经有人做了讨论,戳这里Given a collection of numbers, return all possible permutations.For example,[1,2,3]have the following permutations:[1,2,3],[1,3,2],[2,1,3], 阅读全文
posted @ 2013-10-25 13:59 Apprentice.Z 阅读(311) 评论(0) 推荐(0) 编辑