摘要: 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 of every node never differ by more than 1.class Solution {public: int sub(TreeNode* root){ if(root==NULL)return 0; ... 阅读全文
posted @ 2013-10-04 17:09 懒猫欣 阅读(127) 评论(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 \ 6click to show hints.Hints:If ... 阅读全文
posted @ 2013-10-04 16:36 懒猫欣 阅读(146) 评论(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. A sudoku puzzle......and its solution numbers marked in red.简单的回溯搜索即可class Solution {public: void solveSudoku(vector 阅读全文
posted @ 2013-10-04 16:16 懒猫欣 阅读(238) 评论(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 '.'. A partially filled sudoku which is valid.class Solution {public: bool isValidSudoku(vector > &board) { // Note: The 阅读全文
posted @ 2013-10-04 14:28 懒猫欣 阅读(155) 评论(0) 推荐(0)
摘要: You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?class Solution {public: int climbStairs(int n) { // Note: The Solution object is instantiated only once and is reused by each ... 阅读全文
posted @ 2013-10-04 13:56 懒猫欣 阅读(116) 评论(0) 推荐(0)
摘要: Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]class Solution {public: void sub(vector >& ret,int n,int k){ if(n >copy=ret; for(int i=0;i > comb... 阅读全文
posted @ 2013-10-04 12:36 懒猫欣 阅读(156) 评论(0) 推荐(0)
摘要: Given n, generate all structurally unique BST's (binary search trees) that store values 1...n. For example,Given n = 3, your program should return all 5 unique BST's shown below. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / ... 阅读全文
posted @ 2013-10-04 12:16 懒猫欣 阅读(193) 评论(0) 推荐(0)
摘要: Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 ... 阅读全文
posted @ 2013-10-04 03:03 懒猫欣 阅读(165) 评论(0) 推荐(0)
摘要: Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:a) Insert a characterb) Delete a characterc) Replace a characterclass Solution {public: int minDistan... 阅读全文
posted @ 2013-10-04 02:05 懒猫欣 阅读(143) 评论(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.click to show more practice.More practice:If you have figured out the O(n) solution 阅读全文
posted @ 2013-10-04 00:39 懒猫欣 阅读(123) 评论(0) 推荐(0)
摘要: Given a string containing only digits, restore it by returning all possible valid IP address combinations. For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order does not matter)class Solution {public: void sub(string &s,string&ret,i 阅读全文
posted @ 2013-10-04 00:28 懒猫欣 阅读(185) 评论(0) 推荐(0)