Java
JavaDoc
javadoc命令是用来生成自己API文档的
参数信息:
@author 作者名
@version 版本号
@since 指明需要最早使用的jdk版本
@param 参数名
@return 返回值情况
@throws 异常抛出情况
/*** @author XXX* @version 1.0* @since 1.8*/public class Doc {String name;/*** @author XXX* @param name* @return* @throws Exception*/public String test(String name) throws Exception{return name;}
}
scanner
import java.util.Scanner;public class Demo01 {public static void main(String[] args) {// 创建一个扫描器对象,用于接受键盘数据Scanner scanner = new Scanner(System.in);System.out.println("使用next方式接收:");// 判断用户有没有输入字符串if (scanner.hasNext()){// 使用next方式接收String str = scanner.next(); // 程序会等待用户输入完毕System.out.println("输出的内容:"+str);}// 凡是IO流的类如果不关闭会一直占用资源,要养成用完就关的习惯scanner.close();}
}
import java.util.Scanner;public class Demo02 {public static void main(String[] args) {// 从键盘接收数据Scanner scanner = new Scanner(System.in);System.out.println("使用nextLine方式接收:");// 判断是否还有输入if (scanner.hasNextLine()){String str = scanner.nextLine();System.out.println("输出的内容为:"+str);}scanner.close();}
}
package com.xiang.scanner;import java.util.Scanner;public class Demo05 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);// 从键盘接收数据int i = 0;float f = 0.0f;System.out.println("请输入整数:");// 如果。。。那么。。。if (scanner.hasNextInt()){i = scanner.nextInt();System.out.println("整数数据:" + i);}else{System.out.println("输入的不是整数数据!");}System.out.println("请输入小数:");// 如果。。那么。。if (scanner.hasNextFloat()){f = scanner.nextFloat();System.out.println("小数数据:" + f);}else{System.out.println("输入的不是小数数据!");}scanner.close();}
}
九九乘法表
public class ForDemo04 {public static void main(String[] args) {// 打印九九乘法表for (int j = 1; j <= 9; j++) {for (int i = 1; i <= j; i++) {System.out.print(i+"*"+j+"="+(i*j)+"\t");}System.out.println();}}
}
每日一题
题目:
请设计一个算法,判断给定的二叉树是否为完全二叉树(Complete Binary Tree),并分析其时间和空间复杂度。
详细分析
1. 完全二叉树的定义
完全二叉树的定义是:
除了最后一层外,其他层的节点数必须达到最大值,且最后一层的节点必须全部集中在左侧。
示例:
复制
1 // 第1层(满)/ \2 3 // 第2层(满)/ \ /4 5 6 // 第3层(最后一层,节点靠左)
这棵树是完全二叉树。
非完全二叉树示例:
复制
1/ \2 3/ \ \4 5 7 // 最后一层的节点未靠左
2. 算法思路
核心思想:通过层次遍历(广度优先遍历),找到第一个不满足完全二叉树条件的节点。
关键点:
- 完全二叉树中,若某个节点存在右子节点,则必须存在左子节点。
- 在层次遍历中,若遇到某个节点只有右子节点(无左子节点),直接判定为非完全二叉树。
- 若某个节点缺少子节点(左或右),则后续所有节点必须为叶子节点。
具体步骤:
- 使用队列进行层次遍历。
- 设置一个标志位
mustBeLeaf
,初始为false
。 - 遍历每个节点时:
- 若
mustBeLeaf
为true
,但当前节点存在左或右子节点,则返回false
。 - 若当前节点左子节点为空但右子节点存在,返回
false
。 - 若当前节点左子节点存在但右子节点为空,将
mustBeLeaf
设为true
。
- 若
- 若遍历结束未发现异常,则返回
true
。
3. 代码实现(Python)
python
复制
class TreeNode:def __init__(self, val=0, left=None, right=None):self.val = valself.left = leftself.right = rightdef is_complete_tree(root):if not root:return Truequeue = [root]must_be_leaf = Falsewhile queue:node = queue.pop(0)if must_be_leaf:if node.left or node.right:return Falseelse:if node.left and node.right:queue.append(node.left)queue.append(node.right)elif node.left:queue.append(node.left)must_be_leaf = Trueelif node.right:return Falseelse:must_be_leaf = Truereturn True
4. 复杂度分析
- 时间复杂度:
O(n)
,所有节点遍历一次。 - 空间复杂度:
O(n)
,队列最多存储最后一层节点(最坏情况下为满二叉树,最后一层有n/2
个节点)。
5. 测试用例
-
完全二叉树:
复制
1/ \2 3/ \ / 4 5 6
输出:
True
-
非完全二叉树:
复制
1/ \2 3\ \5 7
输出:
False
-
边界情况:
- 空树:
True
- 单节点树:
True
- 空树:
6. 考察能力
- 对完全二叉树定义的理解。
- 层次遍历的应用能力。
- 边界条件的处理(如空树、单节点树)。
- 代码实现的简洁性与鲁棒性。