相信有很多的java學(xué)子都是想找到理想工作,大家對(duì)攜程并不陌生,很多小伙伴都想加入其中,java筆試題是應(yīng)聘攜程時(shí)候必不可少的部分,為了幫助大家,長(zhǎng)沙達(dá)內(nèi)教育java培訓(xùn)機(jī)構(gòu)的小編準(zhǔn)備了攜程java應(yīng)聘筆試題解析,大家可以作為參考。
1、鏈表排序
設(shè)置標(biāo)志位flag來(lái)區(qū)分低于m值的結(jié)點(diǎn)是否已經(jīng)放在鏈表首部,同時(shí)設(shè)置pre來(lái)存放前一個(gè)結(jié)點(diǎn),方便刪除當(dāng)前結(jié)點(diǎn)。對(duì)于第一次在隊(duì)首插入比m值小的結(jié)點(diǎn),有兩種情況:是該鏈表的第一個(gè)結(jié)點(diǎn)、不是該鏈表的第一個(gè)結(jié)點(diǎn)。而對(duì)于不是第一次在隊(duì)首插入結(jié)點(diǎn),則設(shè)置last來(lái)存放左區(qū)域的最后一個(gè)結(jié)點(diǎn)。
#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、表達(dá)式解析
設(shè)置棧container來(lái)存儲(chǔ)已遍歷的內(nèi)容,遇到)時(shí)候直到遍歷到(或者container為空,將遍歷的內(nèi)容存儲(chǔ)在臨時(shí)字符數(shù)組current中,再依次反向壓入棧container中。在輸出的時(shí)候,要注意棧container中的字符已經(jīng)是符合要求的,所以不能依次出棧輸出。
res += val;:字符串累加元素reverse(res.begin(), res.end()):反轉(zhuǎn)字符串#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、任務(wù)調(diào)度
設(shè)置上界max(array)和下界sum(array),采用二分查找進(jìn)行搜索。這里根據(jù)的是根據(jù)這個(gè)值mid可以分塊的多少count與目標(biāo)塊block的大小關(guān)系來(lái)調(diào)整下一次的上下界。
#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;}
以上就是長(zhǎng)沙達(dá)內(nèi)教育java培訓(xùn)機(jī)構(gòu)的小編針對(duì)“攜程java應(yīng)聘筆試題解析”的內(nèi)容進(jìn)行的回答,希望對(duì)大家有所幫助,如有疑問(wèn),請(qǐng)?jiān)诰€咨詢,有專業(yè)老師隨時(shí)為你服務(wù)。
Java筆試題