上一页 1 2 3 4 5 6 7 8 ··· 11 下一页

2014年3月26日

Reorder List

摘要: Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given{1,2,3,4}, reorder it to{1,4,2,3}.本题 O(n)空间时间做完,但实际有O(1)的做法。那个办法是把后半部分翻转,然后merge一下就可以了。/** * Definition for singly-linked list. * struct 阅读全文

posted @ 2014-03-26 16:16 pengyu2003 阅读(106) 评论(0) 推荐(0) 编辑

Binary Tree Preorder Traversal

摘要: Given a binary tree, return thepreordertraversal of its nodes' values.For example:Given binary tree{1,#,2,3}, 1 \ 2 / 3return[1,2,3].此题之前和文清讨论过,一次过了。但是总体难度上,比后续迭代简单不少。/** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * ... 阅读全文

posted @ 2014-03-26 14:51 pengyu2003 阅读(87) 评论(0) 推荐(0) 编辑

Binary Tree Postorder Traversal

摘要: 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?递归版本就不写了,那个没难度。迭代的方法,利用一个栈。左子树一律进栈。当没有左子树的情况出现时,再考虑又子树。由于访问过的点可能也存在左子树和右子树,所以用一个last标记刚刚... 阅读全文

posted @ 2014-03-26 11:23 pengyu2003 阅读(107) 评论(0) 推荐(0) 编辑

2014年3月25日

Insertion Sort List

摘要: Sort a linked list using insertion sort.指针的初始值设置有问题,梁旭两天童颜的错误了。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *insertionSortList(ListNode *head) { if(hea... 阅读全文

posted @ 2014-03-25 16:37 pengyu2003 阅读(90) 评论(0) 推荐(0) 编辑

2014年3月24日

Sort List

摘要: Sort a linked list inO(nlogn) time using constant space complexity.逻辑正确,尾指针没有置空,调了好久……/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *sortList(ListNode *head) {... 阅读全文

posted @ 2014-03-24 22:04 pengyu2003 阅读(135) 评论(0) 推荐(0) 编辑

Evaluate Reverse Polish Notation

摘要: Evaluate the value of an arithmetic expression inReverse Polish Notation.Valid operators are+,-,*,/. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13&qu 阅读全文

posted @ 2014-03-24 16:33 pengyu2003 阅读(102) 评论(0) 推荐(0) 编辑

Reverse Words in a String

摘要: Given an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".click to show clarification.Clarification:What constitutes a word?A sequence of non-space characters constitutes a word.Could the input string contain leading o 阅读全文

posted @ 2014-03-24 16:14 pengyu2003 阅读(131) 评论(0) 推荐(0) 编辑

Sqrt(x)

摘要: Implementint sqrt(int x).Compute and return the square root ofx.此题看着简单,实则坑爹啊!!!!!首先,关于平方,int是不够用的。其次,二分法肯定好用,但是,最终的结果怎么取?原则上取小于等于的最近整数。但是mid的计算可能会更改这个数,所以需要调整一下。class Solution {public: int sqrt(int x) { long long left = 0; long long right = x; long long mid = left + (right - lef... 阅读全文

posted @ 2014-03-24 15:27 pengyu2003 阅读(126) 评论(0) 推荐(0) 编辑

Permutations II

摘要: Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2]have the following unique permutations:[1,1,2],[1,2,1], and[2,1,1].class Solution {public: vector > re; vector > permuteUnique(vector &num) { map m; int size = ... 阅读全文

posted @ 2014-03-24 14:00 pengyu2003 阅读(104) 评论(0) 推荐(0) 编辑

2014年3月23日

Merge Intervals

摘要: Given a collection of intervals, merge all overlapping intervals.For example,Given[1,3],[2,6],[8,10],[15,18],return[1,6],[8,10],[15,18].排序/** * Definition for an interval. * struct Interval { * int start; * int end; * Interval() : start(0), end(0) {} * Interval(int s, int e) : start(... 阅读全文

posted @ 2014-03-23 16:17 pengyu2003 阅读(141) 评论(0) 推荐(0) 编辑

上一页 1 2 3 4 5 6 7 8 ··· 11 下一页

导航