2022 5 Bytes Internship --- Pico

Part 1 Self-introduction + project

10min self-introduction and project introduction (didn't dig deep into the project, I got a general understanding of it)


Part II Basics

About 20 minutes;
the interviewer asks targeted questions based on the content on the resume. Before asking you about some knowledge, he will ask you how you have mastered it. How often do you come into contact with projects by yourself. It’s quite humane, hahahaha, it doesn’t matter. The details
are as follows:
ISO the function of each layer of the seven-layer network model and what protocols are in each layer;
understand the Http status code and ask directly. This seems very special; process threads and coroutines ; The similarities and differences of TCP UDP; Three-way handshake and four-way handshake Why not two handshakes or three times? Here I only consider the case of two handshakes. The four handshakes should be similar; several ways of process communication IPC; focus on the application scenarios of message queues ; ; Linux kernel-related knowledge; ? For example, how the exit process signal is transmitted after pressing ctrl+c , this piece is not very familiar; the familiar data structure, the difference between conventional problems; interspersed to ask how to judge whether the linked list has a ring, and also asked C++ about the array How the size change is realized; (The interviewer means let me introduce how C++ realizes the change of the array capacity. This will involve the problem of array out-of-bounds) and also talked about the double expansion mechanism of the vector; also talked about the difference between the stack ; Also talked about the difference;Http 1.0 和http 2.0
503服务器不可访问


四次握手

异步 解耦 削峰
Linux下信号有哪几种形式
数组和链表




栈和队列
I asked where the global variables are stored in the heap or the stack (heap), where
some function parameters return value local variables, etc. are stored (stack). In
practical applications, where the heap will be used and where the stack will be used ; I answered 递归的本质是用栈实现; obviously Didn’t understand what the interviewer meant;
堆的应用场景: Heap sorting (big top heap and small top heap); priority queue (priority_queue) implements top_k problems, etc.; :
栈的应用场景Change the order of elements; bracket matching; reverse Polish expression evaluation; two stack implementations a queue;


Part 3 Code Questions

About 20 minutes
classic topic: seek小于给定数字n的最大数

insert image description here

With reference to several problem-solving ideas, the following solutions are given:

Option 1: 逐位比较(存在一些问题)OK (also the way the interviewer prompted me to write)


//小于n的最大数
//假设给定的数组是 [1,2,4,9] n=2533 则返回的最大数是2499
class Solution
{
    
    
public:
	vector<int>maxNumber(const vector<int>& nums, int n)
	{
    
    
		vector<int>arr, ans;//arr存放给定数字的每一位;ans返回最终的结果
		int minChoose = 10, maxChoose = 0;
		bool exist[10];
		for (int x : nums)
		{
    
    
			exist[x] = true;
			minChoose = min(minChoose, x);//可选择的最小值 给定例子中就是1 
			maxChoose = max(maxChoose, x);//可选择的最大值 给定的例子中就是9
		}
		while (n)
		{
    
    
			arr.push_back(n % 10);//将n的每一位存到数组中
			n /= 10;
		}
		reverse(arr.begin(), arr.end());//答案应尽可能和num位数相同 若位数相同无解 则答案减少一位且每位取最大值 
		for (int i = 0; i < arr.size(); i++)   //arr[i]是当前位 arr[i+1]是后一位
		{
    
                    //待选择的后一位大于等于可选数字的最小值时,当前位才可以选等值;否则只能选小于的值
			int j = (i == arr.size() - 1 || arr[i + 1] >= minChoose ? arr[i] : arr[i] - 1); //j是你所找数字的最高位
			while (j >= 0 && !exist[j])
			{
    
    
				j--;  //j=0 j--之后-1小于0 考虑最高位是1的情况
				if (j < 0)//同位数无解 降低解的位数 ;因为位数是少于给定数字位数的 所以所有的位都取最大值即可;
				{
    
    
					ans.clear();
					for (int k = 0; k < arr.size() - 1; k++)
					{
    
    
						ans.push_back(maxChoose);
					}
					break;
				}
				if (j != arr[i])//当前位取了小于原数同位的值 则解就确定了 后面每一位都可以取最大值;
				{
    
    
					ans.push_back(j);
					while (ans.size() < arr.size())
					{
    
    
						ans.push_back(maxChoose);
					}
					//break;
				}
				ans.push_back(j);//当前位取了等于原数同位的值 直接将当前位放入结果数组中即可
			}
		}
		return ans;
	}
};


int main()
{
    
    
	vector<int>nums = {
    
     1,2,4,9};
	int n = 2533;
	vector<int>res;
	//int ans;
	Solution so;
	//res=so.maxNumber(nums, n);
	res = so.maxNumber(nums,n);
	for (auto it = res.begin(); it != res.end(); it++)
	{
    
    
		cout << *it;
	}
	system("pause");
	return 0;
}

Option 2: 全排列Ideas(回溯) ---测试案例都是成功的


#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<stack>
#include<unordered_map>
#include<algorithm>


//全排列+回溯
//给定一个数组nums和一个指定的数字n,用数组中的数字组成一个整数 使得这个整数是不大于n的最大数;
class Solution
{
    
    
public:
	vector<int>vec;//存储一个结果集合
	string path;//存储单个数字的集合
	int findMax(vector<int>& nums, int n)
	{
    
    
		int res = INT_MIN;
		dfs(nums, n);
		for (auto num : vec)
		{
    
    
			res = max(res, num);//从容器中选一个最大的值
		}
		return res;
	}
	void dfs(vector<int>& nums, int x) 
	{
    
    
		if (path.size() > 0 && stoi(path) >= x)return; //递归终止条件
		for (int i = 0; i < nums.size(); i++) 
		{
    
    
			path += to_string(nums[i]);//用数组中的数字组成数字 用一个字符串来接收每一位数字
			if (stoi(path) < x)  ///将字符串表示的数字转成整型数字进行比较
			{
    
    
				vec.push_back(stoi(path));//将所有小于给定数字的数字存入容器中
			}
			dfs(nums, x);
			path.pop_back();//回溯
		}
	}
};

int main()
{
    
    
	vector<int> nums = {
    
    5,6,7};
	int x = 120;
	Solution so;
	int ans = 0;
	ans = so.findMax(nums, x);
	cout << ans<< endl;
	system("pause");
	return 0;
}






Jianzhi offer 45 Arrange the array into the smallest number

Enter one 非负整数数组, concatenate all the numbers in the array to form a number, and print the smallest one among all the numbers that can be concatenated;

For example:

Input: [10,2]
Output: "102"

insert image description here

//核心思路就是从高位到低位  一直选取最小的数字

class Solution
{
    
    
public:
	string minNumber(vector<int>& nums)
	{
    
    
		vector<string>strs;
		string ans;
		for (int i = 0; i < nums.size(); i++)//遍历数组中的所有元素
		{
    
    
			strs.push_back(to_string(nums[i]));//把整数转换成字符串 接下来实现重排列
		} //每两个表示数字的字符串进行比较 使得两个字符串排成较小的数字
		auto cmp = [](string& s1, string& s2) {
    
    return s1 + s2 < s2 + s1; };//Lamda表达式实现比较 比较方式是 第一个字符串+第二个字符串 小于 第二个字符串+第一个字符串组成的数字;
		sort(strs.begin(), strs.end(), cmp);//前两个参数是排序范围 第三个参数是排序规则
		for (auto str:strs) ans+=str;
		return ans;
	}
};


//用冒泡排序实现 =================================================================================

//用冒泡排序实现
class Solution
{
    
    
public:
	string minNumber(vector<int>& nums)
	{
    
    
		int len = nums.size();
		vector<string>str;
		string res;
		for (int i = 0; i < len; i++)
		{
    
    
			str.push_back(to_string(nums[i]));
		}
		for (int i = 0; i < str.size(); i++) //冒泡排序的逻辑实现
		{
    
    
			for (int j = 0; j < str.size() - i - 1; j++)
			{
    
    
				if (str[j] + str[j + 1] > str[j + 1] + str[j])
				{
    
    
					string tmp = str[j];
					str[j] = str[j + 1];
					str[j + 1] = tmp;
				}
			}
		}
		for (auto c : str)res+=c;
		return res;
	}
};



leetcode402 remove k digits

Given a string representation 非负整数num和一个整数k,, remove the k digits of this number to make the remaining number the smallest; return the smallest number in the form of a string For example:
input
: num = "1432219", k = 3
output: "1219"
explanation: remove three The numbers 4, 3, and 2 form a new smallest number 1219.

Input: num = "10200", k = 1
Output: "200"
Explanation: Remove the first 1 and the remaining number is 200. Note that the output cannot have any leading zeros

insert image description here
小注:
The minimal implementation is by adding elements to the container, 当遇到更小的元素删除容器中的最后一个元素,然后将这个较小的元素放入到容器中;

C++ implementation :

class Solution
{
    
    
public:
	string removek(string num, int k)
	{
    
    
		vector<int>res;
		for (auto x : num)
		{
    
    
			while (!res.empty() && x < res.back() && k)
			{
    
    
				res.pop_back();//将较大的数字移除
				k--;
			}
			res.push_back(x);//较小的数字存入容器中
		}
		for (;k>0; k--)//考虑k最终没减为0的情况 则依次删除容器尾部的元素即可
		{
    
    
			res.pop_back();
		}
		string ans = "";
		bool isZero = true;
		for (auto num : res)
		{
    
    
			if(isZero && num == '0')continue;
			isZero = false;
			ans += num;
		}
		return ans == "" ? 0 : ans;
	}
};

//移掉k位数字使剩下的数字可以组成的数字是最小的 不能改变原给定数组中的数字的顺序
class Solution
{
    
    
public:
	string removeKdigits(string num, int k)
	{
    
    
		vector<int>res;
		for (auto& digit : num)//从前向后遍历 移除遇到的较大的k个数字
		{
    
    
			while (res.size() > 0 && res.back() > digit && k)//当前遍历到的数字小于容器尾部的数字 删除尾部数字 k做自减 最后留下的数字就是一个符合条件的最小数字
			{
    
    
				res.pop_back();
				k--;
			}
			res.push_back(digit);
		}
		for (; k > 0; --k)//考虑k最终没有减为0的情况 则依次删除容器尾部的元素 例如132319 k=3 当得到最后的数字后 k=1 
		{
    
    
			res.pop_back();
		}
		string ans = "";//初始化一个空串用于连接最小数字的字符串
		bool isLeadingZero = true;//考虑存在前导零的情况 例如 104129这个数字去掉三个数之后是存在前导零的 
		for (auto& digit : res)
		{
    
    
		//遇到前导0 不计入 直接进行下一个数字的拼接
			if (isLeadingZero && digit == '0')continue;//continue是跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环
			isLeadingZero = false;//不存在前导零 
			ans += digit;//将vector中的数字连接成字符串
		}
		return ans == "" ? "0" : ans;//最后的字符串是空串直接返回0 不是空串返回这个字符串即可;
	}
};


Guess you like

Origin blog.csdn.net/weixin_48433164/article/details/124749854