腾讯3.26笔试(4/5)
T1. 链表
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
* @param head ListNode类
* @return ListNode类
*/
ListNode* reorderList(ListNode* head) {
// write code here
vector<int> res;
while(head){
res.push_back(head->val);
head = head->next;
}
int n = (int)res.size();
vector<vector<int>> group;
for(int i = 0; i + 1 < n; i += 2){
group.push_back({res[i], res[i + 1]});
}
if(n & 1) group.push_back({res[n - 1]});
res.clear();
int siz = (int)group.size();
for(int i = 0; i + 1 < siz; i+= 2){
swap(group[i], group[i + 1]);
}
for(auto vec: group){
for(int v: vec){
res.push_back(v);
}
}
ListNode *ans = new ListNode(res[0]);
ans->next = nullptr;
ListNode *curr = ans;
for(int i = 1; i < n; i++){
ListNode *temp = new ListNode(res[i]);
temp->next = nullptr;
curr->next = temp;
curr = temp;
}
return ans;
}
};
T2. 爆搜
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<string> s(n + 1);
for(int i = 1; i <= n; i++){
cin >> s[i];
}
vector<int> vis(26, 0);
unordered_set<string> st;
string now;
function<void(int, int)> dfs = [&](int row, int col){
if(vis[s[row][col] - 'a']){
return ;
}
vis[s[row][col] - 'a'] = 1;
now += s[row][col];
if(row == n){
st.insert(now);
now.pop_back();
vis[s[row][col] - 'a'] = 0;
return ;
}
for(int i = 0; i < (int)s[row + 1].size(); i++){
dfs(row + 1, i);
}
now.pop_back();
vis[s[row][col] - 'a'] = 0;
};
dfs(0, 0);
cout << (int)st.size() << endl;
return 0;
}
T3. 构造+贪心
先排序,然后记录0 1 2的位置,然后从小到大先放0再放1再放2即可
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin >> n;
vector<long long> p(n);
vector<pair<long long, int>> a(n);
for(int i = 0; i < n; i++){
cin >> a[i].first;
}
for(int i = 0; i < n; i++){
cin >> a[i].second;
}
sort(a.begin(), a.end());
vector<int> idx[3];
for(int i = 0; i < n; i++){
idx[a[i].second].push_back(i);
}
vector<int> pos(n);
int cnt = 0;
for(int i = 0; i < 3; i++){
for(int j: idx[i]){
pos[j] = ++cnt;
}
}
long long ans = 0;
for(int i = 0; i < n; i++){
ans += abs(pos[i] - a[i].first);
}
cout << ans << endl;
}
T4. 乱搞
难点在于1怎么处理,我的方法是压缩1,然后再算
#include <bits/stdc++.h>
using namespace std;
long long gao(int n){
long long ans = 0;
for(int i = n; i >= 0; i-= 2){
ans += i;
}
return ans;
}
int main(){
int n;
cin >> n;
vector<int> a(n + 1);
for(int i = 1; i <= n; i++){
cin >> a[i];
}
vector<pair<int, int>> arr;
int tot = 0;
for(int i = 1; i <= n; i++){
if(a[i] == 1){
++tot;
}else{
if(tot) arr.push_back({1, tot});
tot = 0;
arr.push_back({a[i], -1});
}
}
if(tot) arr.push_back({1, tot});
long long ans = 0;
int siz = (int)arr.size();
for(int st = 0; st < siz; st++){
long long mul = 1;
long long xor_sum = 0;
long long tot_ones = 0;
for(int ed = st; ed < siz; ed++){
if(arr[ed].first == 1){
tot_ones += arr[ed].second;
}
mul *= arr[ed].first;
if(arr[ed].first != 1) xor_sum ^= arr[ed].first;
if(mul > 1e9){
break;
}
if(mul == 1) continue;//暂时不考虑单个块全是1的情况
tot_ones %= 2;
if(arr[st].first != 1 && arr[ed].first != 1){
if((xor_sum ^ tot_ones) == mul){
++ans;
}
}
if(arr[st].first == 1 && arr[ed].first != 1){
int ones = arr[st].second;
if((xor_sum ^ 1) == mul){
ans += (ones + 1) / 2;
}
if(xor_sum == mul){
ans += ones / 2;
}
}
if(arr[st].first != 1 && arr[ed].first == 1){
int ones = arr[ed].second;
if((xor_sum ^ 1) == mul){
ans += (ones + 1) / 2;
}
if(xor_sum == mul){
ans += ones / 2;
}
}
if(arr[st].first == 1 && arr[ed].first == 1){
long long left_ones = arr[st].second;
long long right_ones = arr[ed].second;
if((xor_sum ^ 1) == mul){
ans += ((left_ones + 1) / 2) * (right_ones / 2);
ans += (left_ones / 2) * ((right_ones + 1) / 2);
}
if(xor_sum == mul){
ans += (left_ones / 2) * (right_ones / 2);
ans += ((left_ones + 1) / 2) * ((right_ones + 1) / 2);
}
}
}
}
for(int i = 0; i < siz; i++){
if(arr[i].first == 1){
ans += gao(arr[i].second);
}
}
cout << ans << endl;
}
#我的实习求职记录#
查看10道真题和解析