普通输入
Scanner sc = new Scanner(System.in);
快速输入
BufferedReader cin = new BufferedReader(new InputStreamReader(System.in));
并在main抛出异常
throws Exception
输出
System.out.printf()
大数据量时输出
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
out.printf("%d\n", 123);
out.flush();//刷新流 一定要写
读取文件
//填写绝对路径
FileReader fileReader = new FileReader("XXXXXX");
//传入相应的FileReader即可
BufferedReader cin = new BufferedReader(fileReader);
//读取方式,与之前的一致
cin.readLine();
常用api
//数组拷贝(只能拷贝一维,对于多维只能for循环进行拷贝)
int[] arr = {1, 2, 3, 4};
int[] clone = arr.clone();
//数组排序,在区间进行排序[i, j]
Array.sort(clone, i, j + 1);
//自定义排序以及区间
Integer arr = new Integer[n];
Arrays.sort(arr, 1, n + 1, (a, b)->{
int s1 = s(a);
int s2 = s(b);
return s1 == s2 ? a - b : s1 - s2;
});
//向上取整
Math.floor()
//向下取整
Math.ceil()
//多维数组初始化
int[][][] a = new int[1][1][1];//更方便些
//转int型
Integer.parseInt("02") //2
//输出一个数字补0
printf("%02d", 1);//01
//Pair键值对
Pair<Integer, Integer> pair = new Pair<>(1, 2);
System.out.println(pair.getKey());
System.out.println(pair.getValue());
//唯一集合容器
Set<xx> t = new HashSet<xx>();
t.add(xx); [若是添加已经有,返回false]
t.contains(xx)
//栈集合
Stack<xx> s = new Stack<xx>();
s.push(xx);
s.isEmpty()
s.pop()
//动态数组集合(可动态扩容的一个数组)
List<Integer> list = new ArrayList<Integer>();
list.size();
list.add();
list.get(i);
list.remove(i);
list.indexOf(i);
list.contains(i);
list.indexOf(i);
//链表集合
new LinkedList<Node>() getFirst() addFirst removeLast() isEmpty()
//双端队列
ArrayDeque<Integer> dq = new ArrayDeque<Integer>(); dq.peekFirst() dq.peekLast() dq.pollFirst() dq.pollLast(); dq.getFirst() dq.getLast() dq.addLast(i)
//队列(链表集合中就有队列的实现)
Deque<TreeNode> queue = new LinkedList<>(); 出队:queue.poll() 入队:queue.offer(node.left); 查看第一个:queue.peekFirst() 删除最后一个:queue.removeLast()
//哈希表
HashMap<Integer, Integer> map = new HashMap<>(); map.containsKey();
//优先队列
PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
//集合转数组
String[] strArrayTrue = (String[]) arrayList.toArray(new String[0]);
//集合转数组:
ArrayList<Integer> res = new ArrayList<>();int[] ts = (int[])(res.toArray(new int[]));
//自定义比较器
compare()返回值:https://blog.csdn.net/weixin_44998686/article/details/109550175
return 0:不交换位置,不排序
return 1:交换位置
return -1:不交换位置
return o1-o2:升序排列
return o2-o1:降序排列
//打印long类型
System.out.printf("%s", String.valueOf(1L));
快速幂模板
long FastPower(long base, long power) { //base是底数,power是幂数,result是结果
long result= 1;
while(power > 0) {
if((power & 1) != 0) {
result*= base;
power -= 1;
}
base *= base;
power >>= 1;
}
return result;
}
gcd/lcm
int gcd(int a,int b)
{
return b == 0 ? a : gcd(b,a%b) ;
}
lcm
a*b/gcd(a,b)
大顶堆
(PriorityQUeue默认小顶堆)
PriorityQueue<Integer>bigHeap=new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2-o1;
}
});
String类型不可改变,如果需要改变可以使用StringBuilder
String 常用API
public char charAt(int index):返回字符串中第index个字符;
public int length():返回字符串的长度;
public int indexOf(String str):返回字符串中第一次出现str的位置;
public int indexOf(String str,int fromIndex):返回字符串从fromIndex开始第一次出现str的位置;
public String substring(int beginIndex):返回该字符串从beginIndex开始到结尾的子字符串;
public String substring(int beginIndex,int endIndex):返回该字符串从beginIndex开始到endsIndex结尾的子字符串
public int compareTo(String str):比较字符串的大小,小于返回负数,大于返回正数,相等返回0
public boolean equals(String str):判断字符串是否一样
public char[] toCharArray () :将此字符串转换为新的字符数组
public String[] split(String regex):将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组
StringBuilder常用API
创建Stringbuilder对象:StringBuilder strB = new StringBuilder();
append(String str) / append(Char c):字符串连接
toString():返回一个与构建起或缓冲器内容相同的字符串
setCharAt(int i, char c):将第 i 个代码单元设置为 c(可以理解为替换)
insert(int offset, String str) / insert(int offset, Char c):在指定位置之前插入字符(串)
delete(int startIndex,int endIndex):删除起始位置(含)到结尾位置(不含)之间的字符串
length():获得长度
快速幂a^b %p
private static int pow(int n, int m)
int res = 1;
int base = n;
while(m != 0) {
if ((m&1) == 1) {
res = res * base;
}
base = base * base;
m = m >> 1;
}
return res;
}
并查集
Dji