摘要: Path Sum IIGiven a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ / \ 7 2 5 1return[ ... 阅读全文
posted @ 2014-03-21 20:00 flowerkzj 阅读(122) 评论(0) 推荐(0) 编辑
摘要: Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree andsum = 22, 5 / \ 4 8 / / \ 11 13 4 ... 阅读全文
posted @ 2014-03-21 19:51 flowerkzj 阅读(110) 评论(0) 推荐(0) 编辑
摘要: Single NumberGiven an array of integers, every element appearstwiceexcept for one. Find that single one.一个数组中只有一个整数是只出现了一次,其余均为两次,找出那个数。直接用异或即可,因为如果两个数相同的话异或结果为0. 1 class Solution { 2 public: 3 int singleNumber(int A[], int n) { 4 int ret = A[0]; 5 for (int i = 1; i < n; ++i) { 6... 阅读全文
posted @ 2014-03-21 19:42 flowerkzj 阅读(97) 评论(0) 推荐(0) 编辑
摘要: Add Two NumbersYou are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.Input:(2 -> 4 -> 3) + (5 -> 6 -> 4)Output:7 -> 0 -> 8大非负整数相 阅读全文
posted @ 2014-03-21 19:38 flowerkzj 阅读(136) 评论(0) 推荐(0) 编辑
摘要: Search Insert PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 → 1[1,3,5,6], 7 → 4[1,3,5,6], 0 阅读全文
posted @ 2014-03-21 19:27 flowerkzj 阅读(130) 评论(0) 推荐(0) 编辑
摘要: Rotate ListGiven a list, rotate the list to the right bykplaces, wherekis non-negative.For example:Given1->2->3->4->5->NULLandk=2,return4->5->1->2->3->NULL.就是给一个循环列表,有个头的链接,求转动k个位置后链接头的变化这里的话其实是要找出倒数第k个数的位置,然后在这个位置截开,后面链表接上前面链表的头。找出倒数第k个数首先思路用的是快慢指针。但是有一个需要考虑的就是k可以很大,因为 阅读全文
posted @ 2014-03-21 19:23 flowerkzj 阅读(194) 评论(0) 推荐(0) 编辑
摘要: Reverse Words in a StringGiven an input string, reverse the string word by word.For example,Given s = "the sky is blue",return "blue is sky the".单词反转,思路就是单词反转后再整句反转,只是加了一些要求,首尾的空格不能要,中间出现连续的空格都变成一个。 1 class Solution { 2 public: 3 void reverseAll(string &s, int i, int j) { 4 w 阅读全文
posted @ 2014-03-21 19:03 flowerkzj 阅读(151) 评论(0) 推荐(0) 编辑