上一页 1 ··· 21 22 23 24 25 26 27 28 29 ··· 51 下一页
2014年3月21日
摘要: 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)
2014年3月20日
摘要: 2014-03-20 04:15题目:你有n个盒子,用这n个盒子堆成一个塔,要求下面的盒子必须在长宽高上都严格大于上面的。如果你不能旋转盒子变换长宽高,这座塔最高能堆多高?解法:首先将n个盒子按照长宽高顺序排好序,然后动态规划,我写了个O(n^2)时间复杂度的代码。代码: 1 // 9.10 A stack of n boxes is form a tower. where every stack must be strictly larger than the one right above it. 2 // The boxes cannot be rotated. 3 #include . 阅读全文
posted @ 2014-03-20 04:16 zhuli19901106 阅读(326) 评论(0) 推荐(0)
摘要: 2014-03-20 04:08题目:八皇后问题。解法:DFS解决。代码: 1 // 9.9 Eight-Queen Problem, need I say more? 2 #include 3 #include 4 using namespace std; 5 6 class Solutio... 阅读全文
posted @ 2014-03-20 04:08 zhuli19901106 阅读(192) 评论(0) 推荐(0)
摘要: 2014-03-20 04:04题目:给你不限量的1分钱、5分钱、10分钱、25分钱硬币,凑成n分钱总共有多少种方法?解法:理论上来说应该是有排列组合的公式解的,但推导起来太麻烦而且换个数据就又得重推了,所以我还是用动态规划解决。代码: 1 // 9.8 Given unlimited quarters(25 cents), dimes(10 cents), nickels(5 cents) and pennies(1 cent), how many ways are there to represent n cents. 2 #include 3 #include 4 using nam.. 阅读全文
posted @ 2014-03-20 04:07 zhuli19901106 阅读(321) 评论(0) 推荐(0)
摘要: 2014-03-20 03:35题目:实现画图的Flood Fill操作。解法:DFS和BFS皆可,但BFS使用的队列在时间复杂度上常数项比较大,速度略慢,所以我选了DFS。当然,如果图很大的话DFS是会导致call stack溢出的,那就摊上事儿了。代码: 1 // 9.7 Implement a flood fill painter that changes a certain area to a certain color. You are given one point as the seed. 2 #include 3 #include 4 using namespace std.. 阅读全文
posted @ 2014-03-20 03:40 zhuli19901106 阅读(310) 评论(0) 推荐(0)
摘要: 2014-03-20 03:27题目:输出所有由N对括号组成的合法的括号序列。比如n=2,“()()”、“(())”等等。解法:动态规划配合DFS,应该也叫记忆化搜索吧。一个整数N总可以拆成若干个正整数的和,执行搜索的时候也是按照这个规则,将N序列拆成多个子序列进行搜索,同时将中间的搜索结果记录下来,以便下次再搜到的时候直接调用,省掉重复计算的开销。代码: 1 // 9.6 Print all valid parentheses sequences of n ()s. 2 #include 3 #include 4 #include 5 using namespace std; 6 7... 阅读全文
posted @ 2014-03-20 03:34 zhuli19901106 阅读(253) 评论(0) 推荐(0)
摘要: 2014-03-20 03:23题目:给定一个字符串,输出其全排列。解法:可以调用STL提供的next_permutation(),也可以自己写一个。对于这种看起来简单的题目,应该在能优化的地方,尽量想办法优化。在面试里如果大家都会做的题,你就得做的很好才能拉开差距,否则就等着thank you了。代码: 1 // 9.5 Print all permutations of a string. 2 #include 3 #include 4 #include 5 using namespace std; 6 7 void countingSort(char s[], int n) 8 ... 阅读全文
posted @ 2014-03-20 03:26 zhuli19901106 阅读(215) 评论(0) 推荐(0)
上一页 1 ··· 21 22 23 24 25 26 27 28 29 ··· 51 下一页