数组输入

import java.util.*;

/*
对于每一组测试数据,第一行输入两个整数 n,k 代表这个序列的长度和要判断的元素
接下来输入 n 个整数,a[i] 表示序列中第 i 个元素
举例:
5 8
1 2 3 4 5
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

while (sc.hasNext()) {
int n = sc.nextInt();
int k = sc.nextInt();
int[] nums = new int[n];

for (int i = 0; i < n; i++) {
nums[i] = sc.nextInt();
}

// write code here ...
}
}
}
import java.io.*;
import java.util.*;

/**
* 输入一个 n * m 的图
* 举例:
* 4 4
* 1122
* 1222
* 3111
* 3333
*/
public class Main {
// 记得抛异常
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] params = br.readLine().trim().split(" ");
int n = Integer.parseInt(params[0]);
int m = Integer.parseInt(params[1]);
char[][] grid = new char[n][m];
for(int i = 0; i < n; i++)
grid[i] = br.readLine().trim().toCharArray();
}
}

链表输入

import java.util.*;

/*
输入一串数字,用逗号分隔
举例:
1,2,3,4,5
*/
public class Main {
static class ListNode {
int val;
ListNode next;

ListNode() {
}

ListNode(int val) {
this.val = val;
}

ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

while (sc.hasNext()) {
String s = sc.next().toString();
String[] arr = s.split(",");
int[] ints = new int[arr.length];

for (int i = 0; i < ints.length; i++) {
ints[i] = Integer.parseInt(arr[i]);
}

ListNode dummyHead = new ListNode(0);
ListNode cur = dummyHead;

for (int i = 0; i < ints.length; i++) {
cur.next = new ListNode(ints[i]);
cur = cur.next;
}
cur.next = null;

// write code here ...
}
}
}

树的输入 1

import java.io.*;
import java.util.*;

/*
第一行两个数 n, root,分别表示二叉树有 n 个节点,第 root 个节点是二叉树的根
接下来共 n 行,第 i 行三个数 val_i, left_i, right_i
分别表示第 i 个节点的值 val 是 val_i,左孩子 left 是第 left_i 个节点,右孩子 right 是第 right_i 个节点
节点 0 表示空
举例:
5 1
5 2 3
1 0 0
3 4 5
4 0 0
6 0 0
*/
public class Main {
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 static void main(String[] args) throws Exception {
// write code here ...
}
}

树的输入 2

import java.util.*;

/*
输入的第一行表示节点的个数 n(2 ≤ n ≤ 1000,节点的编号为 0 到 n - 1)组成
下面是 n-1 行,每行有两个整数,第一个数表示父节点的编号,第二个数表示子节点的编号
举例:
5
0 1
0 2
1 3
1 4
*/
public class Main {
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 static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int numberOfNode = sc.nextInt();
HashMap<Integer, TreeNode> nodeMap = new HashMap<>();

int rootVal = sc.nextInt();
TreeNode root = new TreeNode(rootVal);
int firstChildVal = sc.nextInt();
TreeNode firstChild = new TreeNode(firstChildVal);
root.left = firstChild;

nodeMap.put(firstChildVal, firstChild);
nodeMap.put(rootVal, root);

for (int i = 0; i < numberOfNode - 2; i++) {
int parentVal = sc.nextInt();
int childVal = sc.nextInt();
TreeNode child = new TreeNode(childVal);
TreeNode parent = nodeMap.get(parentVal);

if (parent != null) {
if (parent.left == null) {
parent.left = child;
} else if (parent.right == null) {
parent.right = child;
}
} else {
parent = new TreeNode(parentVal);
parent.left = child;
}

nodeMap.put(parentVal, parent);
nodeMap.put(childVal, child);
}

// write code here ...

sc.close();
}
}