上一页 1 ··· 10 11 12 13 14 15 16 17 18 ··· 20 下一页
摘要: Q:Given a linked list and a valuex, partition it such that all nodes less thanxcome before nodes greater than or equal tox.You should preserve the original relative order of the nodes in each of the two partitions.For example,Given1->4->3->2->5->2andx= 3,return1->2->2->4-> 阅读全文
posted @ 2013-09-16 10:57 summer_zhou 阅读(129) 评论(0) 推荐(0) 编辑
摘要: Q:Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].A: 先按interval的start排序,后顺序扫描进行merge vector merge(vector &intervals) { // Start typing your C/C++ solution below // DO NOT write int main() functio... 阅读全文
posted @ 2013-09-16 10:07 summer_zhou 阅读(140) 评论(0) 推荐(0) 编辑
摘要: Q:You are given annxn2D matrix representing an image.Rotate the image by 90 degrees (clockwise).顺时针90度旋转矩阵。in placeA:分两步走:1.沿着主对角线交换两侧的对称元素,2.交换第j列和第n-1-j列。PS:如果是逆时针旋转,则1.沿着主对角线交换两侧的对称元素,2.交换第i行和第n-1-i行 void rotate(vector > &matrix) { // Start typing your C/C++ solution below // DO ... 阅读全文
posted @ 2013-09-15 22:53 summer_zhou 阅读(178) 评论(0) 推荐(0) 编辑
摘要: Q:Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.A: 用N-Queens的方法,当n=12时,会超时。下述方法,把可以放皇后的位置,直接计算出来,不需要每次遍历并判断。void queens(int row,int ld,int rd,int upper,int& cnt){ if(row==upper) cnt++; else { int pos ... 阅读全文
posted @ 2013-08-17 20:34 summer_zhou 阅读(146) 评论(0) 推荐(0) 编辑
摘要: DFS int cnt[10] = {0,0,3,3,3,3,3,4,3,4}; char map[10][5] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; vector letterCombinations(string digits) { // Start typing your C/C++ solution bel 阅读全文
posted @ 2013-08-11 17:32 summer_zhou 阅读(168) 评论(0) 推荐(0) 编辑
摘要: Q:八皇后问题。DFS vector > solveNQueens(int n) { // Start typing your C/C++ solution below // DO NOT write int main() function vector > result; if(n positions; Queens(0,n,positions,result); return result; } void Queens(int ... 阅读全文
posted @ 2013-08-11 16:37 summer_zhou 阅读(156) 评论(0) 推荐(0) 编辑
摘要: Q:Given a set of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Thesamerepeated number may be chosen fromCunlimited number of times.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � , 阅读全文
posted @ 2013-08-10 16:24 summer_zhou 阅读(117) 评论(0) 推荐(0) 编辑
摘要: Q:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"A: DFS问题。 void generate_aux(int leftcount,int rightcount, 阅读全文
posted @ 2013-08-10 15:53 summer_zhou 阅读(192) 评论(0) 推荐(0) 编辑
摘要: Q:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations inCwhere the candidate numbers sums toT.Each number inCmay only be usedoncein the combination.Note:All numbers (including target) will be positive integers.Elements in a combination (a1,a2, � ,ak) mus 阅读全文
posted @ 2013-08-10 13:35 summer_zhou 阅读(146) 评论(0) 推荐(0) 编辑
摘要: Q: 有重复元素的全排列问题。DFS,注意筛选条件。 vector > permuteUnique(vector &num) { // Start typing your C/C++ solution below // DO NOT write int main() function vector path; vector > result; vector bvisited(num.size(),false); sort(num.begin(),num.end()); /... 阅读全文
posted @ 2013-08-09 15:30 summer_zhou 阅读(167) 评论(0) 推荐(0) 编辑
上一页 1 ··· 10 11 12 13 14 15 16 17 18 ··· 20 下一页