总结

一、HashSet集合

特点:

  • 元素的无序性
  • 不可重复性(插入重复元素不报错)
  • 允许集合元素值为null

构造:

HashSet<数据类型> 集合名 = new HashSet<数据类型>(); //指定集合类型
HashSet 集合名 = new HashSet(); //不指定集合类型

添加元素

//向hashset中添加一个字符串
hashset.add("abc");
//向hashset中添加一个整数
hashset.add(1);
//向hashset中添加一个字符
hashset.add('a');
//向hashset中添加一个数组
int[] abc={10,11,12};
hashset.add(abc);
//向hashset中添加一个自定义对象
Cat cat1=new Cat("asd", 2);
hashset.add(cat1);

hashset.add(E e):返回boolean型,如果此 set 中尚未包含指定元素,则添加指定元素;如果此 set 已包含该元素,则该调用不更改 set 并返回 false。

遍历HashSet

Iterator it = set.iterator();
while(it.hasNext()) {
	Object obj = it.next();
	System.out.println(obj);
}

其他常用方法

hashset.clear()//从此 set 中移除所有元素。
hashset.remove(Object o)//如果指定元素存在于此 set 中,则将其移除。
hashset.isEmpty()//如果此 set 不包含任何元素,则返回 true。
hashset.contains(Object o)//如果此 set 包含指定元素,则返回 true。
hashset.size()//返回此 set 中的元素的数量(set 的容量)。

二、StringBuffer 和 StringBuilder 类
StringBuffer 字符串变量(线程安全)
StringBuilder 字符串变量(非线程安全)
当对字符串进行修改的时候,特别是字符串对象经常改变的情况下,需要使用 StringBuffer 和 StringBuilder 类。
和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
StringBuilder 类和 StringBuffer 之间的最大不同在于 StringBuilder 的方法不是线程安全的(不能同步访问)。
由于 StringBuilder 相较于 StringBuffer 有速度优势,所以多数情况下建议使用 StringBuilder 类。然而在应用程序要求线程安全的情况下,则必须使用 StringBuffer 类。

创建对象

StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();

常用方法:StringBuilder同StringBuffer

buf.append() //将指定的字符串追加到此字符序列。
buf.reverse() //将此字符序列用其反转形式取代。
buf.delete(int start, int end) //移除此序列的子字符串中的字符。
buf.insert(int i, t) //将t插入到下标i所在的位置,后面字符后移。
buf.replace(int start, int end, String str) //使用给定 String 中的字符替换此序列的子字符串中的字符。
buf.indexOf(String str) //返回第一次出现的指定子字符串在该字符串中的索引。
buf.setCharAt(int index, char ch) //将给定索引处的字符设置为 ch。
buf.substring(int start, int end) //返回一个新的 String,它包含此序列当前所包含的字符子序列。

四、ArrayList
ArrayList是一个数组队列,相当于动态数组
定义

List<Order> list = new ArrayList<Order>();

自定义排序

Collections.sort(list,new Comparator<Order>() {
			@Override//对订单按照店铺id和时间ts排序
			public int compare(Order arg0, Order arg1) {
				if(arg0.id!=arg1.id)
					return arg0.id-arg1.id;
				else
					return arg0.ts-arg1.ts;
			}
		});
// Collection中定义的API
boolean             add(E object)
boolean             addAll(Collection<? extends E> collection)
void                clear()
boolean             contains(Object object)
boolean             containsAll(Collection<?> collection)
boolean             equals(Object object)
int                 hashCode()
boolean             isEmpty()
Iterator<E>         iterator()
boolean             remove(Object object)
boolean             removeAll(Collection<?> collection)
boolean             retainAll(Collection<?> collection)
int                 size()
<T> T[]             toArray(T[] array)
Object[]            toArray()
// AbstractCollection中定义的API
void                add(int location, E object)
boolean             addAll(int location, Collection<? extends E> collection)
E                   get(int location) //可以用来遍历List
int                 indexOf(Object object)
int                 lastIndexOf(Object object)
ListIterator<E>     listIterator(int location)
ListIterator<E>     listIterator()
E                   remove(int location)
E                   set(int location, E object)
List<E>             subList(int start, int end)
// ArrayList新增的API
Object               clone()
void                 ensureCapacity(int minimumCapacity)
void                 trimToSize()
void                 removeRange(int fromIndex, int toIndex)

Bob和Alice一共出现了几次,虽然感觉应该放在一块看,但是可以分别存储Bob和Alice的位置,再处理。题目中绑定在一起的元素处理时不一定也要绑定处理,可以先分别处理,再共同处理。

