专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 携程java应聘笔试题解析

携程java应聘笔试题解析

更新时间:2020-06-30 07:36:32 来源:动力节点 浏览1810次

相信有很多的java学子都是想找到理想工作,大家对携程并不陌生,很多小伙伴都想加入其中,java笔试题是应聘携程时候必不可少的部分,为了帮助大家,动力节点java培训机构的小编准备了携程java应聘笔试题解析,大家可以作为参考。

1、链表排序

设置标志位flag来区分低于m值的结点是否已经放在链表首部,同时设置pre来存放前一个结点,方便删除当前结点。对于第一次在队首插入比m值小的结点,有两种情况:是该链表的第一个结点、不是该链表的第一个结点。而对于不是第一次在队首插入结点,则设置last来存放左区域的最后一个结点。

#include <iostream>
#include <vector>
#include <numeric>
#include <limits>
using namespace std;
class ListNode {
public:
    int val;
    ListNode* next;
    ListNode(int val){
        this->val=val;
        this->next=NULL;
    };
};
/*请完成下面这个函数,实现题目要求的功能
******************************开始写代码******************************/
ListNode* partition(ListNode* head,int m) {
	bool flag = true;
	ListNode* p = head;
	ListNode* pre = head;
	ListNode* last = NULL;
	while(p)
	{
		if(p->val <= m)
		{
			if(flag)
			{
				if(pre == p)
				{
					p = p->next;
				}
				else
				{
					pre->next = p->next;
					p->next = head;
					head = p;
					p = pre->next;
				}
				last = head;
				flag = false;
			}
			else
			{
				if(last->next == p)
				{
					last = p;
					pre = p;
					p = p->next;
				}
				else
				{
					pre->next = p->next;
					p->next = last->next;
					last->next = p;
					last = p;
					p = pre->next;
				}
			}
		}
		else
		{
			pre = p;
			p = p->next;
		}
	}
	return head;
}
/******************************结束写代码******************************/

int main() {
    ListNode* head=NULL;
    ListNode* node=NULL;
    int m;
    cin>>m;
    int v;
    while(cin>>v){
        if(head==NULL){
            node=new ListNode(v);
            head=node;
        }else{
            node->next=new ListNode(v);
            node=node->next;
        }
    }
    head = partition(head, m);
    if(head!=NULL){
        cout<<head->val;
        node=head->next;
        delete head;
        head=node;
        while(head!=NULL){
            cout<<","<<head->val;
            node=head->next;
            delete head;
            head=node;
        }
    }
    cout<<endl;
    return 0;
}

2、表达式解析

设置栈container来存储已遍历的内容,遇到)时候直到遍历到(或者container为空,将遍历的内容存储在临时字符数组current中,再依次反向压入栈container中。在输出的时候,要注意栈container中的字符已经是符合要求的,所以不能依次出栈输出。

res += val;:字符串累加元素
reverse(res.begin(), res.end()):反转字符串
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <iterator>
using namespace std;

/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ 
******************************开始写代码******************************/
string resolve(string expr) {
	stack<char> container;
	for(int ei = 0; ei < int(expr.size()); ei++)
	{
		char eu = expr[ei];
		if(eu == '(')
			container.push('(');
		if(eu >= 'a' && eu <= 'z')
			container.push(eu);
		if(eu == ')')
		{
			vector<char> current;
			while(!container.empty() && container.top() != '(')
			{
				char cur = container.top();
				current.push_back(cur);
				container.pop();
			}
			if(container.empty())
				return "";
			container.pop();
			vector<char>::iterator it;
			for(it = current.begin(); it != current.end(); it++)
			{
				container.push(*it);
			}
		}
	}

	string res;
	while(!container.empty())
	{
		char val = container.top();
		if(val != '(')
		{
			res += val;
			container.pop();
		}
	}
	reverse(res.begin(), res.end());
	return res;
}
/******************************结束写代码******************************/
int main() {
    string res;
    string _expr;
    getline(cin, _expr);
    res = resolve(_expr);
    cout << res << endl;
    return 0;
}

3、任务调度

设置上界max(array)和下界sum(array),采用二分查找进行搜索。这里根据的是根据这个值mid可以分块的多少count与目标块block的大小关系来调整下一次的上下界。

#include <iostream>
#include <vector>
#include <numeric>
#include <limits>
using namespace std;
/*请完成下面这个函数,实现题目要求的功能
当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ 
******************************开始写代码******************************/
int cal_value(vector<int>seq, int num, int block, int low, int high)
{
	int mid;
	while(low <= high)
	{
		mid = (low+high) >> 1;
		int count =0;
		bool flag = true;
		int index = 0;
		while(index<num)
		{
			int total=0;
			while(index<num && total+seq[index] <= mid)
			{
				total += seq[index];
				index += 1;
			}
			count += 1;
			if(count>block)
			{
				flag = false;
				break;
			}
		}
		if(flag)
			high = mid-1;
		else
			low = mid+1;
	}
	return mid;
}

int schedule(int m,vector < int > array) {
	int n = array.size();
	int left =0, right =0;
	for(int i= 0; i<n; i++)
	{
		if(left < array[i])
			left = array[i];
		right += array[i];
	}
	return cal_value(array, n, m, left, right);
}
/******************************结束写代码******************************/
int main() {
    int res;

    int m, n;
	cin >> m >> n;
	vector<int> array(n, 0);
	for(int i=0; i<n; i++)
	{
		cin >> array[i];
	}
    res = schedule(m,array);
    cout << res << endl;
    
    return 0;
}

以上就是动力节点java培训机构的小编针对“携程java应聘笔试题解析”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>