输入输出模板
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static StreamTokenizer in = new StreamTokenizer(br);
public static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
自定义排序
- 实现
Comparable<T>
接口,重写compareTo()
方法
static class Log implements Comparable<Log> {
int ts, id;
Log (int ts, int id) {
this.ts = ts;
this.id = id;
}
@Override
public int compareTo(Log o) {
int n1 = this.ts - o.ts;
int n2 = n1 == 0 ? this.id - o.id : n1;
return n2;
}
}
// public static void sort(Object[] a, int fromIndex, int toIndex)
Arrays.sort(logs, 0, n);
- 实现
Comparator<T>
接口,重写compare
方法
java刷题必备排序,Arrays.sort()和Collections.sort()
翻转去重函数
// 翻转
public static void reverse(int[] g) {
for (int i = 0; i < n / 2; i++) {
int temp = g[i];
g[i] = g[n - i - 1];
g[n - i - 1] = temp;
}
}
// 判重
public static int unique(int[] arr) {
int t = 0;
for (int i = 0; i < cnt; i++) {
if (i == 0 || arr[i] != arr[i - 1])
arr[t++] = arr[i];
}
return t;
}
ArrayList
去重
ys = (ArrayList<Double>) ys.stream().distinct().collect(Collectors.toList());
快读模板
class Read {//自定义快读 Read
public BufferedReader reader;
public StringTokenizer tokenizer;
public Read(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public String nextLine() {
String str = null;
try {
str = reader.readLine();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
return str;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
}