BigInteger不是基本数据类型之一,它其实更像String,是Java里的一个类
既然不是基本数据类型,所以大数的加减乘除也不能使用+、-、*、/这些运算符号,
Java也没有对这些运算符号进行重定义,取而代之的是用一些方法来代替,比如add()、subtract()、mutiply()、divide()这四种方法,

//BigInteger提供的常用的方法:
BigInteger abs()  返回大整数的绝对值
BigInteger add(BigInteger val) 返回两个大整数的和
BigInteger and(BigInteger val)  返回两个大整数的按位与的结果
BigInteger andNot(BigInteger val) 返回两个大整数与非的结果
BigInteger divide(BigInteger val)  返回两个大整数的商
double doubleValue()   返回大整数的double类型的值
float floatValue()   返回大整数的float类型的值
BigInteger gcd(BigInteger val)  返回大整数的最大公约数
int intValue() 返回大整数的整型值
long longValue() 返回大整数的long型值
BigInteger max(BigInteger val) 返回两个大整数的最大者
BigInteger min(BigInteger val) 返回两个大整数的最小者
BigInteger mod(BigInteger val) 用当前大整数对val求模
BigInteger multiply(BigInteger val) 返回两个大整数的积
BigInteger negate() 返回当前大整数的相反数
BigInteger not() 返回当前大整数的非
BigInteger or(BigInteger val) 返回两个大整数的按位或
BigInteger pow(int exponent) 返回当前大整数的exponent次方
BigInteger remainder(BigInteger val) 返回当前大整数除以val的余数
BigInteger leftShift(int n) 将当前大整数左移n位后返回
BigInteger rightShift(int n) 将当前大整数右移n位后返回
BigInteger subtract(BigInteger val)返回两个大整数相减的结果
byte[] toByteArray(BigInteger val)将大整数转换成二进制反码保存在byte数组中
String toString() 将当前大整数转换成十进制的字符串形式
BigInteger xor(BigInteger val) 返回两个大整数的异或
 文件流三件套
 PrintStream out=System.out;//创建一个打印流out,此流已打开并准备接受输出数据。
 PrintStream ps = new PrintStream(new File("ans.txt"));//创建又一具有指定文件名的打印流ps(不带自动刷新)
 System.setOut(ps); //重新分配“标准”输出流,重定向输出到ps对象中(即指定路径名文件中)。若去掉此句,则内容都会在console面板中输出,
 System.out.println(a.toString()+b.toString()+"i");//在已定向的输出路径中打印输出字符串内容
 System.setOut(out);//重定向输出到out对象中(即屏幕上)

一层循环到1亿
两层循环到1万
三层循环到500

不要忽略对第一条数据的检验
考虑对不同的情况,是否都能执行到

判断闰年
1.能被4整除而不能被100整除。
2.能被100整除也能被400整除。

long取值范围
-9223372036854775808~9223372036854775807
LONG的写法是:long l = 9223372036854775806L
注意后面的一个L,必须加上去。

int取值范围
-2147483648到+2147483648

队列先进先出,栈先进后出。

String转int

double   d   =   Double.parseDouble(s); 
float   f   =   Float.parseFloat(s);
int i = Integer.parseInt([String]);
Integer.valueOf(String s)

int转String有三种方式

(1)num + ""
(2)String.valueOf(num)
(3)Integer.toString(num)

进制转换函数

System.out.println(n + "的二进制是:" + Integer.toBinaryString(n));
		System.out.println(n + "的八进制是:" + Integer.toOctalString(n));
		System.out.println(n + "的十六进制是:" + Integer.toHexString(n));
		System.out.println(n + "的三进制是:" + Integer.toString(n, 3));

空位补0

String str=String.format("%5d", 1).replace(" ", "0");//5代表总共是几位数

时间类
在这里插入图片描述

c.set(1990,12,3);

斐波那契数列
在这里插入图片描述

字节单位
1TB=1024GB
1GB=1024MB
1MB=1024KB
1KB=1024B

java保留两位小数

double data = 3.02;
String result = String.format("%.1f",data);
System.out.println(result);//输出3.0

BFS

package Hello10;
/*
 * 最短路:R、D、L、U
*字典序:"D L R U"的顺序,保证了在步数最小的前提下最小字典序一定会最早出现。
 */
import java.util.*;

