2013年11月6日

Combinations

摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]思路:DFS另外,用next permutation里提到的方法也可以。代码: 1 void search(int start, int n, int k, vector > &result, vector &tmp){ 2 ... 阅读全文

posted @ 2013-11-06 21:16 waruzhi 阅读(190) 评论(0) 推荐(0) 编辑

Populating Next Right Pointers in Each Node II

摘要: 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 still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文

posted @ 2013-11-06 20:44 waruzhi 阅读(134) 评论(0) 推荐(0) 编辑

Sort Colors

摘要: Given an array withnobjects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.Note:You are not suppose to use the l 阅读全文

posted @ 2013-11-06 19:59 waruzhi 阅读(131) 评论(0) 推荐(0) 编辑

Binary Tree Preorder Traversal

摘要: Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?思路:要求递归实现,所以引入一个栈。又因为避免重复进入已访问的节点,所以用pair来记录某个节点是否被访问。代码: 1 vector preorderTraversal(... 阅读全文

posted @ 2013-11-06 17:16 waruzhi 阅读(198) 评论(0) 推荐(0) 编辑

Spiral Matrix II

摘要: Given an integern, generate a square matrix filled with elements from 1 ton2in spiral order.For example,Givenn=3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]]代码: 1 vector > generateMatrix(int n) { 2 // IMPORTANT: Please reset any member data you declared... 阅读全文

posted @ 2013-11-06 16:50 waruzhi 阅读(107) 评论(0) 推荐(0) 编辑

Spiral Matrix

摘要: Given a matrix ofmxnelements (mrows,ncolumns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]You should return[1,2,3,6,9,8,7,4,5].思路:取长和宽里比较小的一个值,逐层输出,剩余一层再单独判断。代码: 1 int min(int a, int b){ 2 if(a spiralO... 阅读全文

posted @ 2013-11-06 11:19 waruzhi 阅读(150) 评论(0) 推荐(0) 编辑

Pascal's Triangle II

摘要: Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?思路:最开始的想法是用两个vector依次记录当前行和上一行的结果。但是要求用k的空间,所以使用一个vector,每次从后往前r[j] = r[j] + r[j-1]即可。代码: 1 vector getRow(int rowIndex) { 2 ... 阅读全文

posted @ 2013-11-06 10:32 waruzhi 阅读(127) 评论(0) 推荐(0) 编辑

Rotate Image

摘要: You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?代码; 1 void rotate(vector > &matrix) { 2 // Start typing your C/C++ solution below 3 // DO NOT write int main() function 4 int n = matrix[0].s... 阅读全文

posted @ 2013-11-06 10:15 waruzhi 阅读(177) 评论(0) 推荐(0) 编辑

导航