上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 20 下一页
摘要: A: 回溯法解决,和八皇后一个思路。用三个二维数组标记数字在行,列,block是否出现过。 vector> row; vector> col; vector> block; void solveSudoku(vector > &board) { // Start typing your C/C++ solution below // DO NOT write int main() function row.clear(); col.clear(); block.clear(); ... 阅读全文
posted @ 2013-09-30 14:03 summer_zhou 阅读(162) 评论(0) 推荐(0) 编辑
摘要: Q: 九宫独。判断一个board是否是有效的九宫独。用三个二维数组标记,row,col,block:row[i][j]表示第i行数字j+1是否已经出现过,col和block数组同理。对于board[i][j],对应的block计算是:i - i%3+j/3 bool isValidSudoku(vector > &board) { // Start typing your C/C++ solution below // DO NOT write int main() function int n = board.size(); if... 阅读全文
posted @ 2013-09-30 10:41 summer_zhou 阅读(171) 评论(0) 推荐(0) 编辑
摘要: Q:Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal is to reach the last index in the minimum number of jumps.For example:Given array A =[2,3,1,1,4]The minimum 阅读全文
posted @ 2013-09-29 22:33 summer_zhou 阅读(168) 评论(0) 推荐(0) 编辑
摘要: f(n) = f(n-1)+f(n-2) DP int climbStairs(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n<=2) return n; int a = 1,b = 2; int cnt; for(int i=3;i<=n;i++) { cnt = a+b; a = b; ... 阅读全文
posted @ 2013-09-29 16:56 summer_zhou 阅读(131) 评论(0) 推荐(0) 编辑
摘要: Q: 0-1背包问题。dp[i][j]: 表示S[0,i-1]包含T[0,j-1]的子序列个数。那么dp[i][j] = dp[i-1][j] + (S[i]==T[j]?dp[i-1][j-1]:0);因此时间复杂度为O(m+n)。空间复杂度可以优化到O(n). m表示S的长度,n表示T的长度。dp[j]表示第i次迭代的值。 int numDistinct(string S, string T) { // Start typing your C/C++ solution below // DO NOT write int main() function ... 阅读全文
posted @ 2013-09-29 16:48 summer_zhou 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 递归 int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return 0; int ld = maxDepth(root->left); int rd = maxDepth(root->right); return max(ld+1,rd+1); } 阅读全文
posted @ 2013-09-29 10:46 summer_zhou 阅读(125) 评论(0) 推荐(0) 编辑
摘要: 树的问题,优先考虑递归。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution {public: bool isSameTree(TreeNode *p, TreeNode *q) { // Start typing your C/C++ solu... 阅读全文
posted @ 2013-09-29 10:36 summer_zhou 阅读(152) 评论(0) 推荐(0) 编辑
摘要: Q:Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longes 阅读全文
posted @ 2013-09-25 23:35 summer_zhou 阅读(169) 评论(0) 推荐(0) 编辑
摘要: Q:Given a strings1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation ofs1="great": great / \ gr eat / \ / \g r e at / \ a tTo scramble the string, we may choose any non-leaf ... 阅读全文
posted @ 2013-09-24 23:06 summer_zhou 阅读(225) 评论(0) 推荐(0) 编辑
摘要: Q: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.Note: The sequence of in 阅读全文
posted @ 2013-09-24 20:35 summer_zhou 阅读(136) 评论(0) 推荐(0) 编辑
上一页 1 ··· 7 8 9 10 11 12 13 14 15 ··· 20 下一页