[notes on February 7]

Posted by andym01480 on Mon, 07 Feb 2022 19:40:38 +0100

A room with headlights on on a cloudy day

catalogue

Option 1

The following statement is correct

The total number of handshakes generated by establishing and closing a tcp is

What are the similarities between TCP protocol and UDP protocol in computer networks

Given the port number bound to a process, the command to query the process is

The following protocols belong to the application layer:

02 code

Lee_921: add the least number of valid parentheses

Lee_ 1492: factor k of n

Option 1

The following statement is correct

Threads in java are non preemptive

Threads in java cannot share data

Every java program has at least one thread, the main thread

Threads in java cannot share code

java multithreading is based on preemptive implementation
 Multithreading in java can use a shared memory area: heap method area
 A piece of code in java is either an instance method or a static method. The code itself is stored in the method area and belongs to the thread sharing area

The total number of handshakes generated by establishing and closing a tcp is

7

6

3

5

The number of handshakes refers to the number of network communications, that is, the number of network packets sent
 Establish three handshakes and four waves, a total of seven
​

What are the similarities between TCP protocol and UDP protocol in computer networks

Connectionless oriented protocol

Connection oriented protocol

The rest of the options are wrong

Transport layer protocol

Both tcp and udp are transport layer protocols. tcp is connected and udp is connectionless.

Given the port number bound to a process, the command to query the process is

ps

find

netstat

show

Known process name, query method: ps -ef |grep process name
 If the process port is known, the query method is netstat -anp|grep process name

The following protocols belong to the application layer:

TCP

IP

ARP

FTP

tcp is the transport layer
 ip is a network layer protocol
 arp is a protocol between data link layer and network layer
​

02 code

Lee_921: add the least number of valid parentheses

Given a string S composed of '(' and ')' parentheses, we need to add the least parentheses ('(' or ')' anywhere) to make the resulting parenthesis string valid.

Formally speaking, the parenthesized string is valid only if one of the following points is satisfied:

It is an empty string, or it can be written as AB (A connects with B), where A and B are valid strings, or it can be written as (A), where A is valid string. Given A parenthesis string, returns the minimum number of parentheses that must be added for the result string to be valid.

Source: LeetCode link: Force buckle The copyright belongs to Lingkou network. For commercial reprint, please contact the official authorization, and for non-commercial reprint, please indicate the source.

public int minAddToMakeValid(String s) {
        if (s.length() == 0 || s == null) return 0;
        int count  = 0;
        int res = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '('){
                count++;
            }else {
                count--;
            }
​
            if (count < 0 ){
                res++;
                count = 0;
            }
​
        }
        return res+=count;
    }

Lee_ 1492: factor k of n

Force buckle

//Use stack for violent resolution;
class Solution {
     public int kthFactor(int n, int k) {
        Stack<Integer> stack = new Stack<>();
​
        for (int i = 1; i <= n ; i++) {
            if (n %i == 0){
                stack.push(i);
            }
        }
​
​
        if (stack.size() < k) {
            return -1;
        }
        int len = stack.size();
        for (int i = 0; i < len-k ; i++) {
            stack.pop();
        }
        return stack.peek();
    }
}
//optimization
public class Test1 {
    public int KthFac(int n ,int k){
        int count = 0 ,i;
        for (i = 1;  i*i<=n ; i++) {
            if (n % i == 0){
                count++;    
                if (count == k){
                     return i;
                }// If the value of k has been changed before
    
            }
​
        }
        i--;
        if (i*i == n){
            i--;
        }
        for (; i >0 ; i--) {
            if (n% i == 0 ){
                count++;
                if (count == k){
                    return n/i;
                }
            }
        }
        return -1;
    }
​
​
}
​
​
​

    

Topics: Java network Interview