随笔- 509  文章- 0  评论- 151  阅读- 22万 
01 2014 档案
剑指Offer - 九度1348 - 数组中的逆序对
摘要:剑指Offer - 九度1348 - 数组中的逆序对2014-01-30 23:19题目描述:在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对。输入一个数组,求出这个数组中的逆序对的总数。输入:每个测试案例包括两行:第一行包含一个整数n,表示数组中的元素个数。其中1 右半段的某个a[y]的话,那么a[x]、a[x+ 1]、...、a[j]必然都大于a[y]。 按上面那种算法,一次就多了j-x+1个逆序数。这么一来,就不用一个一个地算了。要是真一个一个地算逆序数,时间复杂度必然是O(n^2)了,因为逆序数本身就是O(n^2)数量级的。 最后,别忘了用64位整数来.. 阅读全文
posted @ 2014-01-30 23:24 zhuli19901106 阅读(443) 评论(0) 推荐(0) 编辑
LeetCode - Sort List
摘要:Sort List2014.1.14 23:17Sort a linked list inO(nlogn) time using constant space complexity.Solution: The first thing I thought of about this problem is merge sort. It's weakness in space complexity is perfectly avoided with linked list, as you won't need another O(n) space when merging two s 阅读全文
posted @ 2014-01-14 23:19 zhuli19901106 阅读(178) 评论(0) 推荐(0) 编辑
LeetCode - Insertion Sort List
摘要:Insertion Sort List2014.1.14 21:58Sort a linked list using insertion sort.Solution: Everyone must've have learned about insertion sort on some data structure textbooks, like "Data Structures & Algorithm Analysis". They usually present you with source code for insertion sort on arra 阅读全文
posted @ 2014-01-14 22:13 zhuli19901106 阅读(138) 评论(0) 推荐(0) 编辑
LeetCode - Binary Tree Postorder Traversal
摘要:Binary Tree Postorder Traversal2014.1.14 21:17Given 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?Solution1: Recursive solution is trivial,... 阅读全文
posted @ 2014-01-14 21:38 zhuli19901106 阅读(289) 评论(0) 推荐(0) 编辑
LeetCode - Binary Tree Preorder Traversal
摘要:Binary Tree Preorder Traversal2014.1.14 02:36Given a binary tree, return thepreordertraversal 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?Solution1: The recursive version of preorde... 阅读全文
posted @ 2014-01-14 02:38 zhuli19901106 阅读(196) 评论(0) 推荐(0) 编辑
LeetCode - Reorder List
摘要:Reorder List2014.1.13 22:07Given 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}.Solution1: My solution for this problem is in three steps: 1. cut the list i 阅读全文
posted @ 2014-01-13 22:17 zhuli19901106 阅读(209) 评论(0) 推荐(0) 编辑
LeetCode - Linked List Cycle II
摘要:Linked List Cycle II2014.1.13 21:43Given 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?Solution: This problem doesn't only ask you to check if there is a loop in the list, but also to find out where yo 阅读全文
posted @ 2014-01-13 21:45 zhuli19901106 阅读(148) 评论(0) 推荐(0) 编辑
LeetCode - Linked List Cycle
摘要:Linked List Cycle2014.1.13 21:24Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?Solution: The problem indicated that you shouldn't use extra space. And you might be wondering why you should use extra space if you could just use two poin 阅读全文
posted @ 2014-01-13 21:42 zhuli19901106 阅读(258) 评论(0) 推荐(0) 编辑
LeetCode - Copy List with Random Pointer
摘要:Copy List with Random Pointer2014.1.13 20:45A 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.Solution: First of all, the problem description indicated that you have to return a deep copy. 阅读全文
posted @ 2014-01-13 20:49 zhuli19901106 阅读(258) 评论(0) 推荐(0) 编辑
LeetCode - Single Number II
摘要:Single Number II2014.1.13 20:25Given 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?Solution: Still, exclusive OR is a wonderful operator. This.. 阅读全文
posted @ 2014-01-13 20:44 zhuli19901106 阅读(198) 评论(0) 推荐(0) 编辑
LeetCode - Single Number
摘要:Single Number2014.1.13 20:17Given an array of integers, every element appearstwiceexcept for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?Solution: Exclusive OR is a wonderful operator. Time complexity is O.. 阅读全文
posted @ 2014-01-13 20:19 zhuli19901106 阅读(115) 评论(0) 推荐(0) 编辑
LeetCode - Sum Root to Leaf Numbers
摘要:Sum Root to Leaf Numbers2014.1.1 19:55Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number.An example is the root-to-leaf path1->2->3which represents the number123.Find the total sum of all root-to-leaf numbers.For example, 1 / \ 2 3The root-to-l.. 阅读全文
posted @ 2014-01-13 20:02 zhuli19901106 阅读(149) 评论(0) 推荐(0) 编辑
LeetCode - Longest Consecutive Sequence
摘要:Longest Consecutive Sequence2014.1.13 19:00Given 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) complex 阅读全文
posted @ 2014-01-13 19:19 zhuli19901106 阅读(221) 评论(0) 推荐(0) 编辑
LeetCode - Valid Palindrome
摘要:Valid Palindrome2014.1.13 18:48Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome.Note:Have you consider that the string might be em 阅读全文
posted @ 2014-01-13 18:58 zhuli19901106 阅读(154) 评论(0) 推荐(0) 编辑
LeetCode - Best Time to Buy and Sell Stock III
摘要:Best Time to Buy and Sell Stock III2014.1.13 01:43Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may not engage in multiple transactions at the same time (ie, you mus 阅读全文
posted @ 2014-01-13 01:46 zhuli19901106 阅读(256) 评论(0) 推荐(0) 编辑
LeetCode - Best Time to Buy and Sell Stock II
摘要:Best Time to Buy and Sell Stock II2014.1.10 22:39Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However 阅读全文
posted @ 2014-01-10 22:50 zhuli19901106 阅读(188) 评论(0) 推荐(0) 编辑
LeetCode - Best Time to Buy and Sell Stock
摘要:Best Time to Buy and Sell Stock2014.23.56Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.Solution: To maxi. 阅读全文
posted @ 2014-01-09 23:59 zhuli19901106 阅读(153) 评论(0) 推荐(0) 编辑
LeetCode - Triangle
摘要:Triangle2014.1.9 22:56Given 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:Bo... 阅读全文
posted @ 2014-01-09 23:18 zhuli19901106 阅读(207) 评论(0) 推荐(0) 编辑
LeetCode - Pascal's Triangle II
摘要:Pascal's Triangle II2014.1.8 22:58Given an indexk, return thekthrow of the Pascal's triangle.For example, givenk= 3,Return[1,3,3,1].Note:Could you optimize your algorithm to use onlyO(k) extra space?Solution1: Pascal's Triangle is a typical O(n^2) dynamic programming problem. Here is the 阅读全文
posted @ 2014-01-08 23:01 zhuli19901106 阅读(223) 评论(0) 推荐(0) 编辑
LeetCode - Pascal's Triangle
摘要:Pascal's Triangle2014.1.8 22:44GivennumRows, generate the firstnumRowsof Pascal's triangle.For example, givennumRows= 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]Solution: Pascal's Triangle is a typical O(n^2) dynamic programming problem. Here is the recurrence relation: 1. f.. 阅读全文
posted @ 2014-01-08 22:49 zhuli19901106 阅读(157) 评论(0) 推荐(0) 编辑
LeetCode - Populating Next Right Pointers in Each Node II
摘要:Populating Next Right Pointers in Each Node II2014.1.8 21:00Follow 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 followin 阅读全文
posted @ 2014-01-08 21:57 zhuli19901106 阅读(179) 评论(0) 推荐(0) 编辑
LeetCode - Populating Next Right Pointers in Each Node
摘要:Populating Next Right Pointers in Each Node2014.1.8 05:59Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be... 阅读全文
posted @ 2014-01-08 06:43 zhuli19901106 阅读(255) 评论(0) 推荐(0) 编辑
LeetCode - Path Sum II
摘要:Path Sum II2014.1.8 05:38Given 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 ... 阅读全文
posted @ 2014-01-08 05:51 zhuli19901106 阅读(184) 评论(0) 推荐(0) 编辑
LeetCode - Path Sum
摘要:Path Sum2014.1.8 04:59Given 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... 阅读全文
posted @ 2014-01-08 05:00 zhuli19901106 阅读(138) 评论(0) 推荐(0) 编辑
LeetCode - Minimum Depth of Binary Tree
摘要:Minimum Depth of Binary Tree2014/1/8 04:29Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.Solution: The height of a tree is defined as the longest path from the root node down to the farthe. 阅读全文
posted @ 2014-01-08 04:33 zhuli19901106 阅读(160) 评论(0) 推荐(0) 编辑
LeetCode - Balanced Binary Tree
摘要:Balanced Binary Tree2014.1.8 04:09Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1.Solution: Just do as the problem says, calculate the h. 阅读全文
posted @ 2014-01-08 04:15 zhuli19901106 阅读(160) 评论(0) 推荐(0) 编辑
LeetCode - Convert Sorted Array to Binary Search Tree
摘要:Convert Sorted Array to Binary Search Tree2014.1.8 02:11Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Solution: A height balanced binary tree is a binary tree, whose heights of left and right subtrees varie by at most 1. Thus constructing such a tr. 阅读全文
posted @ 2014-01-08 02:39 zhuli19901106 阅读(162) 评论(0) 推荐(0) 编辑
LeetCode - Binary Tree Level Order Traversal II
摘要:Binary Tree Level Order Traversal II2014.1.8 01:45Given a binary tree, return thebottom-up level ordertraversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree{3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its bottom-up leve... 阅读全文
posted @ 2014-01-08 01:54 zhuli19901106 阅读(143) 评论(0) 推荐(0) 编辑
LeetCode - Construct Binary Tree from Inorder and Postorder Traversal
摘要:Construct Binary Tree from Inorder and Postorder Traversal2014.1.8 01:16Given inorder and postorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Solution: inorder traversal = left, root, right; postordertraversal = left, right, root; ... 阅读全文
posted @ 2014-01-08 01:41 zhuli19901106 阅读(136) 评论(0) 推荐(0) 编辑
LeetCode - Construct Binary Tree from Preorder and Inorder Traversal
摘要:Construct Binary Tree from Preorder and Inorder Traversal2014.1.8 00:59Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Solution: preorder traversal = root, left, right; inorder traversal = left, root, right; Th... 阅读全文
posted @ 2014-01-08 01:16 zhuli19901106 阅读(196) 评论(0) 推荐(0) 编辑
LeetCode - Maximum Depth of Binary Tree
摘要:Maximum Depth of Binary Tree2014.1.7 23:26Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Solution: depth(tree) = max(depth(left subtree), depth(right subtree)) + 1; depth(nullptr) = 0; T... 阅读全文
posted @ 2014-01-08 00:05 zhuli19901106 阅读(128) 评论(0) 推荐(0) 编辑
LeetCode - Binary Tree Zigzag Level Order Traversal
摘要:Binary Tree Zigzag Level Order Traversal2014.1.1 02:13Given a binary tree, return thezigzag level ordertraversal 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 ... 阅读全文
posted @ 2014-01-01 02:28 zhuli19901106 阅读(189) 评论(0) 推荐(0) 编辑
LeetCode - Binary Tree Level Order Traversal
摘要:Binary Tree Level Order Traversal2014.1.1 01:53Given a binary tree, return thelevel ordertraversal 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], [1... 阅读全文
posted @ 2014-01-01 02:12 zhuli19901106 阅读(212) 评论(0) 推荐(0) 编辑
LeetCode - Symmetric Tree
摘要:Symmetric Tree2014.1.1 01:12Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the following is not: 1 / \ 2 2 \ \ 3 3Solution: Well..., I guess it's the new... 阅读全文
posted @ 2014-01-01 01:20 zhuli19901106 阅读(202) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示