Flash written test questions

Multiple choice questions

What protocol does the ping command not involve?
A: DNS
B: TCP
C: ARP
D: ICMP

B. Ping is based on the ICMP protocol, and ARP and DNS are used to resolve routes.

Three people a, b, and c participate in a subject competition. Each subject is given points for x, y, and z in order of first, second, and third place. It is known that a gets 22 points, b and c get 9 points, and b is the first place in mathematics. Let me ask you What results can be derived?

Insufficient conditions

Given that there are 768 complete binary tree nodes, how many leaf nodes are there?

Even number (n1 = 1): n0= n/2;
Odd number (n1 = 0): n0=(n+1)/2;

Programming questions

sum of two numbers

Idea: as follows
Insert image description here

 vector<int> twoSum(vector<int>& nums, int target) {
    
    
 	unordered_map<int, int> mp;
 	for(int j = 0; j < nums.size();j++){
    
    
 		auto it = mp.find(target - nums[j];
 		if( it != mp.end())
 			return {
    
    it->second, j};
 		mp[nums[j]] = j;
 	} 
 	return {
    
    };
 }

Stack Sorting
Question:
Sort the stack so that the smallest element is at the top of the stack. At most, you can use one other temporary stack to store data, but you must not copy elements into other data structures (such as arrays).

https://blog.csdn.net/jiaomubai/article/details/102319275
Solution:

Push the auxiliary stack sequentially from the main stack until the top element of the main stack is not larger than the top element of the auxiliary stack (keep the auxiliary stack elements in descending order from top to bottom), take out the element of the
main stack first, and then continue to push the auxiliary stack elements. Pop the stack to the main stack until the top element of the auxiliary stack is smaller than this element,
and then continue to push from the main stack to the auxiliary stack to cycle

//stack1为主栈,stack2为辅助栈
stack<int> sort(stack<int> stack1){
    
    
    stack<int> stack2;
    while (!stack1.empty()){
    
    
		int temp = stack1.top();
		stack1.pop();
		//如果辅助栈不为空且当前元素比辅助栈栈顶元素小,则将辅助栈中元素弹出压入主栈中
		while (!stack2.empty() && stack2.top() > temp){
    
    
		    stack1.push(stack2.top());
		    stack2.pop();
		}
		//如果辅助栈为空或者当前元素比辅助栈栈顶元素大,则将当前元素直接压入辅助栈中
		stack2.push(temp);
    }
    return stack2;
}

Guess you like

Origin blog.csdn.net/BinBinCome/article/details/133467723