更新時間:2020-06-30 07:36:32 來源:動力節點 瀏覽2262次
相信有很多的java學子都是想找到理想工作,大家對攜程并不陌生,很多小伙伴都想加入其中,java筆試題是應聘攜程時候必不可少的部分,為了幫助大家,動力節點java培訓機構的小編準備了攜程java應聘筆試題解析,大家可以作為參考。
設置標志位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;
}
以上就是動力節點java培訓機構的小編針對“攜程java應聘筆試題解析”的內容進行的回答,希望對大家有所幫助,如有疑問,請在線咨詢,有專業老師隨時為你服務。
0基礎 0學費 15天面授
有基礎 直達就業
業余時間 高薪轉行
工作1~3年,加薪神器
工作3~5年,晉升架構
提交申請后,顧問老師會電話與您溝通安排學習