腾讯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;
}

#我的实习求职记录#
全部评论
老哥能讲讲第四题的思路吗,菜菜人实在看不懂了
1 回复 分享
发布于 2023-03-28 22:50 四川
第5题,暴力水过去了
1 回复 分享
发布于 2023-03-27 00:14 广东
第三题是不是出错了呀?我记得他的条件是“i < j && B[i] > B[j]时 , 要求 C[i] > C[j]",但没约束i < j && B[i] <= B[j] 的情况,此时依然可以C[i] > C[j] , 并不是任意的i,j,大的B必须对应大的C?
1 回复 分享
发布于 2023-03-26 22:30 北京
第三题和你写的一模一样为啥我就过57%
1 回复 分享
发布于 2023-03-26 22:08 山东
请问是做过测评了才会收到笔试吗
点赞 回复 分享
发布于 2023-04-04 04:22 美国
第二题是啥来着
点赞 回复 分享
发布于 2023-03-27 20:14 广东
楼主 问个问题 这个第二题 为什么我输入这里写成这样: int num; cin >> num; cin.ignore(); vector<string> strs (num + 1); int i = 1; while (num--) { string s; getline(cin, s); strs[i++] = s; } 其他都和你一样,程序就没有输出呢,改成你那样就又可以了。</string>
点赞 回复 分享
发布于 2023-03-27 17:06 天津
第四题的思路还能再说一下嘛,有点不太懂
点赞 回复 分享
发布于 2023-03-27 02:21 美国
来膜拜一下
点赞 回复 分享
发布于 2023-03-26 22:36 江苏
第三题makepair先B后A的vector,然后排序就行了
点赞 回复 分享
发布于 2023-03-26 22:24 江苏
第五题是个莫比乌斯板子
点赞 回复 分享
发布于 2023-03-26 22:19 陕西
牛的
点赞 回复 分享
发布于 2023-03-26 22:13 江苏
太牛了,,我都不会写
点赞 回复 分享
发布于 2023-03-26 22:07 湖北
点赞 回复 分享
发布于 2023-03-26 22:06 江苏

相关推荐

个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
评论
17
44
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务