考前记得下插件 Translation 可以翻译函数内方法解释
Time类
- LocalTime 仅对时间的作用 24h内
plusMinutes()
plusHours()
pluseSeconds()
minus~()
- LocalDate 仅对日期做用,不包含日期
getDayOfWeek()得到星期
lengthOfYear()一年有多少天
isLeapYear()是否是闰年
getDayOfYear()今年已经过去多少天
LocalTime中plus~()方法也可以在中使用
3.LocalDateTime 时间日期
实例化
LocalDateTime time=LocalDateTime.of(year,month,dayofMonth,hour,minute,second);
BigInteger 大数
//两种实例化方法
1. BigInteger bigInteger1= new BigInteger("99966622233");
BigInteger bigInteger2=BigInteger.valueOf(12334455L);
2.add(); 大整数相加
3.subtract(); 相减
4.multiply(); 相乘
5.divide(); 相除取整
6.remainder(); 取余
7.pow(); a.pow(b)=a^b
8.gcd(); 最大公约数
9.abs(); 绝对值
10.negate(); 取反数
11.mod(); a.mod(b)=a%b=a.remainder(b);
12.max(); min();
13.punlic int comareTo(); 大于返回1 小于返回-1
14.boolean equals(); 是否相等
15.mod() 取余,取模
进制转化
Integer.to~
String toHexString(int i) 十进制转十六进制
String toBinaryString(int i) 十进制转二进制
String toOctalString(int i) 十进制转八进制
Integer.toString(num,r) 十进制转r进制
Integer.parseInt("123",r) r进制转十进制
递归 与 回溯
1.递归
二分查询
while()方法
递归方法
例题:
求数的阶乘
注意:递归中使用return调用方法递归,回溯是直接调用方法,ruturn可以得到答案后直接返回。
2.回溯
- 深度优先搜索(dfs)
例题:
对数的全排列
n皇后
手链样式
2. 广度优先搜索(bfs)
例题:
迷宫
路径问题
注意:n皇后的条件中对角线的判断,在一条对角线是 两个点的行和列,
if(Math.abs(nl[i]-row)==Math.abs(rl-line)) return false;
注意:str+str去重原理 ss=str+str,ss.reverse() 在用ss.indexOf(str)查子字符串
最优路径
注意:01背包用二维数组 状态转移方程: if(weight[i]>j) dp[i][j]=dp[i-1][j]; else dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-weight[i]]+price[i]); 完全背包用一维数组 dp[j] = max(dp[j],dp[j-w[i]]+v[i]); | |||
1,01背包 完全背包 | |||
2,dijkstra() o(n*2)空间复杂度 floyd () o(n*3) 空间复杂度 | |||
注意:最大公因数(最大公约数) 两数相同最大因子 最小公倍数 两数作为因子组成的最小数 两数之积==最大公约数*最小公倍数 最大公约数可以由辗转相除法得出 | |||
注意:求余数可以用取模的方法(模仿手动计算)temp=(temp*10+(num.charAt(i)-'0'))%模;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
class Main {
public static void main(String[] args) {
//time();
// BigIntegerMethod();
//toOtherBase();
int[] nums={3,8,6,1,2};
// bubbleSort(nums);
// selectionSort(nums);
// System.out.println(binarySearch(nums,8,0,nums.length-1));
// for (int demo:nums
// ) {
// System.out.println(demo);
// }
boolean[] useNum=new boolean[nums.length];
// dfs(nums,useNum,0,"");
// System.out.println(set.size());
// for (String temp:set
// ) {
// System.out.println(temp);
// }
bagMethod();
}
public static void time(){
LocalTime time=LocalTime.of(13,21,59);
time=time.plusMinutes(20);
time=time.plusHours(21);
time=time.minusHours(21);
// Duration duration=Duration.between()
System.out.println(time);
LocalDate date=LocalDate.of(2022,4,8);
LocalDate dateLate=LocalDate.of(2022,4,9);
//得到星期
System.out.println(date.getDayOfWeek());
//这个月的第几天
System.out.println(date.getDayOfMonth());
//这一年有的天数
System.out.println(date.lengthOfYear());
//是否为闰年
System.out.println(date.isLeapYear());
//date时间在dateLate时间之前
System.out.println(date.isBefore(dateLate));
LocalDateTime dateTime=LocalDateTime.of(2022,8,9,11,20,31);
//今年已经过去多少天了
System.out.println(dateTime.getDayOfYear());
}
public static void BigIntegerMethod(){
//两种实例化方法
BigInteger bigInteger1= new BigInteger("99966622233");
BigInteger bigInteger2=BigInteger.valueOf(12334455L);
//相加
bigInteger1=bigInteger2.add(bigInteger1);
System.out.println(bigInteger1);
//相减
bigInteger1=bigInteger1.subtract(bigInteger2);
//相乘
bigInteger1=bigInteger2.multiply(bigInteger1);
//绝对数
bigInteger1=bigInteger1.abs();
System.out.println(bigInteger1);
//相除
bigInteger1=bigInteger1.divide(bigInteger2);
System.out.println(bigInteger1);
//取余
bigInteger1=bigInteger1.remainder(bigInteger2);
System.out.println(bigInteger1);
//幂次方
bigInteger1=bigInteger1.pow(2);
//取余但是永远为非负数
bigInteger1.mod(bigInteger2);
System.out.println(bigInteger1);
//最大公约数
System.out.println(bigInteger1.gcd(bigInteger2));
//取反数
System.out.println(bigInteger1.negate());
//比大小 大于取1 小于取-1
System.out.println(bigInteger1.compareTo(bigInteger2));
}
public static void toOtherBase(){
System.out.println(20);//十进制
System.out.println(Integer.toHexString(20));//十六进制
System.out.println(Integer.toBinaryString(20));//二进制
System.out.println(Integer.toOctalString(20));//八进制
System.out.println(Integer.toString(20,2));//向radix进制转化
System.out.println(Integer.parseInt("100",2));//r进制转十进制
}
//冒泡排序 o(1) or o(2)复杂度
public static int[] bubbleSort(int[] array){
for (int i = 0; i < array.length ; i++) {
for (int j = 0; j <array.length-1-i ; j++) {
//改变大小符号可以改变排列顺序
if(array[j]>array[j+1]){
int temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}return array;
}
//选择排序 稳定o(2)
public static int[] selectionSort(int[] array){
for (int i = 0; i < array.length; i++) {
int minIndex=i;
for (int j = i; j < array.length ; j++) {
if(array[j]<array[minIndex]){
minIndex=j;
}
}
int temp=array[minIndex];
array[minIndex]=array[i];
array[i]=temp;
}
return array;
}
//二分查找
public static int binarySearch(int[] array,int num){
int left=0;
int right=array.length-1;
while (left<=right){
int mid=(right-left)/2+left;//防止数据溢出
if(array[mid]==num) return mid;
//目标数在左边
else if(array[mid]>num){
right=mid-1;
}
else if(array[mid]<num){
left=mid+1;
}
}
return -1;
}
//递归版 二分查询
public static int binarySearch(int[] array,int num,int left,int right){
if(left>right) return -1;
int mid=(right-left)/2+left;//防止数据溢出
if(array[mid]==num) return mid;
//目标数在左边
else if(array[mid]>num){
return binarySearch(array,num,left,mid-1);
}
else if(array[mid]<num){
return binarySearch(array,num,mid+1,right);
}
return -1;
}
//dfs深度优先搜索
static Set<String> set=new HashSet<>();
//全排列数组中的数字
public static void dfs(int[] array,boolean[] useNum,int count,String str){
//截至条件
//改变下行条件可以改变全安排列的个数
if(count>=5){
set.add(str);
//回溯
return;
}
for (int i = 0; i < array.length; i++) {
if(!useNum[i]){
useNum[i]=true;
str+=array[i];
dfs(array,useNum,count+1,str);
str=str.substring(0,str.length()-1);
useNum[i]=false;
}
}
}
//bfs 广度优先搜索
static class pair{
int x,y;
//记忆路径
String path="";
public pair(int x,int y,String path){
super();
this.x=x;
this.y=y;
this.path=path;
}
}
public static void bfs(){
int[][] area = {
{ 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0 },
{ 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1 },
{ 0, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0,
0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 1 },
{ 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,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1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1,
0, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1,
1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 1,
0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0 },
{ 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1,
0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0 },
{ 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1,
0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0 } };
//DLRU 优先级
int[][] dir={{1,0},{0,-1},{0,1},{-1,0}};
Deque<lianqiao1455.pair> deque=new LinkedList<>();
//起点
lianqiao1455.pair start=new lianqiao1455.pair(0,0,"");
deque.offer(start);
String[] roads={"D","L","R","U"};
while (!deque.isEmpty()){
lianqiao1455.pair p1=deque.poll();
//对四个方向搜索
for (int i = 0; i < 4; i++) {
int line=p1.x+dir[i][0];
int row=p1.y+dir[i][1];
//处理没有方向的路
if(row<0||line>29||row>49||line<0||area[line][row]==1) continue;
String road=p1.path+roads[i];
//实例化四个方向的坐标,存入队列
lianqiao1455.pair p2=new lianqiao1455.pair(line,row,road);
deque.offer(p2);
//走过的路变成1
area[line][row]=1;
//终点判断
if(row==49&&line==29){
System.out.println(p2.path);
}
}
}
}
/*a 重1 价值5
b 重2 价值7
c 重3 价值10
d 重5 价值6
*/
//01 背包问题
public static void bagMethod(){
//4个物品 8个空间 需要背包初始化
int[] price={0,5,7,10,6};
int[] weight={0,1,2,3,5};
int[][] dp=new int[4+1][8+1];
for (int i = 1; i < dp.length; i++) {
for (int j = 1; j < dp[i].length; j++) {
if(weight[i]>j) dp[i][j]=dp[i-1][j];
else dp[i][j]=Math.max(dp[i-1][j],dp[i-1][j-weight[i]]+price[i]);
}
}
System.out.println(dp[4][8]);
}
//最短路径
private static void dijkstra() {
long[][] distance=new long[2021][2021];
//得到点到其他相邻点的距离
for (int i = 0; i <2021; i++) {
for (int j = 0; j <2021 ; j++) {
if(i==j) distance[i][j]=0;
else if(Math.abs(i-j)<=21) distance[i][j]=getGB(i+1,j+1);
else distance[i][j]=999999999L;
}
}
boolean[] useSpot=new boolean[2021];//选择点
long[] dis=new long[2021];
for (int i = 0; i <2021 ; i++) {
dis[i]=999999999L;
}
dis[0]=0;
for (int i = 0; i <2021 ; i++) {
int minIndex=0;
long min=999999999L;
for (int j = 0; j < 2021; j++) {
if(!useSpot[i]&&Math.abs(i-j)<=21){
if(min>dis[j]){//选出最优路线
minIndex=j;
min=dis[j];
}
}
}
for (int j = 0; j < 2021; j++) {
if(distance[minIndex][j]!=0&&min+distance[minIndex][j]<dis[j]){
dis[j]=min+distance[minIndex][j];
}
}
useSpot[minIndex]=true;
}
System.out.println(dis[2020]);
}
public static void floyd () {
long[][] num=new long[2021][2021];
for (int i = 0; i <2021; i++) {
for (int j = 0; j <2021 ; j++) {
if(i==j) num[i][j]=0;
else if(Math.abs(i-j)<=21) num[i][j]=getGB(i+1,j+1);
else num[i][j]=99999999L;
}
}
for (int i = 0; i <2021 ; i++) {
for (int j = 0; j <2021 ; j++) {
for (int k = 0; k < 2021; k++) {
if(num[j][i]+num[i][k]<num[j][k])
num[j][k]=num[j][i]+num[i][k];
}
}
}
System.out.println(num[0][2020]);
}
//求最小公倍数
public static long getGB(long i,long j){
//最大公因数 两数之积等于最大公因数乘最小公倍数
long temp=i*j;
while (i%j!=0){
long tempI=i;
long tempJ=j;
i=j;
j=tempI%tempJ;
}
return temp/j;
}
}