摘要:        
2014-03-19 06:22题目:将整数A变成整数B,每次只能变一个二进制位,要变多少次呢。解法:异或,然后求‘1’的个数。代码: 1 // 5.5 Determine the number of bits required to convert integer A to B. 2 #include 3 using namespace std; 4 5 int numberOfOnes(unsigned int n) 6 { 7 int res = 0; 8 9 while (n != 0) {10 n = n & (n - 1);11 ...    阅读全文
        
            posted @ 2014-03-19 06:24
zhuli19901106
阅读(199)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 06:15题目:解释(n & (n - 1)) == 0是什么意思?解法:n&n-1是去掉最低位‘1’的方法。根据运算符优先级,貌似用不着加那个括号,但位运算的优先级总是个模棱两可的东西,所以一般还是要加上的。去掉一个‘1’就成了0,也就是说n是2的整次幂。代码: 1 // 5.4 Show what the code "n & (n - 1) == 0" means. 2 #include 3 using namespace std; 4 5 int main() 6 { 7 unsigned int n; 8 9 while    阅读全文
        
            posted @ 2014-03-19 06:22
zhuli19901106
阅读(179)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 05:57题目:给定一个整数N,求出比N大,而且二进制表示中和N有相同个数的‘1’的最小的数,比如3是‘11’,接下来的5是‘101’,再接下来的6是‘110’。解法:从低位往高位,首先跳过连续的‘0’,然后跳过连续的‘1’,并数数有多少个1。如果这时还没到最高位,那就从刚才跳过的‘1’中拿出1个放到这位上(当前位是‘0’),然后把剩下的‘1’填到最低的几位上去。中间填充‘0’。比如:‘100111000’的下一个是‘101000011’代码: 1 // 5.3 Find the next largest number that have same number of &    阅读全文
        
            posted @ 2014-03-19 06:15
zhuli19901106
阅读(204)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 05:47题目:给定一个double型浮点数,输出其二进制表示,如果不能在32个字符内完成输出,则输出“ERROR”。解法:如果你熟悉IEEE754标准,应该知道double和float型的二进制位里都是什么。double型最高位是符号位,随后11位是指数位,之后52位是尾数。你可以根据尾数和指数来判断要用多少二进制位才能精确表示这个浮点数。代码不怎么好写,这种题目应该也不常考吧。代码: 1 // 5.2 Given a double, print its binary representation if can be done in 32 characters. 2 #    阅读全文
        
            posted @ 2014-03-19 05:56
zhuli19901106
阅读(436)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 05:45题目:给定两个数M和N,将N按照二进制位,覆盖到M的特定段位中去。解法:位操作,请看代码。代码: 1 // 5.1 Insert one number into the certain bit segment of another number. 2 #include 3 using namespace std; 4 5 void printBinary(unsigned num) 6 { 7 unsigned bit = 1 >= 1;12 } while (bit);13 }14 15 unsigned insertBits(unsign...    阅读全文
        
            posted @ 2014-03-19 05:48
zhuli19901106
阅读(233)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 05:07题目:给定一棵二叉树T和一个值value,在T中找出所有加起来和等于value的路径。路径的起点和终点都可以是树的任意节点。解法:我偷了个懒,直接把这棵树看成一个无向图,用DFS来进行暴力搜索解决问题。因为没有什么数据顺序或是范围的限制,所以搜索剪枝好像也不太容易。代码: 1 // 4.9 Find all paths in a binary tree, the path doesn't have to start or end at the root or a leaf node. 2 #include 3 #include 4 #include ..    阅读全文
        
            posted @ 2014-03-19 05:15
zhuli19901106
阅读(284)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 05:04题目:给定两棵二叉树T1和T2,判断T2是否是T1的子树。子树的定义是,以T1的某个节点(可以是T1的根)作为根节点,得到的这棵树和T2一模一样。解法:首先可以根据节点个数省去一大部分不必要的搜索,然后再递归判断。代码还比较简单,请看下面。代码: 1 // 4.8 Check if a tree is a subtree of another. 2 #include 3 #include 4 using namespace std; 5 6 struct TreeNode { 7 int val; 8 TreeNode *l...    阅读全文
        
            posted @ 2014-03-19 05:08
zhuli19901106
阅读(326)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 04:48题目:最近公共父节点问题。解法1:Naive算法,先对其高度,然后一层一层往上直到找到结果。代码: 1 // 4.7 Least Common Ancestor 2 // This solution is Naive Algorithm, may timeout on very large and skewed trees. 3 #include 4 #include 5 using namespace std; 6 7 const int MAXN = 10005; 8 // tree[x][0]: parent of node x 9...    阅读全文
        
            posted @ 2014-03-19 05:02
zhuli19901106
阅读(394)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 04:16题目:找出一棵二叉搜索树中的中序遍历后继节点,每个节点都有指针指向其父节点。解法1:分两种情况:向下走时,先右后左;向上走时,先左后右。如果目标节点有右子树,就向右下走,否则往左上走。话说,如果没有父指针的话,还是一口气进行各中序遍历,求出所有结果比较有效率。代码: 1 // 4.6 Find the inorder successor of a node in the binary tree. 2 // online algorithm with parent pointer. 3 #include 4 #include 5 using name...    阅读全文
        
            posted @ 2014-03-19 04:32
zhuli19901106
阅读(285)
评论(0)
推荐(0)
        
            
        
        
摘要:        
2014-03-19 04:11题目:设计算法检查一棵二叉树是否为二叉搜索树。解法:既然是二叉搜索树,也就是说左子树所有节点都小于根,右子树所有节点都大于根。如果你真的全都检查的话,那就做了很多重复工作。只需要将左边最靠右,和右边最靠左的节点和根进行比较,然后依照这个规则递归求解即可。代码: 1 // 4.5 Check if a binary tree is binary search tree. 2 #include 3 using namespace std; 4 5 struct TreeNode { 6 int val; 7 TreeNode *left; 8 ...    阅读全文
        
            posted @ 2014-03-19 04:15
zhuli19901106
阅读(264)
评论(0)
推荐(0)
        
    
                    
                

浙公网安备 33010602011771号