2014年3月21日
摘要: 2014-03-21 22:23题目:假设你一开始有一个空数组,你在读入一些整数并将其插入到数组中,保证插入之后数组一直按升序排列。在读入的过程中,你还可以进行一种操作:查询某个值val是否存在于数组中,并给出这个元素在数组中的位置(如果有多个的重复元素话,给出最小的下标)。解法:书上的原题不是这么描述的,但我觉得用这种插入排序的说法更好理解。虽然说是数组,但实际上既可以用真的数组来模拟这一过程,也可以用一棵二叉搜索树来做。我的实现是用二叉搜索树,每个节点里除了记录元素的值之外,还记录它的左子树有多少个点。这样在树里面也能进行对数级的查找。其实,用数组直接模拟的话,代码应该更好写的。代码: 1 阅读全文
posted @ 2014-03-21 22:38 zhuli19901106 阅读(254) 评论(0) 推荐(0)
摘要: 2014-03-21 22:05题目:给你N个盒子堆成一座塔,要求下面盒子的长和宽都要严格大于上面的。问最多能堆多少个盒子?解法1:O(n^2)的动态规划解决。其实是最长递增子序列问题,所以也可以用O(n * log(n))的优化算法。代码:// 11.7 n boxes are to stack up to a tower. Every box must be strictly smaller in width and height than the one right below it.// How many boxes at most can you stack up?#include 阅读全文
posted @ 2014-03-21 22:18 zhuli19901106 阅读(227) 评论(0) 推荐(0)
摘要: 2014-03-21 21:50题目:给定一个MxN的二位数组,如果每一行每一列都是升序排列(不代表全展开成一个一维数组仍是升序排列的)。请设计一个算法在其中查找元素。解法:对于这么一个数组,有两点是确定的:1. 左上最小,右下最大;2. 左边不大于右边,上边不大于下边。根据这么个思路,你可以从左下或者右上开始查找,应该向左走向右走,还是向上走向下走,你懂的。用这种方法,可以在线性的时间内找出一个元素。代码: 1 // 11.6 Given an MxN matrix, each row and each column is sorted in ascending order. 2 // Fo 阅读全文
posted @ 2014-03-21 21:58 zhuli19901106 阅读(225) 评论(0) 推荐(0)
摘要: 2014-03-21 21:37题目:给定一个字符串数组,但是其中夹杂了很多空串“”,不如{“Hello”, “”, “World”, “”, “”, “”, “Zoo”, “”}请设计一个算法在其中查找字符串。解法:要么一次性将其中夹杂的空串去掉,要么在二分查找的过程中逐个跳过空串。反正整体思路仍是二分。代码: 1 // 11.5 Given an array of strings interspersed with empty string ""s. Find out if a target string exists in the string. 2 #include 阅读全文
posted @ 2014-03-21 21:45 zhuli19901106 阅读(194) 评论(0) 推荐(0)
摘要: 2014-03-21 21:28题目:给定一个20GB大小的文本文件,每一行都是一个字符串。请设计方法将这个文件里的字符串排序。解法:请看下面的注释。代码: 1 // 11.4 Given a file of 20GB containing strings, one word each line. How would you sort them all? 2 // Answer: 3 // 1. Split them into 200M pieces. 4 // 2. For each pieces, use comparison sort or hashing to sort i... 阅读全文
posted @ 2014-03-21 21:37 zhuli19901106 阅读(174) 评论(0) 推荐(0)
摘要: 2014-03-21 20:55题目:给定一个旋转过的升序排序好的数组,不知道旋转了几位。找出其中是否存在某一个值。解法1:如果数组的元素都不重复,那么我的解法是先找出旋转的偏移量,然后进行带偏移量的二分搜索。两个过程都是对数级的。代码: 1 // 11.3 Given a sorted array rotated by a few positions, find out if a value exists in the array. 2 // Suppose all elements in the array are unique. 3 #include 4 #include 5 #in.. 阅读全文
posted @ 2014-03-21 21:26 zhuli19901106 阅读(235) 评论(0) 推荐(0)
摘要: 2014-03-21 20:49题目:设计一种排序算法,使得anagram排在一起。解法:自定义一个comparator,使用额外的空间来统计字母个数,然后比较字母个数。代码: 1 // 11.2 Sort an array of strings such that anagrams stay next to each other. 2 #include 3 #include 4 #include 5 #include 6 using namespace std; 7 8 string ta, tb; 9 int counting[256];10 11 void countingSo... 阅读全文
posted @ 2014-03-21 20:55 zhuli19901106 阅读(183) 评论(0) 推荐(0)
摘要: 2014-03-21 20:35题目:给定已升序排列的数组A和数组B,如果A有足够的额外空间容纳A和B,请讲B数组合入到A中。解法:由后往前进行归并。代码: 1 // 11.1 Given two sorted array A and B, suppose A is large enough to hold them both. Merge B into A. 2 #include 3 #include 4 using namespace std; 5 6 void mergeBIntoA(int a[], int b[], int na, int nb) 7 { 8 if (n... 阅读全文
posted @ 2014-03-21 20:48 zhuli19901106 阅读(286) 评论(0) 推荐(0)
摘要: 2014-03-21 20:20题目:给定一个只包含‘0’、‘1’、‘|’、‘&’、‘^’的布尔表达式,和一个期望的结果(0或者1)。如果允许你用自由地给这个表达式加括号来控制运算的顺序,问问有多少种加括号的方法来达到期望的结果值。解法:DFS暴力解决,至于优化方法,应该是可以进行一部分剪枝的,但我... 阅读全文
posted @ 2014-03-21 20:32 zhuli19901106 阅读(270) 评论(0) 推荐(0)