赞
踩
专业面试笔试题1:计算二叉树的深度
- public int computeTreeDepth(BinaryTree binaryTree){
- if(binaryTree == null){
- return 0;
- }
- if(binaryTree.lChild == null && binaryTree.rChild == null){
- return 1;
- }
- int lDepth = computeTreeDepth(binaryTree.lChild) + 1;
- int rDepth = computeTreeDepth(binaryTree.rChild) + 1;
- return Math.max(lDepth, rDepth);
- }
专业面试笔试题2:反转数字,输入一个32位整形数字a,输出反转后的数字,例如:123,输出321;-2324,输出-4232;1003400,输出43001;如果输入的数超过32位,则输出0;
- #include <iostream>
- #include <math.h>
- using namespace std;
- int main() {
- int a,b=0,c[10],weiShu;
- cin >> a;
-
- if(a<-2147483647 || a>2147483647){
- b=1;
- cout<<b<<endl;
- return 0;
- }
- //把各个位的数字取出来
- for(int i=0; i<10; i++){
- c[i] = a%10;
- a = a/pow(10,1);
- }
- //计算这个数字共几位
- for(int j=9; j>=0; j--){
- if(c[j] != 0){
- weiShu = j;
- break;
- }
- }
- //反转
- for(int z=weiShu; z>=0; z--){
- b = b+c[z]*pow(10,weiShu-z);
- }
- cout<<b<<endl;
- return 0;
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。