摘要: Q:Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note: extra space: O(K)A: 开辟k+1大的空间用于存储结果。做K+1次迭代。第i次迭代,将result[i]置成1,同时对于j:[1,i-1],result[j] = result[j] + old result[j-1]; vector<int> getRow(int rowIndex) { // Start typing your C/C++ sol 阅读全文
posted @ 2013-06-13 21:57 summer_zhou 阅读(142) 评论(0) 推荐(0)
摘要: Q: 杨辉三角A:1. 左右两端的数都为12. 每个数字等于上一行的左右两个数字之和。3. 每行数字左右对称,由1开始逐渐变大。4. 第n行的数字有n项。 vector<vector<int> > generate(int numRows) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<vector<int> > result; if(numRows<=0) return ... 阅读全文
posted @ 2013-06-13 20:16 summer_zhou 阅读(136) 评论(0) 推荐(0)
摘要: Q:Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.A:Findiandjthat maximizes Aj- Ai, wherei<j. int maxPro 阅读全文
posted @ 2013-06-13 19:47 summer_zhou 阅读(113) 评论(0) 推荐(0)
摘要: Q: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.找到连续的子数组,使得和最大。A: 典型的动态规划。f(i):以i结尾的子数组最大和. 定义成1~i之间最大和,是不对的,因为题目要求连续的最大子数组。f(i) = f(i 阅读全文
posted @ 2013-06-12 15:34 summer_zhou 阅读(123) 评论(0) 推荐(0)
摘要: Q : You are climbing a stair case. It takesnsteps 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?A: 动态规划的题。记f(n)是爬n steps stair的不同方式。爬到n层,那么有两种选择:1. 先爬n-1层,再往上1step2. 先爬n-2层,再往上2 steps。那么f(n) = f(n-2) + f(n-1) (Fibonacci数) 注意不要用递归, 阅读全文
posted @ 2013-06-11 15:52 summer_zhou 阅读(149) 评论(0) 推荐(0)
摘要: Q:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.A: Two pointers问题。i从头往后走,j从后往前走,直到i>=j. 这里注意如果遇到非文字数字,需要跳过。 1 bool isPalindr 阅读全文
posted @ 2013-06-06 21:12 summer_zhou 阅读(111) 评论(0) 推荐(0)
摘要: Q:Given a number represented as an array of digits, plus one to the number.A: 很简单。将carry初始化置成1即可。注意vector insert的用法 vector<int> plusOne(vector<int> &digits) { // Start typing your C/C++ solution below // DO NOT write int main() function vector<int> sum; int carry = 1; ... 阅读全文
posted @ 2013-06-06 15:25 summer_zhou 阅读(157) 评论(0) 推荐(0)
摘要: Q:Givenn, how many structurally uniqueBST's(binary search trees) that store values 1...n?For example,Givenn= 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ / / / \ \ 3 2 1 1 3 2 / / \ \ 2 1... 阅读全文
posted @ 2013-06-06 14:52 summer_zhou 阅读(120) 评论(0) 推荐(0)
摘要: Q:Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.给定一个递增排序好的链表,将其转换成一个平衡的二叉查找树。A: 这道题与“Convert Sorted Array to BST” 不同。数组随即访问的效率是O(1),所以可以快速的找到中间元素,而链表随即访问的效率为O(n),因此不同用之前的方法。Top-down的方法不能用了,改用:bottom-up的方式建立BST。随着list的遍历,创建树的结点,避免了链表的随机访问 阅读全文
posted @ 2013-06-04 15:45 summer_zhou 阅读(125) 评论(0) 推荐(0)
摘要: Q: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 ofeverynode never differ by more than 1.判断一个二叉树是否高度平衡:对任意结点,其左右子树的高度差<=1.A: 递归的对每个结点做判断是否平衡。子树的height<0,表示该子树不平衡。 bool is 阅读全文
posted @ 2013-06-04 14:44 summer_zhou 阅读(128) 评论(0) 推荐(0)