摘要:
2014-03-20 03:08题目:给定一个集合,返回其幂集。解法:DFS。代码: 1 // 9.4 Return all subsets of a set 2 #include 3 #include 4 using namespace std; 5 6 void getSubsets(const vector &v, int idx, vector &buffer, vector > &res) 7 { 8 if (idx == (int)v.size()) { 9 res.push_back(buffer);10 } else {11 ... 阅读全文
摘要:
2014-03-20 03:01题目:给定一个已按升序排序的数组,找出是否有A[i] = i的情况出现。解法1:如果元素不重复,是可以严格二分查找的。代码: 1 // 9.3 Given a unique sorted array, find a position where A[i] = i, if one exists. 2 #include 3 #include 4 using namespace std; 5 6 int main() 7 { 8 vector v; 9 int n;10 int i;11 int ll, rr, mm;12 ... 阅读全文
摘要:
2014-03-20 02:55题目:从(0, 0)走到(x, y),其中x、y都是非负整数。每次只能向x或y轴的正方向走一格,那么总共有多少种走法。如果有些地方被障碍挡住不能走呢?解法1:如果没有障碍的话,组合数学,排列组合公式C(x + y, x)。代码: 1 // 9.2 How many ways are there to go from (0, 0) to (x, y), if you only go left or up, and one unit at a time? 2 #include 3 using namespace std; 4 5 int combination(.. 阅读全文
摘要:
2014-03-20 02:55题目:小朋友跳台阶,每次跳1层或2层,那么跳N层总共有多少种跳法。解法:斐波那契数列。代码: 1 // 9.1 A child can run up the stair with n staircases. Every time he can hop up by 1, 2 or 3 steps. How many possible way to do this are there? 2 #include 3 #include 4 using namespace std; 5 6 int main() 7 { 8 int n; 9 int i... 阅读全文
摘要:
2014-03-20 02:29题目:将质因数只有3, 5, 7的正整数从小到大排列,找出其中第K个。解法:用三个iterator指向3, 5, 7,每次将对应位置的数分别乘以3, 5, 7,取三者中最小的数作为生成的下一个结果。可以一次性生成整个序列,因为这个序列一般不会很长,增长率是指数级的。代码: 1 // 7.7 Find the kth number that has no prime factors other than 3, 5 or 7. 2 #include 3 #include 4 #include 5 using namespace std; 6 7 int ma... 阅读全文
摘要:
2014-03-20 02:24题目:给定二位平面上一堆点,找到一条直线,使其穿过的点数量最多。解法:我的解法只能适用整点,对于实数坐标就得换效率更低的办法了。请参见LeetCode -Max Points on a Line-zhuli19901106- 博客园。代码: 1 // 7.6 Find the line that crosses the most points. 2 #include 3 #include 4 using namespace std; 5 6 struct Point { 7 int x; 8 int y; 9 Po... 阅读全文
摘要:
2014-03-20 02:20题目:给定二维平面上两个正方形,用一条直线将俩方块划分成面积相等的两部分。解法:穿过对称中心的线会将面积等分,所以连接两个中心即可。如果两个中心恰好重合,那么任意穿过这个点的直线都满足条件。代码:1 // 7.5 Given two squares on two-dimensional plane, draw a line to cut them in two parts with equal area.2 // Answer:3 // Apparently the line should go through the center of the two ... 阅读全文
摘要:
2014-03-20 02:16题目:只用加法和赋值,实现减法、乘法、除法。解法:我只实现了整数范围内的。减法就是加上相反数。乘法就是连着加上很多个。除法就是减到不能减为止,数数总共减了多少个。代码: 1 // 7.4 Implement the -*/ function with only the + operator. 2 // You cannot use bit operation, although you might want it for efficiency. 3 #include 4 using namespace std; 5 6 int negate(in... 阅读全文
摘要:
2014-03-20 02:05题目:给定笛卡尔二维平面上两条直线,判断它们是否相交。解法:相交、重合、平行。代码: 1 // 7.3 Given two lines on the Cartesian, determine if they would intersect. 2 #include 3 using namespace std; 4 5 int main() 6 { 7 // a * x + b * y + c = 0; 8 // d * x + e * y + f = 0; 9 int a, b, c, d, e, f;10 bool suc;1... 阅读全文
摘要:
2014-03-20 01:59题目:有n只蚂蚁在正n边形的n个顶点,同时以同速率开始沿着边走。每只蚂蚁走的方向是随机的,那么这些蚂蚁至少有两只发生碰撞的概率是多少。解法:只有所有蚂蚁都往一个方向走才不会碰撞,所以不碰的概率就是2/2^n,碰的概率就用1减去喽。代码:1 // 7.2 n ants are standing on the vertices of an n-edged equilateral polygon, they start walking at the same time, same speed.2 // Every ant chooses randomly a dire 阅读全文