本文已参与「新人创作礼」活动,一起开启掘金创作之路。
ACM 模式 Input/Output
Template Main
import java.util.*;
import java.lang.*;
public class Main {
public static void main(String[] args) {
// ....
}
}
java.util.Scanner
- nextInt():直至读取到空格或回车之后结束本次的 int 值;
- next():直至读取到空格或回车之后结束本次的 String 值,不可读取回车;
- nextLine():直至读取到换行符(回车)之后结束本次读取的 String,可读取回车(空值)
- hasNext():如果此扫描器的输入中有另一个标记,则返回 true
/* 读取连续整数 */
Scanner sc = new Scanner(System.in);
while(in.hasNext()) {
int a = sc.nextInt();
int b = sc.nextInt();
}
/* 读取有限整数 */
Scanner in = new Scanner(System.in);
int n = in.nextInt();
while(n-- > 0){
int a = in.nextInt();
int b = in.nextInt();
System.out.println(a+b);
}
/* 分隔符 */
Scanner in = new Scanner(System.in);
String[] temp = in.nextLine().split(" ");
java.io.BufferedReader 和 java.io.InputStreamReader
/* 输入为一个字符串 */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
/* 输入为多个数字 */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
String[] strs = line.trim().split(" ");
int n = Integer.parseInt(strs[0]);
转换符
| 转换符 | 说明 | 示例 | 输出 |
|---|---|---|---|
| %s | 字符串 | “Hi,%s:%s.%s”, “王南”,“王力”,“王张” | Hi,王南:王力.王张 |
| %c | 字符 | “字母a的大写是:%c %n”, ‘A’ | 字母a的大写是:A |
| %b | 布尔 | “3>7的结果是:%b %n”, 3>7 | 3>7的结果是:false |
| %d | 整数(十进制) | “100的一半是:%d %n”, 100/2 | 100的一半是:50 |
| %x | 整数(十六进制) | “100的16进制数是:%x %n”, 100 | 100的16进制数是:64 |
| %o | 整数(八进制) | “100的8进制数是:%o %n”, 100 | 100的8进制数是:144 |
| %f | 浮点 | “50元的书打8.5折扣是:%f 元%n”, 50*0.85 | 50元的书打8.5折扣是:42.500000 元 |
| %a | 浮点(十六进制) | “上面价格的16进制数是:%a %n”, 50*0.85 | 上面价格的16进制数是:0x1.54p5 |
| %e | 指数 | “上面价格的指数表示:%e %n”, 50*0.85 | 上面价格的指数表示:4.250000e+01 |
| %g | 通用浮点(f和e类型中较短的) | “上面价格的指数和浮点数结果的长度较短的是:%g %n”, 50*0.85 | 上面价格的指数和浮点数结果的长度较短的是:42.5000 |
| %h | 散列码 | “字母A的散列码是:%h %n”, ‘A’ | 字母A的散列码是:41 |
| %% | 百分比 | “上面的折扣是%d%% %n”, 85 | 上面的折扣是85% |
| %n | 换行符 | ||
| %tx | 日期与时间 |
eg.
String str = null;
str = String.format("Hi, %s, %.2f", "小超", 3.141);
System.out.println(str); // Hi, 小超, 3.14
链表
构造类
static class ListNode {
int val;
ListNode next;
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}
二叉树
构造类(整数节点)
static class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
根据遍历结果构造树
数组
public class ReBulidBinTree{
private static ReBulidBinTree root;
//二叉树结构
public static int val;
public ReBulidBinTree lNode;//左结点
public ReBulidBinTree rNode;//右节点
public ReBulidBinTree(int val) {
this.val = val;
}
public ReBulidBinTree() {
}
/**
* 后序输出
*/
public static ReBulidBinTree reConstructBinaryTree(int [] pre,int [] in) throws Exception {
if(pre==null||in==null) {
return null;
}
if(pre.length!=in.length) {
throw new Exception("长度不一样,非法的输入");
}
root = new ReBulidBinTree();
for (int i = 0; i < in.length; i++) {
if(in[i]==pre[0]) { //确认结点,前序的第一个元素必为根节点。
root.lNode = reConstructBinaryTree(Arrays.copyOfRange(pre,1, i+1), Arrays.copyOfRange(in, 0, i));//中序中在根节点左边的为左子树
root.rNode = reConstructBinaryTree(Arrays.copyOfRange(pre,i+1, pre.length), Arrays.copyOfRange(in, i+1, in.length));//中序中在根节点右边的为右子树
root.val = in[i]; //赋值给结点
System.out.print(root.val+","); //输出结点
}
}
return root;
}
public static void main(String[] args) throws Exception {
int[] pre={1,2,4,7,3,5,6,8};
int[] in={4,7,2,1,5,3,8,6};
ReBulidBinTree root = reConstructBinaryTree(pre,in);
}
}
给定先序字符串
字符串
static class TreeNode{
public char val;
public TreeNode left;
public TreeNode right;
public TreeNode(char val) {
this.val = val;
}
}
static int index = 0;
public static TreeNode buildTree(String line){
index = 0;
return createTreePrevOrder(line);
}
public static TreeNode createTreePrevOrder(String line){
char c = line.charAt(index);
if(c == '#'){
return null;
}
TreeNode root = new TreeNode(c);
index++;
root.left = createTreePrevOrder(line);
index++;
root.right = createTreePrevOrder(line);
return root;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
String line = scanner.next();
TreeNode root = buildTree(line);
inOrder(root);
}
}
public static void inOrder(TreeNode root){
if(root == null){
return;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}
数组
ArrayList
/* 初始化 */
List<String> list1 = new ArrayList<>();
List<String> list2 = new ArrayList<>(Arrays.asList("apple", "banana", "orange"));
List<String> list3 = new ArrayList<>(Collections.nCopies(2, "orange")); // [orange, orange]
// 匿名内部类
List<String> list4 = new ArrayList<>() {
{
add("apple");
add("banana");
add("orange");
}
};
/* 输出 */
System.out.println(list4);
数组和 List 的相互转换
/* List -> Array */
List<String> list = new ArrayList<>(){{add("aa");add("bb");add("cc");}};
// 使用toArray(T[] a)方法
String[] strs = list.toArray(new String[list.size()]);
/* Array -> List */
String[] arrays = new String[]{"aa","bb","cc"};
ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(arrays));
数组自定义排序规则
[void] Arrays.sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) 根据指定比较器引发的顺序对指定对象数组的指定范围进行排序
eg. 区间排序,根据左边界大小升序
Arrays.sort(intervals, new Comparator<int[]>() {
public int compare(int[] interval1, int[] interval2) {
return interval1[0] - interval2[0];
}
});
// lambda 简化为:
Arrays.sort(intervals, (interval1, interval2) -> {
return interval1[0] - interval2[0];
// String: return str1.compareTo(str2);
});
字符串
字符串排序
// str = "abds237sasd9skk"
public static void characterSort(String str) {
System.out.println("原字符串:\n" + str);
char[] chars = str.toCharArray();
Arrays.sort(chars);
// 正序遍历输出
System.out.println("正序输出:");
for (char c : chars) {
System.out.print(c); // 2379aabddkkssss
}
// 倒序遍历输出
System.out.println("\n倒序输出:");
for (int i = chars.length - 1; i >= 0; i--) {
System.out.print(chars[i]); // sssskkddbaa9732
}
}