文章目录
- 一、简单的数组输入
- 二、构造二叉树(输入)
- 三、构造数组
- 四、构造链表
- 五、二叉树变成数组或者字符串(输出)
- 六、其他情况
-
- 1. 数字+字符串
- 2. BufferedRead用法
- 3. 需要定义全局变量
- 4. 未知多少行输入
- 典型例子
-
- 1. 二叉树:LeetCode删除二叉搜索树的节点(基于Scanner)
- 2. 链表(基于Scanner)
- 3. Scanner超时,使用BufferedRead
常规的读输入输出:scanner基本用法
多行输入输出怎么处理
由字符串变数组再变二叉树,以及反过来怎么操作
由字符串变数组再变链表,以及反过来怎么操作
BufferedRead
一、简单的数组输入
[2,7,11,15]
9
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = s.nextLine();
System.out.println("字符串:"line);
int a = s.nextInt();
System.out.println("整数:"+a);
}
}
二、构造二叉树(输入)
public class Main {
private static class TreeNode {
int val;
TreeNode left, 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 scanner = new Scanner(System.in);
String str = scanner.nextLine();
TreeNode root = StringToTreeNode(str);
}
private static TreeNode StringToTreeNode(String str) {
String[] parts = str
.substring(1, str.length() - 1)
.split(",");
String item = parts[0];
TreeNode root = new TreeNode(Integer.parseInt(item));
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int index = 1;
while (!q.isEmpty()) {
TreeNode node = q.poll();
if (index == parts.length) break;
item = parts[index ++];
if (!item.equals("null")) {
node.left = new TreeNode(Integer.parseInt(item));
q.offer(node.left);
}
if (index == parts.length) break;
item = parts[index ++];
if (!item.equals("null")) {
node.right = new TreeNode(Integer.parseInt(item));
q.offer(node.right);
}
}
return root;
}
}
[1,2,3]
2
三、构造数组
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int[] nums = StringToIntArray(line);
}
private static int[] StringToIntArray(String str) {
String[] parts = str
.substring(1, str.length() - 1)
.split(",");
int[] nums = new int[parts.length];
for (int i = 0; i < parts.length; i ++)
nums[i] = Integer.parseInt(parts[i]);
return nums;
}
}
[1,2,3,4]
[1,2,3,4]
1
2
3
4
四、构造链表
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
System.out.println(line);
ListNode node = StringToListNode(line);
System.out.println(node.val);
System.out.println(node.next.next.next.val);
}
private 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; }
}
private static ListNode StringToListNode(String str) {
String[] parts = str
.substring(1, str.length() - 1)
.split(",");
ListNode dummy = new ListNode(-1), cur = dummy;
for (String s: parts) {
cur.next = new ListNode(Integer.parseInt(s));
cur = cur.next;
}
return dummy.next;
}
}
五、二叉树变成数组或者字符串(输出)
public class Solution7 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int key = sc.nextInt();
String[] parts = line.split(" ");
int len = parts.length;
TreeNode root = strToTree(parts);
TreeNode res = deleteNode(root,key);
Integer[] nums = new Integer[len];
treeToArray(nums,res,0);
String str = Arrays.toString(nums);
str = str.substring(1,str.length()-1);
System.out.println(str.replace(","," "));
}
private static void treeToArray(Integer[] arr,TreeNode node,int index){
if(node==null){
return;
}
arr[index] = node.val;
treeToArray(arr,node.left,2*index+1);
treeToArray(arr,node.right,index*2+2);
}
六、其他情况
1. 数字+字符串
import java.util.Scanner;
public class Solution5 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.nextLine();
for(int i=0;i<num;i++){
String str = sc.nextLine();
int res = countSubString(str);
System.out.println(res);
}
}
public static int countSubString(String s){
boolean[][] dp = new boolean[s.length()][s.length()];
int res = 0;
for(int i=s.length()-1;i>=0;i--){
for(int j=i;j<s.length();j++){
if(s.charAt(i)==s.charAt(j)){
if(j-i<=1){
dp[i][j] = true;
}else{
dp[i][j] = dp[i+1][j-1];
}
}
}
}
for(int i=0;i<s.length();i++){
for(int j=0;j<s.length();j++){
if(dp[i][j]){
res++;
}
}
}
return res;
}
}
2. BufferedRead用法
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.BufferedWriter;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String line = reader.readLine();
writer.write(String X);
writer.newLine();
writer.flush();
}
}
3. 需要定义全局变量
public class Main {
static int n;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
n = in.nextInt();
}
private static void test(int m){
int x = m + n;
}
}
4. 未知多少行输入
有些输入可能是:
输入一个矩阵,每行以空格分隔。
1 2 3
56 8 2 9
对于这种没有给定矩阵行列数的输入,我们只能按照字符串拆分来进行。
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
List<List<Integer>> list = new ArrayList<>();
while (sc.hasNextLine()) {
List<Integer> line = new ArrayList<>();
String str = sc.nextLine();
String[] arr = str.split(" ");
for (String s : arr) {
line.add(Integer.valueOf(s));
}
list.add(line);
}
System.out.println(list);
}
}
典型例子
1. 二叉树:LeetCode删除二叉搜索树的节点(基于Scanner)
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class Solution7 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
int key = sc.nextInt();
String[] parts = line.split(" ");
int len = parts.length;
TreeNode root = strToTree(parts);
TreeNode res = deleteNode(root,key);
Integer[] nums = new Integer[len];
treeToArray(nums,res,0);
String str = Arrays.toString(nums);
str = str.substring(1,str.length()-1);
System.out.println(str.replace(","," "));
}
private 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;
}
}
private static TreeNode strToTree(String[] parts){
TreeNode root = new TreeNode(Integer.parseInt(parts[0]));
int index = 1;
Queue<TreeNode> que = new LinkedList<>();
que.offer(root);
while(!que.isEmpty()){
TreeNode node = que.poll();
if(index == parts.length) break;
String item = parts[index];
index++;
if(!item.equals("null")){
TreeNode left = new TreeNode(Integer.parseInt(item));
node.left = left;
que.offer(left);
}
if(index == parts.length) break;
item = parts[index];
index++;
if(!item.equals("null")){
TreeNode right = new TreeNode(Integer.parseInt(item));
node.right = right;
que.offer(right);
}
}
return root;
}
private static void treeToArray(Integer[] arr,TreeNode node,int index){
if(node==null){
return;
}
arr[index] = node.val;
treeToArray(arr,node.left,2*index+1);
treeToArray(arr,node.right,index*2+2);
}
private static TreeNode deleteNode(TreeNode root,int key){
if(root == null){
return null;
}
if(root.val == key){
if(root.left == null && root.right == null){
return null;
}
if(root.left == null && root.right != null){
return root.right;
}
if(root.left != null && root.right == null){
return root.left;
}
if(root.left != null && root.right != null){
TreeNode node = root.right;
while(node.left!=null){
node = node.left;
}
node.left = root.left;
return root.right;
}
}
if(root.val>key){
root.left = deleteNode(root.left,key);
}else{
root.right = deleteNode(root.right,key);
}
return root;
}
}
2. 链表(基于Scanner)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Solution8 {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int index = sc.nextInt();
sc.nextLine();
String line = sc.nextLine();
ListNode head = strToListnode(line);
ListNode res = removeNthFromEnd(head,index);
ArrayList arr = listnodeToArr(res);
System.out.println(arr.toString());
}
private 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;
}
}
private static ListNode strToListnode(String line){
String[] parts = line.substring(1,line.length()-1).split(",");
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
for (String part : parts) {
cur.next = new ListNode(Integer.parseInt(part));
cur = cur.next;
}
return dummy.next;
}
private static ArrayList listnodeToArr(ListNode head){
ArrayList arr = new ArrayList();
ListNode dummy = new ListNode(-1);
dummy = head;
while(dummy!=null){
arr.add(dummy.val);
dummy = dummy.next;
}
return arr;
}
private static ListNode removeNthFromEnd(ListNode head, int n) {
ListNode dummyNode = new ListNode(-1);
dummyNode.next = head;
ListNode slow = dummyNode;
ListNode fast = dummyNode;
for(int i = 0;i<n+1;i++){
fast = fast.next;
}
while(fast!=null){
fast = fast.next;
slow = slow.next;
}
slow.next = slow.next.next;
return dummyNode.next;
}
}
3. Scanner超时,使用BufferedRead
public class Test03 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(reader.readLine());
for (int i0 = 0; i0 < T; i0++) {
int N = Integer.parseInt(reader.readLine());
char[] a = reader.readLine().toCharArray();
int M = Integer.parseInt(reader.readLine());
char[] b = reader.readLine().toCharArray();
List<PriorityQueue<Integer>> queueList = new ArrayList<>();
queueList.add(new PriorityQueue<>());
queueList.add(new PriorityQueue<>());
for (int i = 0; i < N; i++) {
if (a[i] - '0' < 2) {
queueList.get(a[i] - '0').offer(i + 1);
}
}
int x;
for (int i = 0; i < M; i++) {
if (b[i] == 'M') {
if (!queueList.get(1).isEmpty()) {
x = queueList.get(1).poll();
writer.write(String.valueOf(x));
writer.newLine();
} else {
x = queueList.get(0).poll();
queueList.get(1).offer(x);
writer.write(String.valueOf(x));
writer.newLine();
}
} else {
if (!queueList.get(0).isEmpty()) {
x = queueList.get(0).poll();
queueList.get(1).offer(x);
writer.write(String.valueOf(x));
writer.newLine();
} else {
x = queueList.get(1).poll();
writer.write(String.valueOf(x));
writer.newLine();
}
}
}
writer.flush();
}
}
}