第一天
作业:已知二叉树的
前序遍历:ABCDEFGHK
中序遍历:BDCAEHGKF
根据已有条件,画出这个二叉树,同时输出该二叉树的后序遍历
后序遍历:DCBHKGFEA
作业:课堂案例
作业:
给定一个数字,求该数字的二进制的1的个数
import java.util.Scanner;
public class Zuo {
public static void main(String[] args) {
int count=0;
Scanner iu=new Scanner(System.in);
System.out.print("请输入一个数:");
int num=iu.nextInt();
int tmp;
while (num!=0 )
{
tmp = num % 2;
num = num / 2;
if (tmp == 1)
count++;
}
System.out.println("1的个数为"+count);
}
}
运行结果为:
请输入一个数:10
1的个数为2
作业: 给定一个数组,该数组中除了一个元素只出现了一次,其他元素都出现两次
找到这个没有重复的元素
import java.util.Arrays;
public class Zuo {
public static void main(String[] args) {
int[] arr={1,2,2,3,3};
System.out.println(Arrays.toString(new Integer[]{forSoution(arr)}));
}
public static Integer forSoution(int[] nums) {
for (int i = 0; i < nums.length; i++) {
//相同个数
int equalCount = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[i] == nums[j]) {
equalCount += 1;
}
}
// 除本身外没有其他相同的数字了
if (equalCount == 1)
return nums[i];
}
return -1;
}
}
作业:
给定一个数组,数组的元素共N+1个, 元素是从1到n的联系自然数,其中一个重复值
找到这个重复的元素
public class Test01 {
public int findLoop(int nums[]){
int slow =0;
int fast = 0;
while (true){//遍历数组,让其第一次相遇y
slow = nums[slow];
fast =nums[nums[fast]];
if (slow == fast){//其在y点相遇
slow = 0;//slow从头开始遍历
fast = nums[nums[fast]];//fast从y继续遍历,让fast在环里继续走
while (slow != fast){//不相等,继续遍历,相等了,就停止
slow = nums[slow];
fast = nums[fast];
}
return slow;//环开始的位置,也是其题目求得重复的位置
}
}
}
排序和查找
算法
该算法指的是《数据结构与算法》中的算法
|-- 排序
|-- 查找
排序(sort)
将无序的数据安装特定的规则排成有序数据(升序、降序)
常见的排序算法,有十几种,比较多,有些非常抽象。
|-- 冒泡
|-- 选择
|-- 插入
冒泡分析:
arr = [1, 100, -10, 50, 3, -5, 96] 7个元素
大数上浮法:
如何完成排序:
原有顺序 1, 100, -10, 50, 3, -5, 96
1 1, -10, 50, 3, -5, 96, 100 // 100
2 -10, 1, 3, -5, 50, 96, 100 // 96
3 -10, 1, -5, 3, 50, 96, 100 // 50
4 -10, -5, 1, 3, 50, 96, 100 // 3
小数下沉法:
自己研究下
冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
// 开始每一次两两比较
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
private static void swap(int[] arr, int i, int j) {
arr[j] = arr[j] ^ arr[i];
arr[i] = arr[j] ^ arr[i];
arr[j] = arr[j] ^ arr[i];
}
选择分析:
1, 100, -10, 50, 3, -5, 96
最小值:
假设第一个值为最小值,之后一直查找,直到找到真正的最小值,交换这两个数
原 1, 100, -10, 50, 3, -5, 96
1 -10, 100, 1, 50, 3, -5, 96
2 -10, -5, 1, 50, 3, 100, 96
3 -10, -5, 1, 50, 3, 100, 96
4 -10, -5, 1, 3, 50, 100, 96
5 -10, -5, 1, 3, 50, 100, 96
6 -10, -5, 1, 3, 50, 96, 100
选择排序
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
// j对应的下标才是最小值
min = j;
}
}
// 假设失败,真正的最小值是min
if (min != i) {
swap(arr, min, i);
}
}
}
插入分析
1, 100, -10, 50, 3, -5, 96
以第一个为有序数组,之后的所有元素依次插入到这个有序数组中,插入时,必须保证
数组一直有序!!!
原 1, 100, -10, 50, 3, -5, 96
1 1,100, -10, 50, 3, -5, 96
2 -10,1,100, 50, 3, -5, 96
3 -10,1, 50,100, 3, -5, 96
4 -10,1,3, 50,100, -5, 96
5 -10, -5,1,3, 50,100, 96
6 -10, -5,1,3, 50,96, 100
插入排序
public static void insertSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
// 取下一个元素,倒着插入,保证插入时数组一直有序
for (int j = i + 1; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr, j, j - 1);
}
}
}
}
public static void main(String[] args) {
int[] arr = {1, 100, -10, 50, 3, -5, 96};
System.out.println("排序前:") ;
for (int i : arr) {
System.out.print(i + ", ");
}
// 冒泡排序
// bubbleSort(arr);
// 选择排序
// selectSort(arr);
// selectSort02(arr);
// 插入排序
insertSort(arr);
System.out.println("\n排序后:") ;
for (int i : arr) {
System.out.print(i + ", ");
}
Scanner sc = new Scanner(System.in);
System.out.print("请输入您要查找的数:");
int target = sc.nextInt();
// 使用二分查找完成数据查询
// int index = binarySearch01(arr, target);
// 使用递归完成二分查找
int index = binarySearch02(arr, target, 0, arr.length - 1);
System.out.println("你要查找的数,在数组的"+ index +"位置");
}
// 二分查找,递归完成
public static int binarySearch02(int[] arr, int target, int start, int end) {
int middle = (start + end) >> 1;
if (target == arr[middle]) {
return middle;
} else if (target > arr[middle]) {
return binarySearch02(arr, target, middle + 1, end);
} else if (target < arr[middle]) {
return binarySearch02(arr, target, start, middle - 1);
}
return -1;
}
// 二分查找
public static int binarySearch01(int[] arr, int target) {
int start = 0, end = arr.length - 1;
while (start <= end) {
int middle = (start + end) >> 1;
if (arr[middle] > target) {
end = middle - 1;
} else if (arr[middle] < target) {
start = middle + 1;
} else {
return middle;
}
}
return -1;
}
// 插入排序
public static void insertSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
// 取下一个元素,倒着插入,保证插入时数组一直有序
for (int j = i + 1; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
swap(arr, j, j - 1);
}
}
}
}
// 选择排序
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int min = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[min]) {
// j对应的下标才是最小值
min = j;
}
}
// 假设失败,真正的最小值是min
if (min != i) {
swap(arr, min, i);
}
}
}
// 选择排序改进,让代码的稳定性增加,效率提高
public static void selectSort02(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[i]) {
swap(arr, j, i);
}
}
}
}
// 冒泡排序
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
// 开始每一次两两比较
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr, j, j + 1);
}
}
}
}
private static void swap(int[] arr, int i, int j) {
arr[j] = arr[j] ^ arr[i];
arr[i] = arr[j] ^ arr[i];
arr[j] = arr[j] ^ arr[i];
}
}
算法的特性:
算法的时间复杂度
冒泡:O(n^2)
选择:O(n^2)
算法的空间复杂度
算法的稳定性
冒泡:稳定的排序算法
选择:不稳定
查找算法
都是针对有序数据而言的!!!
二分查找(折半查找):
针对于有序的序列,可以直接查找中间值
二分查找排序
public static int binarySearch01(int[] arr, int target) {
int start = 0, end = arr.length - 1;
while (start <= end) {
int middle = (start + end) >> 1;
if (arr[middle] > target) {
end = middle - 1;
} else if (arr[middle] < target) {
start = middle + 1;
} else {
return middle;
}
}
return -1;
}
二分查找排序,用递归
public static int binarySearch02(int[] arr, int target, int start, int end) {
int middle = (start + end) >> 1;
if (target == arr[middle]) {
return middle;
} else if (target > arr[middle]) {
return binarySearch02(arr, target, middle + 1, end);
} else if (target < arr[middle]) {
return binarySearch02(arr, target, start, middle - 1);
}
return -1;
}
第二天
作业:Stack类的实现
import java.util.Arrays;
public class Stack<E> {
private static Object[] array;
private int size = 0;
public Stack() { //默认初始化方法
this(10);//指定栈空间进行初始化
}
public Stack(int initialCapacity) {
if (initialCapacity <= 0) {
throw new RuntimeException("初始化栈空间错误");
}
array = new Object[initialCapacity];
}
public E push(E item) {
ensureCapacityHelper(size + 1);//获取栈顶元素
array[size++] = item;
return item;
}
public E peek() {
if (isEmpty()) {
throw new IndexOutOfBoundsException("栈为空");//出栈,获取栈顶元素
}
return (E) array[size - 1];
}
public E pop() {
E item = peek();//判断栈是否为空
size--;
return item;
}
public boolean isEmpty() { //入栈前调用该函数,保证栈空间够用
return size == 0;
}
private void ensureCapacityHelper(int minCapacity) {//扩容操作
if (minCapacity > array.length) {
grow();
}
}
private static void grow() {
int oldCapacity = array.length;
int newCapacity = oldCapacity * 2;
if (newCapacity < oldCapacity) {
throw new OutOfMemoryError();
} else {
array = Arrays.copyOf(array, newCapacity);
}
}
public static void main(String[] args) {
}
}
作业2:
定义一个Admin类,该类存在,username、password属性,实现一个控制台版的用户注册登录案例
将注册的用户写在一个数组中。
import java.util.Arrays;
import java.util.Scanner;
public class Admin {
public static String username;
public static int password;
static String[] name = new String[5];
static String[] pwd = new String[5];
static boolean flag = false;
static Scanner iu = new Scanner(System.in);
static int a=0;
static int b=0;
public static void main(String[] args) {
wu();
}
public static void wu() {
Admin admin = new Admin();
System.out.println("欢迎进入用户界面:1.登录 2.注册 3.查看数组");
System.out.print("请选择:");
int x = iu.nextInt();
if (x == 1) {
userSign();
}
if (x == 2) {
userRegister();
wu();
}
if (x == 3) {
Look();
wu();
}
}
public static void Look() {
System.out.println(Arrays.toString(name));
System.out.println(Arrays.toString(pwd));
}
public static void userSign() { //登录
System.out.println("=======欢迎进入登陆界面=======");
System.out.print("请输入用户名:");
String a = iu.next();
System.out.print("请输入密码:");
String b = iu.next();
for (int i = 0; i < name.length; i++) {
if (a.equals(name[i]) && b.equals(pwd[i])) {
flag = true;
break;
}
}
if (flag) {
System.out.println("登陆成功");
} else {
System.out.println("登陆失败");
}
}
public static void userRegister() { //注册
if (a<5) {
System.out.println("=======欢迎进入注册界面=======");
System.out.println("请输入用户名:");
String userName = iu.next();
name[a] = userName;
a++;
System.out.println("请输入密码:");
String passWord = iu.next();
pwd[b] = passWord;
b++;
}else {
System.out.println("注册值已到数组最大储存");
}
}
}
可以添加5个用户并登录
作业3:
定义一个猫类(Cat),该猫有名字、性别、年龄、主人、皮毛
public class Cat {
String name;
char sex;
int age;
String user;
String color;
public static void main(String[] args) {
Cat cat=new Cat();
cat.name="花花";
cat.sex='母';
cat.age=2;
cat.user="王帅哥";
cat.color="黑白色";
System.out.println("这只小喵咪叫"+cat.name);
System.out.println("她是"+cat.sex+"的,今年"+cat.age+"岁");
System.out.println("她的主人是"+cat.user+",她是"+cat.color);
}
}
运行结果:
这只小喵咪叫花花
她是母的,今年2岁
她的主人是王帅哥,她是黑白色
作业5:
class57.MyInteger类(类与对象,构造函数)
public class MyInteger { //类
String name;
int age;
double score;
public MyInteger(String name, int age, double score) { //构造函数
this.name = name;
this.age = age;
this.score = score;
}
public static void main(String[] args) {
MyInteger iu=new MyInteger("王Howe",18,98.5); //对象
}
}
作业6:
Class58.自定义String类(类与对象,构造函数)
public class MyInteger { //类
public static void main(String[] args) {
String s=new String("王帅哥",19,99999);
}
}
class String{ //自定义String类
String name;
int age;
double score;
public String(java.lang.String 王帅哥, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
}
面向对象的编程入门
面向对象的编程:
什么是面向对象的编程:
OOP(Oriented Object Programming):面向对象
什么面向过程:
提出问题,分解问题,解决问题
类:(class),类别
在Java中,使用class关键字定义类
|-- 属性
类的固有特征
|-- 行为和动作
函数function(方法 method)
对象:(object),一个类别中的具体案例(实例:实实在在的案例):
如何定义类
在java中,定义类,使用class关键字
访问修饰符 class 类名称 {
}
如何创建对象
// 定义类,在根据类来创建对象
// 可以通过类的构造方法来创建对象,使用new关键字
类型 对象名称 = new 构造函数();
Scanner sc = new Scanner(System.in);
Random random = new Random();
public class Test { //有一个Test类,public是公开访问修饰符
public static void main(String[] args) {
Test bat=new Test(); //创建了一个bat对象
}
}
如何操作【访问、设置】属性和方法
对象名称.xxxx
public class Test { //有一个Test类,public是公开访问修饰符
String name; //属性
int age;
public static void method(){ //方法被调用时必须是静态
}
public static void main(String[] args) {
Test bat=new Test(); //创建了一个bat对象
bat.name="123"; //调用属性
bat.method(); //调用方法
}
}
二叉树的三种遍历:
前序:跟 左 右
中序:左 跟 右
后序:左 右 根
例如:
前序遍历:ABCDEFGHK
中序遍历:BDCAEHGKF
后序遍历:DCBHKGFEA
内容:
Arrays工具类 数组相关的操作
import java.util.Arrays; //导入Arrays包
public class Test {
public static void main(String[] args) {
int[] arr={4,2,9,8,6};
int[] arr1={1,2};
Arrays.sort(arr);//sort给数组排序
Arrays.fill(arr,4); //填充数组
System.out.println(Arrays.toString(arr));//打印数组
System.out.println(Arrays.equals(arr,arr1));//比较数组元素是否相等
}
}
可变参数 ...
面向对象的编程(OOP)
类 class 抽象概念
对象 具体的案例
对象.属性 // 访问对象的属性
对象.属性 = 值 // 设置对象的属性的值
对象.方法(实参列表); // 调用对象的方法
public class Test { //有一个Test类,public是公开访问修饰符
String name; //属性
int age;
public static void method(){ //方法被调用时必须是静态
}
public static void main(String[] args) {
Test bat=new Test(); //创建了一个bat对象
bat.name="123"; //调用属性
bat.method(); //调用方法
}
}
有只猫,有很多属性
名字
性别
年龄
花色
主人
class Cat{
String name;
char sex;
int age;
String color;
String master;
public static void main(String[] args) {
Cat cat=new Cat();
cat.name="花花";
cat.sex='公';
cat.age=2;
cat.color="白色";
cat.master="王帅哥";
}
}
主人类,也有很多属性
姓名
性别
年龄
地址
有猫
class Method{
String name;
char sex;
int age;
String address;
String cat;
public static void main(String[] args) {
Method method=new Method();
method.name="王Howe";
method.sex='男';
method.age=3;
method.address="翻斗花园";
method.cat="花花";
}
}