Leetcode刷题 - ACM模式模板

1. 【输入输出】普通数组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static void main(String[] args) {
//输入
Scanner scanner = new Scanner(System.in);
String[] numsStr = scanner.nextLine().split(" ");

int[] nums = new int[numsStr.length];
for(int i=0;i<nums.length;i++){
nums[i] = Integer.parseInt(numsStr[i]);
}
int target = scanner.nextInt();

//解题
int[] result = solution(nums, target);

//输出
System.out.println(Arrays.toString(result));
}

2. 【输入输出+链表定义】链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package hot100.listnode;

import java.util.*;

public class AddTwoNumbers {

static class ListNode{
int val;
hot100.listnode.ListNode next;
public ListNode(){}
public ListNode(int val){
this.val = val;
next = null;
}
}

public static ListNode solution(ListNode l1, ListNode l2) {
...
}

private static ListNode arrayToListNode(int[] arr) {
ListNode dummy = new ListNode(-1);
ListNode cur = dummy;
for (int i = 0; i < arr.length; i++) {
cur.next = new ListNode(arr[i]);
cur = cur.next;
if (i == arr.length - 1) {
cur.next = null;
}
}
return dummy.next;
}

public static void main(String[] args) {
// 输入
Scanner scanner = new Scanner(System.in);
String[] numsStr1 = scanner.nextLine().split(",");
int[] nums1 = new int[numsStr1.length];
for (int i = 0; i < numsStr1.length; i++) {
nums1[i] = Integer.parseInt(numsStr1[i]);
}
String[] numsStr2 = scanner.nextLine().split(",");
int[] nums2 = new int[numsStr2.length];
for (int i = 0; i < numsStr2.length; i++) {
nums2[i] = Integer.parseInt(numsStr2[i]);
}

ListNode list1 = arrayToListNode(nums1);
ListNode list2 = arrayToListNode(nums2);

//解题
ListNode result = addTwoNumbers(list1, list2);

//输出
while(result!=null){
System.out.print(result.val);
if(result.next!=null){
System.out.print("->");
}
result = result.next;
}
}
}

3. 【输入输出+二叉树定义】二叉树

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package hot100.BinaryTree;

import java.util.*;

public class InorderTraversal {
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 buildTree(String[] arr) {
if (arr.length == 0 || arr[0].equals("null")) {
return null;
}

Queue<TreeNode> que = new LinkedList<>();
TreeNode root = new TreeNode(Integer.parseInt(arr[0]));
que.offer(root);

int index = 1;
while (!que.isEmpty() && index < arr.length) {
TreeNode currentNode = que.poll();
if (!arr[index].equals("null")) {
currentNode.left = new TreeNode(Integer.parseInt(arr[index]));
que.offer(currentNode.left);
}
index++;

if (index < arr.length && !arr[index].equals("null")) {
currentNode.right = new TreeNode(Integer.parseInt(arr[index]));
que.offer(currentNode.right);
}
index++;

}

return root;
}

public static void inorderTraversal(TreeNode node) {
if (node == null) {
return;
}
inorderTraversal(node.left);
System.out.println(node.val);
inorderTraversal(node.right);
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] numStr = scanner.nextLine().split(",");
TreeNode root = buildTree(numStr);

inorderTraversal(root);
}
}