摘要: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between).For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its zigzag level order traversal as:[ [... 阅读全文
posted @ 2013-11-18 14:42 七年之后 阅读(143) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its level order traversal as:[ [3], [9,20], [15,7]]confused what "{1,#,2,3}" means? > rea. 阅读全文
posted @ 2013-11-18 13:59 七年之后 阅读(184) 评论(0) 推荐(0) 编辑
摘要: There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least one candy.Children with a higher rating get more candies than their neighbors.What is the minimum candies you 阅读全文
posted @ 2013-11-17 14:18 七年之后 阅读(203) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appears three times except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?思考:参考这里。class Solution {public: int singleNumber(int A[], int n) { // IMPORTANT... 阅读全文
posted @ 2013-11-17 12:31 七年之后 阅读(138) 评论(0) 推荐(0) 编辑
摘要: Given an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?思考:位运算。class Solution {public: int singleNumber(int A[], int n) { // IMPORTANT: Pleas... 阅读全文
posted @ 2013-11-17 11:39 七年之后 阅读(166) 评论(0) 推荐(0) 编辑
摘要: A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.思考:先进行单链表复制,在定义random指针,每个结点的random指针都要遍历一次链表,时间复杂度为O(n2)。/** * Definition for singly-linked list with a random pointer. * struct RandomLi 阅读全文
posted @ 2013-11-17 03:14 七年之后 阅读(233) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up: Can you solve it without using extra space?思考:第一步:设环长为len,快慢指针q、p相遇时,q比p多走了k*len。 第二部:p先走k*len步,p,q一起走每次一步,相遇时p比q多走了一个环距离,此时q就是环开始结点。通过分析AC的感觉真好!class Solution {public: ListNode *detectCycle(... 阅读全文
posted @ 2013-11-16 12:07 七年之后 阅读(271) 评论(0) 推荐(0) 编辑
摘要: Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space?思考:快慢指针,快指针一次走两步,慢指针一次一步。若快指针跟慢指针指向同一个结点,则有环。若快指针到达链表末尾即指向NULL,说明没有环。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : ... 阅读全文
posted @ 2013-11-16 11:20 七年之后 阅读(208) 评论(0) 推荐(0) 编辑
摘要: Given a singly linked list L: 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(n2)方法这里就不贴了。因为每次要插入的结点都是尾结点,从头结点开始寻找时间都花费在查询上。题意说只可以改变.next,这算是一个提示吧。我们可以翻转待插入链表结点 阅读全文
posted @ 2013-11-16 10:49 七年之后 阅读(293) 评论(0) 推荐(0) 编辑
摘要: Given a binary tree, return the postorder traversal 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?思考:原思路是为每个节点增加一个标记。这个思路先序遍历不要标记,因为当前访问结点直接出栈,不用看是否访问过。不过此思路redefinition of 'struct... 阅读全文
posted @ 2013-11-15 12:27 七年之后 阅读(256) 评论(0) 推荐(0) 编辑