上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 18 下一页
摘要: 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?对于产生一个新的行用从后往前的方法来更新,这样就只需一个O(k)的空间。 1 class Solution { 2 public: 3 vector<int> getRow(int rowIndex) { 4 // Start typing yo.. 阅读全文
posted @ 2012-11-13 11:44 chkkch 阅读(3272) 评论(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]]模拟题。 1 class Solution { 2 private: 3 vector<vector<int> > ret; 4 public: 5 vector<vector<int> > generate(int numRows) { 6 // Start 阅读全文
posted @ 2012-11-13 11:36 chkkch 阅读(596) 评论(0) 推荐(0) 编辑
摘要: Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved 阅读全文
posted @ 2012-11-13 11:16 chkkch 阅读(9533) 评论(1) 推荐(0) 编辑
摘要: Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.DFS 1 class Solution { 2 private: 3 int ret; 4 int a[100]; 5 bool canUse[100]; 6 public: 7 bool check(int y, int n) 8 { 9 for(int i = 0; i < n; i++)1... 阅读全文
posted @ 2012-11-13 11:01 chkkch 阅读(1484) 评论(0) 推荐(0) 编辑
摘要: Then-queens puzzle is the problem of placingnqueens on ann×nchessboard such that no two queens attack each other.Given an integern, return all distinct solutions to then-queens puzzle.Each solution contains a distinct board configuration of then-queens' placement, where'Q'and'.& 阅读全文
posted @ 2012-11-13 10:58 chkkch 阅读(1982) 评论(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,f[i][j] = min(f[i-1][j], f[i][j-1]) + a[i][j] 1 class Solution { 2 private: 3 int f[1... 阅读全文
posted @ 2012-11-13 10:17 chkkch 阅读(3185) 评论(0) 推荐(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 leaf node.DFS,我们只要叶子节点,和之前的Maximum Depth有点不一样 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 ... 阅读全文
posted @ 2012-11-12 16:53 chkkch 阅读(3555) 评论(0) 推荐(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,−5,4],the contiguous subarray[4,−1,2,1]has the largest sum =6.More practice:If you have figured out the O(n) solution, try coding another solution usi 阅读全文
posted @ 2012-11-12 16:47 chkkch 阅读(1167) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.用DFS搞定 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * ... 阅读全文
posted @ 2012-11-12 16:42 chkkch 阅读(1706) 评论(0) 推荐(0) 编辑
摘要: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.DP。用f[i][j]来记录i行以j列为结尾,往前连续的1的个数。然后再一个O(n^3)的循环来找以(i, j)为右下角的矩形最大的1的面积。 1 class Solution { 2 private: 3 int f[1000][1000]; 4 public: 5 int maximalRectangle(vector<vector< 阅读全文
posted @ 2012-11-12 16:37 chkkch 阅读(3318) 评论(1) 推荐(0) 编辑
上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 18 下一页