上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 17 下一页

2013年11月8日

Convert Sorted List to Binary Search Tree

摘要: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.思路:递归代码: 1 TreeNode *sortedListToBST(ListNode *head, int len){ 2 if(len == 0){ 3 return NULL; 4 } 5 TreeNode *result = new TreeNode(0); 6 if(le... 阅读全文

posted @ 2013-11-08 19:35 waruzhi 阅读(144) 评论(0) 推荐(0) 编辑

Longest Consecutive Sequence

摘要: Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given[100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is[1, 2, 3, 4]. Return its length:4.Your algorithm should run in O(n) complexity.思路:因为用排序的话就需要nlgn,而题目要求是O(n),所以考虑到用哈希。先 阅读全文

posted @ 2013-11-08 19:15 waruzhi 阅读(167) 评论(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?思路:用一个栈来保存节点。因为需要记录是否访问了左右子节点,所以用一个pair的第一位来记录当前的访问状态。代码: 1 vector postorderTraversa... 阅读全文

posted @ 2013-11-08 18:40 waruzhi 阅读(182) 评论(0) 推荐(0) 编辑

2013年11月7日

Triangle

摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [6,5,7], [4,1,8,3]]The minimum path sum from top to bottom is11(i.e.,2+3+5+1= 11).Note:Bonus point if you are a... 阅读全文

posted @ 2013-11-07 22:05 waruzhi 阅读(152) 评论(0) 推荐(0) 编辑

4Sum

摘要: Given an arraySofnintegers, are there elementsa,b,c, anddinSsuch thata+b+c+d= target? Find all unique quadruplets in the array which gives the sum of target.Note:Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie,a≤b≤c≤d)The solution set must not contain duplicate quadruplets. . 阅读全文

posted @ 2013-11-07 21:45 waruzhi 阅读(165) 评论(0) 推荐(0) 编辑

3Sum Closest

摘要: Given an arraySofnintegers, find three integers inSsuch that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution. For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to ... 阅读全文

posted @ 2013-11-07 21:16 waruzhi 阅读(190) 评论(0) 推荐(0) 编辑

3Sum

摘要: Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in non-descending order. (ie,a≤b≤c)The solution set must not contain duplicate triplets. For example, given array S... 阅读全文

posted @ 2013-11-07 21:05 waruzhi 阅读(164) 评论(0) 推荐(0) 编辑

Two Sum

摘要: Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are 阅读全文

posted @ 2013-11-07 20:23 waruzhi 阅读(152) 评论(0) 推荐(0) 编辑

2013年11月6日

Combinations

摘要: Given two integersnandk, return all possible combinations ofknumbers out of 1 ...n.For example,Ifn= 4 andk= 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]思路:DFS另外,用next permutation里提到的方法也可以。代码: 1 void search(int start, int n, int k, vector > &result, vector &tmp){ 2 ... 阅读全文

posted @ 2013-11-06 21:16 waruzhi 阅读(192) 评论(0) 推荐(0) 编辑

Populating Next Right Pointers in Each Node II

摘要: Follow up for problem "Populating Next Right Pointers in Each Node".What if the given tree could be any binary tree? Would your previous solution still work?Note:You may only use constant extra space.For example,Given the following binary tree, 1 / \ 2 3 / \ \ 4 5 ... 阅读全文

posted @ 2013-11-06 20:44 waruzhi 阅读(138) 评论(0) 推荐(0) 编辑

上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 17 下一页

导航