摘要:
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
阅读(271)
评论(0)
推荐(0)


浙公网安备 33010602011771号