2014年3月26日

Candy

摘要: There areNchildren 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 mu 阅读全文

posted @ 2014-03-26 22:53 pengyu2003 阅读(114) 评论(0) 推荐(0)

Single Number

摘要: Given an array of integers, every element appearstwiceexcept for one. Find that single one.和上一题比这题就是渣。class Solution {public: int singleNumber(int A[], int n) { if(A == NULL)return 0; int re = 0; for(int i = 0 ; i < n ;i++)re ^= A[i]; return re; }}; 阅读全文

posted @ 2014-03-26 21:50 pengyu2003 阅读(96) 评论(0) 推荐(0)

Single Number II

摘要: Given an array of integers, every element appearsthreetimes except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?本题最好理解版本有的数字出现3次,个别出现1次,因此每出现3次就可以归零了。我们用三个量分别记录出现1次、2次、3次(其实就是0次)的位,分别为x1,x2,x3.某一位出现1次,可能之 阅读全文

posted @ 2014-03-26 21:45 pengyu2003 阅读(185) 评论(0) 推荐(0)

Copy List with Random Pointer

摘要: 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.这题貌似见过,大概是编程之美之类吧。复习一下深拷贝和浅拷贝的区别:深拷贝保存了指针指向的内容,二浅拷贝应对指针指示给指针赋值。所以对于有指针的地方,浅拷贝非常不安全。这段代码用了2个栈,感觉很不效率,空间O(n)。有个空间O(1)的。/** * Definition for 阅读全文

posted @ 2014-03-26 19:57 pengyu2003 阅读(122) 评论(0) 推荐(0)

Word Break

摘要: Given a stringsand a dictionary of wordsdict, determine ifscan be segmented into a space-separated sequence of one or more dictionary words.For example, givens="leetcode",dict=["leet", "code"].Return true because"leetcode"can be segmented as"leet code&quo 阅读全文

posted @ 2014-03-26 18:50 pengyu2003 阅读(132) 评论(0) 推荐(0)

Linked List Cycle

摘要: Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?更简单的环题/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: bool has... 阅读全文

posted @ 2014-03-26 16:35 pengyu2003 阅读(122) 评论(0) 推荐(0)

Linked List Cycle II

摘要: Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull.Follow up:Can you solve it without using extra space?老题,没难度。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * ... 阅读全文

posted @ 2014-03-26 16:30 pengyu2003 阅读(97) 评论(0) 推荐(0)

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 阅读(114) 评论(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 阅读(94) 评论(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 阅读(113) 评论(0) 推荐(0)

导航