# Introduction

There will be two classic Java machine test questions in each article, and the code will be attached to each question. Each question strives to:

• It can be compiled in JDK11 environment
• Run in the eclipse Java IDE
• Ideas are easy to think, understand and learn

# Question 023     Sum of two numbers (difficulty: ★☆☆☆)

### Title Description:

Given an integer array nums   And an integer target value, please find and as the target value target in the array   And return their array subscripts. You can assume that each input will correspond to only one answer. However, the same element in the array cannot be repeated in the answer. You can return answers in any order. Returns [- 1, - 1] if it does not exist

Tips:

2 <= nums.length <= 104

-109 <= nums[i] <= 109

-109 <= target <= 109

There will only be one valid answer

### Input / output example:

Input: num = [2,7,11,15], target = 9

Output: [0,1]

Input: num = [3,2,4], target = 6

Output: [1,2]

Input: num = [3,3], target = 6

Output: [0,1]

## thinking

A more direct idea is to traverse the array to obtain each value, then subtract the current value with target, and traverse the array to find the subscript of the result

## code

```import java.util.Scanner;

public class Main {

public static void main(String[] args) {
/* Auxiliary input verification code
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int target = scanner.nextInt();
int [] nums = new int[n];
for (int i = 0; i < n; i++) {
nums[i] = scanner.nextInt();
}
scanner.close();

Solution solution = new Solution();
int [] reslut = solution.twoSum(nums, target);

System.out.println("[" + reslut + "," + reslut + "]");
*/

}

}

//Solution function
class Solution {
public int[] twoSum(int [] nums, int target) {
for (int i = 0; i < nums.length; i++) {
int a = nums[i];
int result = target - a;
for (int j = 0; j < nums.length; j++) {
if (j != i && nums[j]== result) {
int[] e = {i, j};
return e;
}
}
}
int [] r = {-1, -1};
return r;
}
}

```

## Operation results   # Question 024     List summation (difficulty: ★★☆☆☆)

### Title Description:

Here you are   A non empty linked list that represents two non negative integers. Each of these numbers is based on   Reverse order   And each node can only store   One   Number.

You can assume that neither of these numbers will be 0 except the number 0   start. Tips:

The number of nodes in each linked list is within the range [1, 100]

0 <= Node.val <= 9

The number represented in the title data guarantee list does not contain leading zeros

### Input / output example:

Input: l1 = [2,4,3], l2 = [5,6,4]

Output: [7,0,8]

Explanation: 342 + 465 = 807

Input: l1 = , l2 = 

Output: 

Explanation: 0 + 0 = 0

Input: L1 = [9,9,9,9,9,9], L2 = [9,9,9]

Output: [8,9,9,9,0,0,1]

Explanation: 999999 + 9999 = 1009998

## thinking

Finally, if the number of a linked list is less than that of another linked list, you can add a 0 by default and continue to add. Note that if the last digit is more than 10, a 1 will be added later in the result;

## code

The key lies in the core solution function Solution(). Other codes are used to assist in verifying the input linked list, constructing the linked list, and constructing the linked list data structure

```import java.util.Scanner;

public class Main {

public static void main(String[] args) {
ListNode l1 = null;
ListNode l2 = null;

ListNode l1Point = null;
ListNode l2Point = null;

Scanner scanner = new Scanner(System.in);

//Enter the length of linked list 1
int n1 = scanner.nextInt();
for (int i = 0; i < n1; i++) {
if (l1 == null) {
l1 = new ListNode(scanner.nextInt());
l1Point = l1;
} else if (l1Point.next == null) {
l1Point.next = new ListNode(scanner.nextInt());
l1Point = l1Point.next;
}
}

//Enter the length of linked list 2
int n2 = scanner.nextInt();
for (int i = 0; i < n2; i++) {
if (l2 == null) {
l2 = new ListNode(scanner.nextInt());
l2Point = l2;
} else if (l2Point.next == null) {
l2Point.next = new ListNode(scanner.nextInt());
l2Point = l2Point.next;
}
}

scanner.close();

Solution solution = new Solution();

System.out.println(result.toString());

}

}

class ListNode {
int val;       //Store the value of the current node
ListNode next; //Store the next node of the current node

//Nonparametric structure
ListNode() {

}

//One parameter structure
ListNode(int val) {
this.val = val;
}

//Two parameter structure
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}

public String toString() {
ListNode currentListNode = this;
String s = "[" + currentListNode.val;
while (currentListNode.next != null) {
currentListNode = currentListNode.next;
s = s + "," + currentListNode.val;
}
s = s + "]";
return s;
}

}

//Core solution function
class Solution {

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int range = 0; //Mark whether the previous bit has carry
ListNode resultListNode = null; //The last result to be returned is a linked list
ListNode tempListNode = null;  //Temporary linked list node

while (l1 != null || l2 != null) {
int sum = 0;
//Whoever is empty takes 0. Otherwise, it takes its own node value
if (l1 == null) {
sum = 0 + l2.val+ range;
} else if (l2 == null) {
sum = l1.val + 0 + range;
} else {
sum = l1.val + l2.val + range;
}

//If the result node is empty, create a new linked list
if (resultListNode == null) {
resultListNode = new ListNode(sum % 10);
tempListNode = resultListNode;
} else {
//Otherwise, add a new node directly at the end of the chain
tempListNode.next = new ListNode(sum % 10);
tempListNode = tempListNode.next;
}

//Mark whether there is carry in this addition
if (sum >= 10) {
range = 1;
} else {
range = 0;
}

//If l1 is not empty, continue to remove the next node
if (l1 != null) {
l1 = l1.next;
}

//If l2 is not empty, continue to remove the next node
if (l2 != null) {
l2 = l2.next;
}
}

//Finally, if there is still carry in the addition of the highest bit, you need to add another carry node
if (range == 1) {
tempListNode.next = new ListNode(range);
}
return resultListNode;

}
}

```

## Operation results ## The above are the two Java machine test questions

If there is any deficiency, you are welcome to criticize and correct

Welcome to the previous article: 100 typical written machine test questions in Java (11)

Welcome to the next article: 100 typical written machine test questions in Java (13)

Author: Little Pumpkin

Date: 09:00, October 24, 2021

Tags: Java

Posted on Sat, 23 Oct 2021 20:10:35 -0400 by phpnewbie81