上一页 1 ··· 34 35 36 37 38 39 40 41 42 ··· 51 下一页
2014年1月30日
摘要: 剑指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 阅读(451) 评论(0) 推荐(0)
2014年1月14日
摘要: 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 阅读(184) 评论(0) 推荐(0)
摘要: 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 阅读(148) 评论(0) 推荐(0)
摘要: 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 阅读(294) 评论(0) 推荐(0)
摘要: 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 阅读(205) 评论(0) 推荐(0)
2014年1月13日
摘要: 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 阅读(214) 评论(0) 推荐(0)
摘要: 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 阅读(161) 评论(0) 推荐(0)
摘要: 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 阅读(265) 评论(0) 推荐(0)
摘要: 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 阅读(264) 评论(0) 推荐(0)
摘要: 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 阅读(201) 评论(0) 推荐(0)
上一页 1 ··· 34 35 36 37 38 39 40 41 42 ··· 51 下一页