2013年9月29日

Best Time to Buy and Sell Stock

摘要: 一次遍历,计算每支股票减掉前面的最小值时的值,然后更新可以获得的最大收益,复杂度为O(n)。注意边界条件的判断。 int maxProfit(vector &prices) { // Start typing your C/C++ solution below // DO NOT write int main() function if(prices.size() == 0 || prices.size() == 1) return 0; int max = INT_MIN, min = prices[0]; ... 阅读全文

posted @ 2013-09-29 09:23 waruzhi 阅读(133) 评论(0) 推荐(0) 编辑

Remove Nth Node From End of List

摘要: 居然一次AC了,虽然是道极水的题。考虑好一些特殊条件,比如n小于等于0, root等于NULL即可。 ListNode *removeNthFromEnd(ListNode *head, int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n next; } if(n > len) return head; if(n == len) r... 阅读全文

posted @ 2013-09-29 09:12 waruzhi 阅读(147) 评论(0) 推荐(0) 编辑

2013年9月28日

Unique Paths

摘要: 最开始使用带记录的递归,结果还是超时,然后改成动规打表以后就通过了。使用三层循环,最外层为长或宽的最大值,复杂度为O(n^3)。 int record[101][101]; void initialize(){ int i, j, k; for(i = 1; i < 101; i++){ record[1][i] = 1; record[i][1] = 1; } for(i = 2; i < 101; i++){ for(j = 2; j <= i; j++){... 阅读全文

posted @ 2013-09-28 20:16 waruzhi 阅读(130) 评论(0) 推荐(0) 编辑

Swap Nodes in Pairs

摘要: 考察指针的使用,但是写的过程中遇到一些问题,主要是写之前没想清需要用到哪些额外的指针。最后解决方案里有4个指针,result记录结果,tmp1和tmp2分别记录前后两个要交换的指针,next记录下面一组指针的第一个指针。解决办法比较笨,应该有简洁的方法,第二遍刷的时候细究吧。 ListNode *swapPairs(ListNode *head) { // Start typing your C/C++ solution below // DO NOT write int main() function if(head == NULL || he... 阅读全文

posted @ 2013-09-28 19:41 waruzhi 阅读(183) 评论(0) 推荐(0) 编辑

Convert Sorted Array to Binary Search Tree

摘要: 使用分治的方法,递归地将数组转成二叉树。这道题提交的时候出现了很多错误,主要问题出在vector、iterator的使用,以及结构体指针的初始化上。代码如下: TreeNode *sortedArrayToBST(vector &num) { // Start typing your C/C++ solution below // DO NOT write int main() function int len = num.size(); if(len == 0) return NULL; Tre... 阅读全文

posted @ 2013-09-28 16:41 waruzhi 阅读(113) 评论(0) 推荐(0) 编辑

Populating Next Right Pointers in Each Node

摘要: 因为是完全二叉树,所以根节点的左节点的next是根节点的右节点,右节点的next是根节点的左节点。递归实现。 1 void connect(TreeLinkNode *root) { 2 // Start typing your C/C++ solution below 3 // DO NOT write int main() function 4 if(root != NULL){ 5 if(root->left) 6 root->left->next = root->right;... 阅读全文

posted @ 2013-09-28 09:53 waruzhi 阅读(160) 评论(0) 推荐(0) 编辑

2013年9月23日

Maximum Subarray

摘要: 经典的求最大连续子数组和,以下有两种方法。方法一:动规,复杂度O(n)一遍扫描,R[i]表示以第i个元素结尾的最大连续子数组和,那么R[i+1] = max(A[i+1], R[i]+A[i+1])。代码如下: int maxSubArray(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function int i; int max = A[0]; int tmp = A[0]; ... 阅读全文

posted @ 2013-09-23 16:58 waruzhi 阅读(190) 评论(0) 推荐(1) 编辑

2013年9月22日

Climbing Stairs

摘要: 最后一步只可能是1或2,所以每一种方案n都是方案n-1和方案n-2的和,其实是斐波那契数列。 int climbStairs(int n) { int result[1000]; // Start typing your C/C++ solution below // DO NOT write int main() function result[0] = 1; result[1] = 1; result[2] = 2; if(result[n] != 0) retur... 阅读全文

posted @ 2013-09-22 21:20 waruzhi 阅读(154) 评论(0) 推荐(1) 编辑

Unique Binary Search Trees

摘要: 使用带标记的DP int result[1000]; int numTrees(int n) { result[0] = 1; result[1] = 1; result[2] = 2; // Start typing your C/C++ solution below // DO NOT write int main() function if(result[n] != 0) return result[n]; int num = 0, t; ... 阅读全文

posted @ 2013-09-22 21:11 waruzhi 阅读(129) 评论(0) 推荐(1) 编辑

Remove Duplicates from Sorted Array

摘要: int removeDuplicates(int A[], int n) { // Start typing your C/C++ solution below // DO NOT write int main() function if(n == 0) return n; int i; int result = 0; for(i = 1; i < n; i++){ if(A[i] != A[result]){ A[result+... 阅读全文

posted @ 2013-09-22 20:58 waruzhi 阅读(135) 评论(0) 推荐(1) 编辑

导航