Java 100 classic machine test questions (12) -- with runnable code

Introduction There will be two classic Java m...
thinking
code
Operation results
thinking
code
Operation results
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
  • Key codes have comments
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[0] + "," + reslut[1] + "]"); */ } } //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 = ; 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.

Please add the two numbers and return a linked list representing sum in the same form.

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 = [0], l2 = [0]

Output: [0]

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

If you try to piece a given single number into a whole number and add it directly to get the result, you will find that it works when there are few digits, but when there are many digits, it is easy to overflow the memory, so we can't simply deal with it. We need to change our thinking. Thinking about the addition of two numbers, we add by bit and then get the result. In fact, there are only two relationships in addition, direct addition and addition, plus the carry from the low bit. For example: 12 + 47, the sum of bits and bits: 2 + 7 = 9, the sum of tens and tens: 1 + 4 = 5. At this time, there is no carry in the low order, so the result is 59 directly; Another example: 14 + 57, the addition of bits and bits: 4 + 7 = 11, the addition of tens and tens: 1 + 5 = 6; Then, at this time, only one bit is reserved for the bit addition result, and the extra ten bit carry is added to the ten bit result calculation: that is, 6 + 1 = 7 result 71. From this, we come to the solution to this problem: add in sequence according to the linked list, and use the mark range to indicate whether the current digit addition exceeds 10, which is used to determine whether the carry is required for the next digit addition. For decimal addition, the carry must be 1.

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(); //Input linked list 1 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(); //Input linked list 2 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(); ListNode result = solution.addTwoNumbers(l1, l2); System.out.println(result.toString()); } } //Linked list data structure 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

Linked list input: [2,4,3], [5,6,4]

Linked list input: [0], [0]

Linked list input [9,9,9,9,9,9,9], [9,9,9]  

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

23 October 2021, 20:10 | Views: 3216

Add new comment

For adding a comment, please log in
or create account

0 comments