“This is the 24th day of my participation in the First Challenge 2022. For details: First Challenge 2022.”
takeaway
In the just concluded daily algorithm & Interview questions, dACHang special training for 28 days and sprint Dachang daily algorithm & Interview questions, dynamic planning for 21 days of training we came here together. But learning can not stop ah, from today we start Java intensive training (algorithm && interview questions) the first day and then roll up.
Java training
Given a non-empty singly linked list with head, return the middle node of the list.
If there are two intermediate nodes, the second intermediate node is returned.
The sample1: Input: [1.2.3.4.5] Output: nodes in this list3(Serialized form: [3.4.5]) returns the value of the node3. (Evaluation system serialized expression of this node is [3.4.5]). Notice that we return an object of type ans: ans.val =3, ans.next.val = 4, ans.next.next.val = 5And ans.next-next-next = NULL.Copy the code
The sample2: Input: [1.2.3.4.5.6] Output: nodes in this list4(Serialized form: [4.5.6]) Since the list has two intermediate nodes, the values are respectively3 和 4, we return to the second node.Copy the code
The disadvantage of linked lists is that they cannot be accessed by subscripts. So we can think about iterating through the list, and putting the iterated elements into array A. If we iterate over N elements, then the length of the list and array is also N, and the corresponding intermediate node is A[N/2].
Initialization:/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; }} * * /Part of the solution:class Solution {
public ListNode middleNode(ListNode head) {
ListNode[] A = new ListNode[100];
int t = 0;
while(head ! =null) {
A[t++] = head;
head = head.next;
}
return A[t / 2]; }}Copy the code
Given the root node of the binary tree, root, returns the sum of all left leaves.
The sample1: Enter: root = [3.9.20.null.null.15.7] output:24Explanation: In this binary tree, there are two left leaves, respectively9 和 15, so return24
Copy the code
The sample2: Enter: root = [1] output:0
Copy the code
Initial section:/** * Definition for a binary tree node. * public 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; *} *} */Solution: Depth-first searchclass Solution {
public int sumOfLeftLeaves(TreeNode root) {
returnroot ! =null ? dfs(root) : 0;
}
public int dfs(TreeNode node) {
int ans = 0;
if(node.left ! =null) {
ans += isLeafNode(node.left) ? node.left.val : dfs(node.left);
}
if(node.right ! =null && !isLeafNode(node.right)) {
ans += dfs(node.right);
}
return ans;
}
public boolean isLeafNode(TreeNode node) {
return node.left == null && node.right == null; }}Copy the code
The interview questions
Can the String class be loaded? When was the previous String loaded? A String with the same fully qualified class name cannot be loaded because of the parent delegate mechanism. Java.lang. String is loaded by the Bootstrap Classloader, the top-level Classloader. whenclassWhen a file is loaded into memory, other constants from the class file constant pool are loaded into the runtime constant pool, but string constants are not. It first creates a string object in the heap, and then stores a reference to that object in the global string constant pool. , description,ThreadLocalWhat are the underlying implementation principles and common scenarios for thread-local variables? How it works: Cost eachThreadThere's one inside each threadThreadLocalMap; With threads as keys and generics as values, you can think of thread-level caching. Each thread gets a separate map. Lent provides access methods such as set and GET, which store a separate copy for each thread using the variable, so the GET method always returns the latest value set by the current executing thread when it calls set. Application Scenarios: AOP ThreadLocal is a class that solves the problem of thread concurrency by creating local variables for a thread. We know that all threads of an object share its global variables. So these variables are not thread-safe and we can use synchronization techniques. But when we don't want to use synchronization, we can choose the ThreadLocal variable. For example, because JDBC connection objects are not thread-safe, they are not thread-safe when multithreaded applications use global variables without collaboration. By storing JDBC connection objects in ThreadLocal, each thread has its own copy of the connection objectCopy the code