-
输入
sc.nextInt() //读取整数 sc.next() //读取一个String,空格结尾 sc.nextLine() //读取一行 sc.next().charAt(0) //读取单个字符 /* 若输入一个整数n后,再换行输入n行字符串,则sc.nextLine()读入整数, 而不应该使用sc.nextInt()读入,否则,在循环读入n行字符串时, 第1行数据会读入空串,导致程序逻辑错误。 因为,nextInt()只读取了数字n却没有读取换行符, 下一个nextLine()会读取换行符并解析为空串。 */ int n = Integer.valueOf(sc.nextLine());
-
输出
System.out.println() // 输出行 System.out.println(a+" , "+b) //字符串拼接形式输出 System.out.printf("%d %d", ans1, ans2) //格式化输出
-
字符串操作
String.toLowerCase() //转成小写 String.toUpperCase() //转成大写 // 判断两个字符串是否相同 String a, b; a.equals(b); // 字符串转化为字符数组 String a = "abcdefg"; char str[] = a.toCharArray(); // 数字型字符串转数字 String str = "123"; int a = Integer.valueOf(str).intValue(); //or int a = Integer.parseInt(num); // 字符串数组转字符串 String[] str = {"abc", "bcd", "def"}; StringBuffer sb = new StringBuffer(); for(int i = 0; i < str.length; i++){ sb. append(str[i]); } String s = sb.toString();
-
数学
double pow(double base, double exponent) // 幂次 double sqrt(double d) // 开方 double pow(double num, double 1.0/n) // 开n次方 Math.abs(number) // 取绝对值,类型重载
-
Set
set.add(Object o) // 添加 set.contains(Object o) //判断是否包含 // 复制set, 使用原set作为构造函数的参数 Set<Integer> new_set = new HashSet<>(old_set); // 取差集, set1 包含set2,取set1 - set2 set1.removeAll(set2) // 取交集, 在set1 保留包含set2的部分 set1.retainAll(set2)
-
Map
map.add(Object o) // 添加 map.keySet() //获取键集合 map.getOrDefault(key, defult value) //获取值,不存在键则返回设定默认值 map.containsKey(key) //判断是否包含该键
-
队列
// 队列 Queue<String> queue = new LinkedList<>() // 双端队列 Deque deque = new LinkedList() offerFirst(e) // 队首加 offerLast(e) // 队尾加 pollFirst() // 队首减 pollLast() // 队尾 减 // 优先队列 PriorityQueue<Integer> queue = new PriorityQueue<>(); // 优先队列 逆序 PriorityQueue<Integer> queue = new PriorityQueue<>((a, b) -> b - a); // 优先队列 对象 PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[1] - b[1]); // or PriorityQueue<int[]> queue = new PriorityQueue<>(new Comparator<int[]>() { @Override public int compare(int[] a, int[] b) { if(a[0] == b[0]) { return a[1] - b[1]; } return a[0] - b[0]; } });
-
List
// List 拼接 list.addAll(list1) // 带值初始化 new ArrayList<>(Arrays.asList(-1)) // 深拷贝 listB = new ArrayList<>(listA)
-
数组
// 数组初始化 int[] base = {1, 0, 0, 1}; // 数组截取 int[] a = new int[]{3, 9, 20, 15, 7}; int [] b = Arrays.copyOfRange(a,0,2);//b:[3,9]
-
栈
Stack<Integer> stack = new Stack<>();
-
排序
//一维、二维、自定义、索引排序 // list sort Collections.sort(List); // 二维数组排序 // nums.size(): (n, 2), 按数组中第2个元素升序排序 Arrays.sort(nums,(a,b)->a[1]-b[1]); // 数组索引排序 // 按difficulty数组从小到大将其索引数组排序 // index是Integer类型 Arrays.sort(index, (a, b) -> { return difficulty[a] - difficulty[b];});
-
gcd
public int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
-
并查集
private int MAXN = 300; private int[] fa = new int[MAXN]; // 父亲节点 private int[] rank = new int[MAXN]; // 树结点数 // 初始化 private void init(int n) { for(int i = 0; i < n; i++) { fa[i] = i; rank[i] = 1; } } // 找到 x 的根节点, 根据根节点是否相同判断是否是同一棵树 private int find(int x) { return x == fa[x] ? x : (fa[x] = find(fa[x])); } // 合并 i 和 j 所在的两棵树 private void merge(int i, int j) { int x = find(i); int y = find(j); if(x == y) { // 已在同一棵树 return; } if(rank[x] <= rank[y]) { // y树节点更多,x挂到y下面 fa[x] = y; rank[y] += rank[x]; } else { fa[y] = x; rank[x] += rank[y]; } }
-
Runtime Error
-
数组越界
-
数值溢出
-
Map访问不存在的键(先判断键是否存在)
-