摘要: 题目:Givennpairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, givenn= 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()()"思路: 可以用递归一步步构造字符串。当左括号出现次数<n时,就可以放置新的左括号。当右括号出现次数小于左括 阅读全文
posted @ 2013-05-07 09:47 tanghulu321 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 题目:Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6思路:此题为前序遍历。 可以用迭代也可以用stack。... 阅读全文
posted @ 2013-05-07 02:17 tanghulu321 阅读(174) 评论(0) 推荐(0) 编辑
摘要: 题目:Given an unsorted integer array, find the first missing positive integer.For example,Given[1,2,0]return3,and[3,4,-1,1]return2.Your algorithm should run inO(n) time and uses constant space.思路:主要的思想就是把对应的数放到对应的索引上,例如1放到A[1]上,这样只需要O(n)的遍历就能完成,然后用一个O(n)的遍历找第一个没有放到索引上的数返回。代码: 1 class Solution { 2 pub. 阅读全文
posted @ 2013-05-07 01:09 tanghulu321 阅读(132) 评论(0) 推荐(0) 编辑
摘要: 题目:Determine if a Sudoku is valid, according to:Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character'.'.思路: 做三个判断: 行, 列, block代码: 1 class Solution { 2 public: 3 bool isValidSudoku(vector<vector<char> > &board) 阅读全文
posted @ 2013-05-07 00:24 tanghulu321 阅读(123) 评论(0) 推荐(0) 编辑
摘要: 题目:Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character'.'.You may assume that there will be only one unique solution.思路: 回溯法,当有可行解时返回代码: 1 class Solution { 2 bool isValid(vector<vector<char> >& board, int x, int y) 3 阅读全文
posted @ 2013-05-06 23:34 tanghulu321 阅读(124) 评论(0) 推荐(0) 编辑
摘要: 题目:Write an efficient algorithm that searches for a value in anmxnmatrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each row is greater than the last integer of the previous row.For example,Consider the following matrix:[ [1, ... 阅读全文
posted @ 2013-05-03 04:51 tanghulu321 阅读(104) 评论(0) 推荐(0) 编辑
摘要: 题目:Follow up for "Search in Rotated Sorted Array":What ifduplicatesare allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the array.思路:此题比较复杂,有三种情况需要判断:1. 左区间是正常的。 2. 右区间正常。 3. 左边和中间相等, 则说明左边到中间都是相同数字,此时需要判断时候和右边相等,如果不相等则搜索右 阅读全文
posted @ 2013-05-03 03:10 tanghulu321 阅读(134) 评论(0) 推荐(1) 编辑
摘要: 题目:Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e.,0 1 2 4 5 6 7might become4 5 6 7 0 1 2).You are given a target value to search. If found in the array return its index, otherwise return -1.You may assume no duplicate exists in the array.思路: 不同于普通的B search, 此题需要判断左右哪 阅读全文
posted @ 2013-05-02 12:12 tanghulu321 阅读(106) 评论(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.思路:可以用recursive的方法,每次返回左右两个树的最小高度的最小值加1. 但是如果这个树不是完整二叉树,则需要在没有左树或者没有右树的情况下让没有的那一叉返回正无穷而不是0. 所以需要判断当root == NULL的时候,这个root是不是到最下面一层了。代码:clas 阅读全文
posted @ 2013-05-02 01:19 tanghulu321 阅读(93) 评论(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.思路1:We can solve this easily using recursion. Why? Because each of the leftChild and rightChild of a node is a sub-tree itself. We first co 阅读全文
posted @ 2013-04-30 11:59 tanghulu321 阅读(109) 评论(0) 推荐(0) 编辑