上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 31 下一页
摘要: 思路:1. 堆栈模拟总结:1. 使用堆栈的话, 后续遍历和前序遍历并不是对称的, 和递归函数的写法大不相同代码:自己最初写的代码, 遍历一遍后, 树已经被破坏掉了class Solution {public: vector postorderTraversal(TreeNode *root) { vector res; if(root == NULL) return res; stack stc; stc.push(root); while(!stc.empty()) { TreeNode* node = stc.top(); if(node->left... 阅读全文
posted @ 2013-12-05 22:18 SangS 阅读(511) 评论(0) 推荐(0) 编辑
摘要: 思路:1. 不让用递归, 改用堆栈2. 堆栈也没必要显示给出, 模拟也是可以的代码:class Solution {public: vector preorderTraversal(TreeNode *root) { vector res; if(root == NULL) return res; stack stc; stc.push(root); while(!stc.empty()) { TreeNode* node = stc.top(); stc.pop(); if(node != NULL) res.push_back(node->val); ... 阅读全文
posted @ 2013-12-05 21:38 SangS 阅读(161) 评论(0) 推荐(0) 编辑
摘要: 思路:1. 空间复杂度为 o(n) 解法. 创建两个链表, 分别记录大于 x 和小于 x 的节点, 最后合并2. o(1) 的空间复杂度解法. 四个指针, 分别指向小于 x 部分链表的头, 尾, 指向大于 x 部分链表的头, 尾总结:1. 使用 dummyNode 减少判断代码:class Solution {public: ListNode *partition(ListNode *head, int x) { ListNode *first, *second, *third, *fourth; if(head == NULL) return head; List... 阅读全文
posted @ 2013-12-05 17:20 SangS 阅读(2520) 评论(0) 推荐(0) 编辑
摘要: 中序遍历代码:#include #include using namespace std; struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution {public: vector record; void inorderTravel(TreeNode *root) { if(root->left) inorderTravel(root->left); r... 阅读全文
posted @ 2013-12-05 16:45 SangS 阅读(162) 评论(0) 推荐(0) 编辑
摘要: 设置一个游标, 记录最后一个不重复的元素代码:#include using namespace std;class Solution {public: int removeDuplicates(int A[], int n) { if(n==0) return 0; int cursor = 0; for(int i = 1; i using namespace std;class Solution {public: int removeElement(int A[], int n, int elem) {... 阅读全文
posted @ 2013-12-05 16:08 SangS 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 仅允许使用常数时间, 交换 k-group 的节点思路:1. 完全逆转一个链表: 每遍历到一个节点, 就将该节点放在链表首位2. 在(1) 的基础上添加大小为 j 的窗口总结:1. 看来上一道题目思路也不是最优的代码: updateclass Solution {public: ListNode *reverseKGroup(ListNode *head, int k) { ListNode *newHead = new ListNode(0); newHead->next = head; int cnt = 0; ListNode... 阅读全文
posted @ 2013-12-05 16:00 SangS 阅读(1878) 评论(0) 推荐(0) 编辑
摘要: 对每一个树节点, 判断其左右孩子的高度差, 一旦有一个不满足条件, 则返回 false代码:class Solution {public: bool ans; int getHeight(TreeNode *root) { if(!ans) return 0; if(root == NULL) return 0; int lt = 0, rt = 0; if(root->left&&ans) { lt = 1+getHeight(root->left); } if(root->right&&ans) rt = 1 + getHeight(roo 阅读全文
posted @ 2013-12-05 15:04 SangS 阅读(440) 评论(0) 推荐(0) 编辑
摘要: 思路:1. 对于每一个节点, 返回其所在子树所能提供的最大值, 且该最大值必须是单支的, WA 过2.max(0, max(lf, rt))+root->val, 可以仅返回根节点, WA 过3. 需要维护一个全局最优解 ans, WA 过代码:class Solution {public: int ans; int solve_dp(TreeNode *root) { if(root == NULL) return 0; int sum = root->val; int lf = 0, rt = 0; if(root->left) lf = solve_dp(root-&g 阅读全文
posted @ 2013-12-05 12:05 SangS 阅读(494) 评论(0) 推荐(0) 编辑
摘要: DescriptionPeople in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not 阅读全文
posted @ 2013-12-04 21:07 SangS 阅读(2460) 评论(0) 推荐(1) 编辑
摘要: DescriptionOn January 1st 2002, The Netherlands, and several other European countries abandoned their national currency in favour of the Euro. This changed the ease of paying, and not just internationally.A student buying a 68 guilder book before January 1st could pay for the book with one 50 guilde 阅读全文
posted @ 2013-12-04 10:53 SangS 阅读(611) 评论(0) 推荐(0) 编辑
上一页 1 ··· 20 21 22 23 24 25 26 27 28 ··· 31 下一页