摘要: Given a binary tree, return the preorder traversal 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?思考:不可以递归,那么用栈存储结点。先用递归法做,算是加深记忆。需要注意ret需要清空,不然会记录上一次测试用例,所以将先序遍历单独写出来。class Solutio... 阅读全文
posted @ 2013-11-14 22:04 七年之后 阅读(147) 评论(0) 推荐(0) 编辑
摘要: 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(Li... 阅读全文
posted @ 2013-11-14 17:17 七年之后 阅读(267) 评论(0) 推荐(0) 编辑
摘要: Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.思考:复习KMP算法。class Solution {public: char *strStr(char *haystack, char *needle) { // IMPORTANT: Please reset any member data you declared, as // the same Solut... 阅读全文
posted @ 2013-11-14 13:41 七年之后 阅读(171) 评论(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 mat... 阅读全文
posted @ 2013-11-13 11:11 七年之后 阅读(160) 评论(0) 推荐(0) 编辑
摘要: Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for... 阅读全文
posted @ 2013-11-13 10:21 七年之后 阅读(165) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, swap every two adjacent nodes and return its head.For example, Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.思考:考虑几种情况:空链表,单节 阅读全文
posted @ 2013-11-12 21:08 七年之后 阅读(155) 评论(0) 推荐(0) 编辑
摘要: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.思考:合并双链表的基础上增加循环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *... 阅读全文
posted @ 2013-11-12 12:47 七年之后 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((... 阅读全文
posted @ 2013-11-11 17:59 七年之后 阅读(192) 评论(0) 推荐(0) 编辑
摘要: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]&qu 阅读全文
posted @ 2013-11-11 11:13 七年之后 阅读(167) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.Note: Given n will always be valid. Try to do this in one p 阅读全文
posted @ 2013-11-10 20:00 七年之后 阅读(144) 评论(0) 推荐(0) 编辑