随笔分类 -  LeetCode

摘要: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,则返回高度,否则返回-1。最后root结点若返回-1则false,否则true。/** * Definit 阅读全文
posted @ 2013-11-10 21:59 xchangcheng 阅读(198) 评论(0) 推荐(0)
摘要:GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]解题思路:顺序加入每一个vector就好,其中注意元素的个数与求和关系。class Solution {public: vector > generate(int numRows) { // IMPORTANT: Please reset any member data you... 阅读全文
posted @ 2013-11-10 20:42 xchangcheng 阅读(1867) 评论(0) 推荐(0)
摘要: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.解题思路:这个问题其实最长连续子序列和的变种。对于此题,设dp[i]为第i天卖出的最大收益,则dp[i] = max( 阅读全文
posted @ 2013-11-10 20:29 xchangcheng 阅读(157) 评论(0) 推荐(0)
摘要:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.解题思路:递归做。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */class Solution ... 阅读全文
posted @ 2013-11-10 20:13 xchangcheng 阅读(152) 评论(0) 推荐(0)
摘要:iven a binary tree, return theinordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,3,2].Note:Recursive solution is trivial, could you do it iteratively?解题思路:结点的访问顺序是,左子树 -> 自己 -> 右子树。对于一个结点,它可以输出的条件是,其子树已经加入list,同时左子树已经访问完成。这里的实现和后序遍历不同,后序遍历只需... 阅读全文
posted @ 2013-11-10 19:52 xchangcheng 阅读(167) 评论(0) 推荐(0)
摘要:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Note:Bonus points if you could solve it both recursively and iterati... 阅读全文
posted @ 2013-11-10 15:16 xchangcheng 阅读(203) 评论(0) 推荐(0)
摘要: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?解题思路:简单的递归,ways[n] = ways[n - 1] + ways[n - 2];class Solution {public: int climbStairs(int n) { // IMPORTANT: Please reset any m... 阅读全文
posted @ 2013-11-10 14:58 xchangcheng 阅读(147) 评论(0) 推荐(0)
摘要:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class So... 阅读全文
posted @ 2013-11-10 14:51 xchangcheng 阅读(167) 评论(0) 推荐(0)
摘要:Given two sorted integer arrays A and B, merge B into A as one sorted array.Note:You may assume that A has enough space to hold additional elements from B. The number of elements initialized in A and B aremandnrespectively.解题思路:额外开了空间,不开也可以,但是会比较麻烦。class Solution {public: void merge(int A[], int ... 阅读全文
posted @ 2013-11-10 14:36 xchangcheng 阅读(157) 评论(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, try 阅读全文
posted @ 2013-11-10 12:53 xchangcheng 阅读(212) 评论(0) 推荐(0)
摘要:Given a sorted array, remove the duplicates in place such that each element appear onlyonceand return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.For example,Given input array A =[1,1,2],Your function should return length =2, and A is 阅读全文
posted @ 2013-11-10 11:38 xchangcheng 阅读(133) 评论(0) 推荐(0)
摘要:Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.解题思路:从前往后扫一遍,遇到elem,试图从后往前找一个元素与之交换,如果找不到可以交换的元素,则停止。注意elem如果没有出现,则应该返回n。class Solution {public: int remo 阅读全文
posted @ 2013-11-10 11:21 xchangcheng 阅读(205) 评论(0) 推荐(0)
摘要:Given a binary tree, return thepostordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[3,2,1].Note:Recursive solution is trivial, could you do it iteratively?解题思路:相比于前序遍历,后续遍历思维上难度要大些,前序遍历是通过一个stack,首先压入父亲结点,然后弹出父亲结点,并输出它的value,之后压人其右儿子,左儿子即可。然而后... 阅读全文
posted @ 2013-11-10 10:22 xchangcheng 阅读(3328) 评论(0) 推荐(0)
摘要:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 → 0解题思路:顺序查找非常简单,这里尝试 阅读全文
posted @ 2013-11-09 22:35 xchangcheng 阅读(160) 评论(0) 推荐(0)
摘要:Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].Note:Recursive solution is trivial, could you do it iteratively?解题思路:用迭代的方法实现二叉树的遍历,需要借助一些数据结构,比如list,stack等。首先在stack中push入当前的root,由于是前序遍历,故root的value是先于... 阅读全文
posted @ 2013-11-09 22:10 xchangcheng 阅读(1300) 评论(0) 推荐(0)
摘要:Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set toNULL.Initially, all next pointers are set toNULL.N... 阅读全文
posted @ 2013-11-09 19:36 xchangcheng 阅读(148) 评论(0) 推荐(0)
摘要:Given a sorted linked list, delete all duplicates such that each element appear onlyonce.For example,Given1->1->2, return1->2.Given1->1->2->3->3, return1->2->3.顺序扫描一遍即可,注意释放被删除的结点的空间。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; 阅读全文
posted @ 2013-11-09 13:19 xchangcheng 阅读(163) 评论(0) 推荐(0)
摘要: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-11-09 12:11 xchangcheng 阅读(474) 评论(0) 推荐(0)
摘要:Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at 阅读全文
posted @ 2013-11-09 10:23 xchangcheng 阅读(152) 评论(0) 推荐(0)
摘要:Reverse digits of an integer.Example1:x = 123, return 321Example2:x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integer's last digit is 0, what should the output be? ie, cas 阅读全文
posted @ 2013-11-09 10:01 xchangcheng 阅读(393) 评论(0) 推荐(0)