上一页 1 ··· 4 5 6 7 8 9 10 11 12 下一页
摘要: 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 straight forward solution using O(mn) space is probably a bad idea.A simple improvement uses O(m+n) space, but still not the best solution.Could you de 阅读全文
posted @ 2013-11-11 20:02 xchangcheng 阅读(121) 评论(0) 推荐(0) 编辑
摘要: You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?class Solution {public: void rotate(vector > &matrix) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will b... 阅读全文
posted @ 2013-11-11 15:16 xchangcheng 阅读(170) 评论(0) 推荐(0) 编辑
摘要: A robot is located at the top-left corner of amxngrid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).How many possible uni 阅读全文
posted @ 2013-11-11 15:06 xchangcheng 阅读(139) 评论(0) 推荐(0) 编辑
摘要: Given amxngrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesthe sum of all numbers along its path.Note:You can only move either down or right at any point in time.解题思路:一个简单的dp,从上往下,从左往右扫一遍即可。class Solution {public: int minPathSum(vector > &grid) { 阅读全文
posted @ 2013-11-11 14:58 xchangcheng 阅读(232) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?解题思路,参看http://www.cnblogs.com/changchengxiao/p/3413294.htmlclass Solution {public: int si... 阅读全文
posted @ 2013-11-11 12:01 xchangcheng 阅读(117) 评论(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]]/** * Definition for binary tree * struct ... 阅读全文
posted @ 2013-11-11 10:36 xchangcheng 阅读(166) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up level order traversal as:[ [15,7] [9,20], [3],]解题思路... 阅读全文
posted @ 2013-11-11 09:49 xchangcheng 阅读(160) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example,Given1->2->3->4, you should return the list as2->1->4->3.Your algorithm should use only constant space. You maynotmodify the values in the list, only nodes itself can be changed.解题思路:链表中1,2交换,3,4交换,5 阅读全文
posted @ 2013-11-10 22:27 xchangcheng 阅读(247) 评论(0) 推荐(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 of the two subtrees ofeverynode never differ by more than 1.解题思路:递归分别求左子树和右子树的高度,然后比较,如果他们相差小于1,则返回高度,否则返回-1。最后root结点若返回-1则false,否则true。/** * Definit 阅读全文
posted @ 2013-11-10 21:59 xchangcheng 阅读(188) 评论(0) 推荐(0) 编辑
摘要: GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]解题思路:顺序加入每一个vector就好,其中注意元素的个数与求和关系。class Solution {public: vector > generate(int numRows) { // IMPORTANT: Please reset any member data you... 阅读全文
posted @ 2013-11-10 20:42 xchangcheng 阅读(1858) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 12 下一页