虾皮3-14第二批笔试部分题解
话说有点太简单了,选择题到还可以。 就是这破编辑器老是编译错误,我直接用数组去写了~ 1.连标反转?? 我只要用连标,他就编译错误,给我恶心坏了。直接数组 /** * struct ListNode { * int val; * struct ListNode *next; * ListNode(int x) : val(x), next(nullptr) {} * }; */ #include<bits/stdc++.h> using namespace std; const int maxx = 1e5+7; class Solution { public: /** * Note: 类名、方法名、参数名已经指定,请勿修改 * * * 合并两个降序链表为一个升序链表 * @param l1 ListNode类 降序链表 * @param l2 ListNode类 降序链表 * @return ListNode类 */ ListNode* MergeList(ListNode* l1, ListNode* l2) { // write code here int a[maxx], b[maxx]; int cnt1 = 0, cnt2 = 0; ListNode *ans = new ListNode(-1); ListNode *ans2 = ans; while(l1 != nullptr){ a[++cnt1] = l1->val; l1 = l1->next; } while(l2 != nullptr){ b[++cnt2] = l2->val; l2 = l2->next; } for(int i =1;i<=cnt1;i++){ printf("%d\n",a[i]); } while(cnt1 != 0 && cnt2 != 0){ if(a[cnt1] <= b[cnt2]){ ListNode *node = new ListNode(a[cnt1]); ans->next = node; ans = node; cnt1--; } else { ListNode *node = new ListNode(b[cnt2]); ans->next = node; ans = node; cnt2--; } } while(cnt1>=1){ ListNode *node = new ListNode(a[cnt1]); ans->next = node; ans = node; cnt1--; } while(cnt2>=1){ ListNode *node = new ListNode(b[cnt2]); ans->next = node; ans = node; cnt2--; } return ans2->next; } }; 五进制 转十进制。 没啥可说的 然后还是不知道为啥有个编译错误~ #include<bits/stdc++.h> using namespace std; const int maxx = 1e5+7; #define ll long long class Solution { public: ll quickPow(ll a,ll b){ ll ans = 1; while(b){ if(b & 1) ans = ans*a; a = a*a; b >>= 1; } return ans; } long long Convert(vector<int>& nums) { // write code here ll ans = 0; ll n = nums.size(); for(ll i = 0;i < n;i++){ ll x = quickPow(5,n - i -1); ans += x * nums[i]; } return ans; } }; 排个序,直接n方暴力就过了,连二分都不用二分的 离谱的笔试题 #include<bits/stdc++.h> using namespace std; const int maxx = 1e5+7; #define ll long long struct stu{ int cost,val; }A[maxx]; int cmp(stu a, stu b){ return a.val > b.val; } class Solution { public: int findMaximizedCapital(int k, int w, vector<int>& profits, vector<int>& capital) { // write code here int cnt = 0; int n = profits.size(); int vis[n + 1]; memset(vis, 0, sizeof(vis)); for(int i = 0; i < n; i++){ A[++cnt].val = profits[i]; A[cnt].cost = capital[i]; } sort(A + 1, A + 1 + cnt, cmp); int ans = 0; int nowVal = w; for(int pos = 1; pos <= k; pos++){ for(int i = 1; i <= n; i++){ if(!vis[i] && nowVal >= A[i].cost){ vis[i] = 1; nowVal += A[i].val; break; } } } return nowVal; } };
#虾皮笔试##Shopee##笔经#