public class Main {
	public static int[][] maze= new int[1001][1001]; //存储0-1迷宫矩阵
	public static int[][] vis= new int[1001][1001]; //存储该点是否访问过
	public static Node[][] pre= new Node[1001][1001];//存储前一个结点的数组:二维Node数组
	public static class Node{ //自定义Node类型,包括三个属性:横坐标、纵坐标、到该点的步数
		int x; 
		int y;
		int step;
		public Node(int x,int y) { //两种初始化方式
			this.x=x;
			this.y=y;
		}
		public Node(int x,int y,int step) {
			this.x=x;
			this.y=y;
			this.step=step;
		}
	}
	public static void Print(int n,int m) {
		Node p = new Node(n,m); //获取迷宫最后一个结点
		Node print[] = new Node[n*m+1]; //构造打印Node一维数组
		int len=0; //len记录打印数组的长度
		while(p!=null) { //当p不为空
			print[len++]=p;//将p存入打印数组
			p = pre[p.x][p.y];//将p指向p的前一个数组
		}
		for(int i=len-1;i>0;i--) { //因为从后向前存储的,入口在数组的末尾,所以从后向前遍历
			Node j = print[i]; //前一步
			Node k =print[i-1];//后一步
			if(j.x==k.x && j.y<k.y)
				System.out.print('R');
			if(j.x==k.x && j.y>k.y)
				System.out.print('L');
			if(j.y==k.y && j.x<k.x)
				System.out.print('D');
			if(j.y==k.y && j.x>k.x)
				System.out.print('U');
		}
	}
	public static void Bfs(int n,int m) {
		int next[][] = {{1,0},{0,-1},{0,1},{-1,0}}; //next二维数组存储上下左右四个方向
		LinkedList<Node> queue = new LinkedList<Node>(); //LinedList<自定义类型>实现先进先出
		Node begin = new Node(1,1,0); //构造begin结点
		queue.add(begin); //begin结点加入队列
		vis[1][1]=1; //置begin结点为访问过
		pre[1][1]=null; //begin结点的前一个结点为空
		while(!queue.isEmpty()) { //当队列不为空
			Node node = queue.poll(); //弹出队首结点
			if(node.x==n && node.y==m) { //如果该结点为迷宫的终点
				System.out.println(node.step); //则输出步数并结束
				break;				
			}
			for(int i=0;i<4;i++) { //遍历四个方向
				int x = node.x+next[i][0]; //获取四个方向的坐标
				int y = node.y+next[i][1];
				if(x<1 || y<1 || x>n || y>m || vis[x][y]==1 || maze[x][y]==1) { //如果四个方向坐标不符合要求或者已经访问过
					continue;													//或者迷宫里走不通,则跳过本次循环
				}
				queue.add(new Node(x,y,node.step+1)); //否则加入队列
				vis[x][y]=1; //标记为已经访问过
				pre[x][y]=node; //保存前一个结点
			}
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		for(int i=1;i<=n;i++) {
			String str = sc.next();
			char s[] = str.toCharArray();
			for(int j=1;j<=m;j++) {
				maze[i][j]=s[j-1]-'0';
			}
		}
		Bfs(n,m);
		Print(n,m);
	}

}

最小生成树Prim

package Hello11;

/*
 * 以sqrt((x_1-x_2)*(x_1-x_2)+(y_1-y_2)*(y_1-y_2))+(h_1-h_2)*(h_1-h_2)为权值
 * 用Prim算法得到最小生成树即为答案。
 */

import java.util.*;
public class Main9 {
	public static double dist[][] = new double[1005][1005]; //两点之间的距离
	public static int vist[] = new int[1005]; //点是否已经加入树
	public static double lowc[] = new double[1005]; //点离树的距离
	public static Node node[] = new Node[1005]; //保存输入数据
	public static class Node{
		int x;
		int y;
		int h;
		public Node(int x,int y,int h) {
			this.x = x;
			this.y = y;
			this.h = h;
		}
	}
	public static double Prim(int n) {
		double price = 0; //最小花费
		for(int i=1;i<=n;i++) { //以村庄1为起点,lowc[i]初始化为每个村庄i到村庄1的距离
			lowc[i]=dist[1][i];
		}
		for(int i=1;i<=n;i++) { //n个点:所以n次循环
			double min = 1000000000; //初始化最小值为无穷大
			int p = -1; //p记录本次循环距离最小生成树最近的点
			for(int j=1;j<=n;j++) {
				if(vist[j]==0 && lowc[j]<min) { //如果没有加入树,并且到树的距离比min小,
					min = lowc[j]; //则更新min
					p = j; //更新p
				}
			}
			if(min==1000000000) return -1; //如果min仍为无穷大,则该图为不连通图
			vist[p] = 1; //标记村庄p加入最小生成树
			price+=min; //更新花费
			for(int j=1;j<=n;j++) { //如果未加入的村庄j,通过加入的村庄p距离更近了
				if(vist[j]==0 && lowc[j]>dist[p][j]) {
					lowc[j] = dist[p][j]; //则更新lowc[j]
				}
			}
		}
		return price;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		for(int i=1;i<=n;i++) {
			node[i] = new Node(sc.nextInt(),sc.nextInt(),sc.nextInt());
		}
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=n;j++) {		
				double len = Math.sqrt(
						 (node[i].x-node[j].x)*(node[i].x-node[j].x)
						+(node[i].y-node[j].y)*(node[i].y-node[j].y))
						+(node[i].h-node[j].h)*(node[i].h-node[j].h);
				dist[i][j]=len;
			}
		}
		double result = (double)((int)(Prim(n)*100))/100; //结果保留两位小数
		//String result = String.format("%.2f",Prim(n));
		System.out.print(result);
	}
}

