摘要:
http://www.itint5.com/oj/#25这题在leetcode上是用中序遍历来做的,但是这里由于有相等的情况,即左子树小于等于根,这样中序遍历无法完全判定。可以用递归来做,用递归给出每个子树的上下界。#include bool isBSTRecur(TreeNode *root, int leftVal, int rightVal) { if (root == NULL) return true; return (root->val val > leftVal) && isBSTRecur(root->left, leftVal, root-& 阅读全文
posted @ 2014-01-20 23:22
阿牧遥
阅读(411)
评论(0)
推荐(0)
摘要:
http://www.itint5.com/oj/#23这里就是26进制的转换,但是要注意没有0,A就是1,Z就是26。所以要想象成从0开始,才能用原来的方法计算。//将十进制数转换为excel数string decToExcel(int decNum) { string ans; while (decNum != 0) { char c = (char) ((decNum - 1) % 26 + 'A'); decNum = (decNum - 1) / 26; ans += c; } int len = ans.length... 阅读全文
posted @ 2014-01-20 22:50
阿牧遥
阅读(190)
评论(0)
推荐(0)
摘要:
http://www.itint5.com/oj/#22这题一开始直接用暴力的DFS来做,果然到25的规模就挂了.vector visited(50, false);vector > vec_row(50);vector > vec_col(50);bool findPath(vector &x, vector &y, int idx, int depth, int direction) { if (depth == x.size()) return true; visited[idx] = true; if (direction == 0) { int row.. 阅读全文
posted @ 2014-01-20 22:24
阿牧遥
阅读(460)
评论(0)
推荐(0)
摘要:
http://www.itint5.com/oj/#18这一题,首先如果直接去算的话,很容易就超出int或者long的表示范围了。那么要利用%的性质,(num * 10 + 1) % a = 10 * (num % a) + 1 % a。除了a为1的情况,都是10 * (num % a) + 1。然后计算的时候,先去掉是2和5的倍数的情况。也可以直接做,如果余数出现过,就不用继续了。int findMinAllOne(int a) { if (a % 2 == 0 || a % 5 == 0) return -1; if (a == 1) return 1; int num ... 阅读全文
posted @ 2014-01-20 20:33
阿牧遥
阅读(218)
评论(0)
推荐(0)
摘要:
http://www.itint5.com/oj/#15用hash来做,目前为止做到最好也是case16超时(20w的规模),即使分桶也超时。注意计算hashcode时,'a'要算成1,否则如果'a'为0,那么"aa"和"a"是一样的。下面是超时的代码:#define BUCKET 65535#define ulong long longvector > uset(BUCKET);vector pow26(11);ulong hashcode(char *str, int n) { ulong code = 0; f 阅读全文
posted @ 2014-01-20 18:35
阿牧遥
阅读(298)
评论(0)
推荐(0)
摘要:
http://www.itint5.com/oj/#12首先由跳马问题一,就是普通的日字型跳法,那么在无限棋盘上,任何点都是可达的。证法是先推出可以由(0,0)到(0,1),那么由对称型等可知任何点都可以到了。加强版是可以跳到(p,q),当然对称的也可以跳到(q,p)。那么接下来是数学推导:http://www.itint5.com/discuss/16/%E8%B7%B3%E9%A9%AC%E9%97%AE%E9%A2%98%E5%8A%A0%E5%BC%BA%E7%89%881. 计算dx=x-x2,dy=y-y2。2. 求出p,q的最大公约数g,如果dx或者dy不能被g整除,那么很显然无 阅读全文
posted @ 2014-01-20 15:15
阿牧遥
阅读(535)
评论(0)
推荐(0)
摘要:
http://www.itint5.com/oj/#42基本上就是word ladder。直接来BFS,记录前驱。vector transform(set &dict, string from, string to) { vector ans; if (from.length() != to.length()) { return ans; } queue que; map prev_map; que.push(from); prev_map[from] = ""; while (!que.empty()) { stri... 阅读全文
posted @ 2014-01-20 00:01
阿牧遥
阅读(193)
评论(0)
推荐(0)

浙公网安备 33010602011771号