携程:笔试题(20190904)

链表排序

设置标志位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;
}

表达式解析

设置栈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;
}

任务调度

设置上界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;
}

(最近更新:2019年09月06日)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值