上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 17 下一页

2013年11月6日

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) 编辑

2013年11月5日

Gray Code

摘要: The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integernrepresenting the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0.For example, givenn= 2, return[0,1,3,2]. Its gray code s 阅读全文

posted @ 2013-11-05 21:38 waruzhi 阅读(163) 评论(0) 推荐(0) 编辑

Balanced Binary Tree

摘要: 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 of the two subtrees ofeverynode never differ by more than 1.思路:一棵树是平衡二叉树,等价于两个子树都是平衡二叉树,并且两子树的深度差不超过1。代码: 1 int max(int a, int b){ 2 if(a... 阅读全文

posted @ 2013-11-05 21:03 waruzhi 阅读(133) 评论(0) 推荐(0) 编辑

Word Ladder

摘要: Given two words (startandend), and a dictionary, find the length of shortest transformation sequence fromstarttoend, such that:Only one letter can be changed at a timeEach intermediate word must exist in the dictionaryFor example,Given:start="hit"end="cog"dict=["hot",&q 阅读全文

posted @ 2013-11-05 18:49 waruzhi 阅读(196) 评论(0) 推荐(0) 编辑

2013年11月4日

Valid Palindrome

摘要: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.Note:Have you consider that the string might be empty? This is a good question to 阅读全文

posted @ 2013-11-04 22:56 waruzhi 阅读(130) 评论(0) 推荐(0) 编辑

上一页 1 ··· 5 6 7 8 9 10 11 12 13 ··· 17 下一页

导航