迪杰斯特拉(一个节点到其余节点的最短距离)

package Hello8;

import java.util.*;
public class Dijskra {
	public static int cost[][] = new int[51][51];
	public static int lowcost[] = new int[51];
	public static int pre[] = new int[51];
	public static boolean vist[] = new boolean[51];
	public static int n;
	public static void dijskra(int s) {
		for(int i=1;i<=n;i++) {
			lowcost[i] = 10000000;
			vist[i] = false;
			pre[i] = -1;
		}
		lowcost[s] = 0;
		for(int i=1;i<=n;i++) {
			int k=-1;
			int min=10000000;
			for(int j=1;j<=n;j++) {
				if(vist[j]==false && lowcost[j]<min) {
					min=lowcost[j];
					k=j;
				}
			}
			if(k==-1) break;
			vist[k]=true;
			for(int j=1;j<=n;j++) {
				if(vist[j]==false && lowcost[k]+cost[k][j]<lowcost[j]) {
					lowcost[j]=lowcost[k]+cost[k][j];
					pre[j]=k;
				}
			}
		}
		
	}
	public static void Print(int s) {
		if(s==1) {
			for(int i=1;i<=n;i++) {
				if(i!=s && lowcost[i]!=10000000) System.out.print(lowcost[i]);
				else if(lowcost[i]==10000000) System.out.print(-1);
				if(i!=n && i!=s) System.out.print(" ");
			}		
		}
		else if(s==n) {
			for(int i=1;i<=n;i++) {
				if(i!=s && lowcost[i]!=10000000) System.out.print(lowcost[i]);
				else if(lowcost[i]==10000000) System.out.print(-1);
				if(i!=s && i!=n-1) {
					System.out.print(" ");
				}
			}
		}
		else {
			for(int i=1;i<=n;i++) {
				if(i!=s && lowcost[i]!=10000000) System.out.print(lowcost[i]);
				else if(lowcost[i]==10000000) System.out.print(-1);
				if(i!=s && i!=n) {
					System.out.print(" ");
				}
			}			
		}
		System.out.println();
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		int s = sc.nextInt();
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=n;j++) {
				cost[i][j] = sc.nextInt();
				if(i!=j && cost[i][j]==0) cost[i][j]=10000000;
			}
		}
		dijskra(s+1);
		Print(s+1);
	}

}

弗洛伊德(任意两点之间最短路)

package Hello8;

import java.util.*;
public class Floyd {
	public static int ans[][] = new int[51][51];
	public static int n;
	public static void floyd() {
		for(int k=1;k<=n;k++) {
			for(int i=1;i<=n;i++) {
				for(int j=1;j<=n;j++) {
					ans[i][j]=Math.min(ans[i][j], ans[i][k]+ans[k][j]);
				}
			}
		}
	}
	public static void Print() {
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=n;j++) {
				if(j!=1) System.out.print(" ");
				if(ans[i][j]==100000000)
					System.out.print(-1);
				else
					System.out.print(ans[i][j]);
			}
			System.out.println();
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		n =sc.nextInt();
		for(int i=1;i<=n;i++) {
			for(int j=1;j<=n;j++) {
				ans[i][j] = sc.nextInt();
				if(i!=j && ans[i][j]==0) {
					ans[i][j]=100000000;
				}
			}
		}
		floyd();
		Print();
	}

}

递归关键是找结束条件,找n与n-1的规律。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值