腾讯音乐笔试
#腾讯音乐秋招# #腾讯音乐求职进展汇总# 求大佬们看看第二题和第三题我写的有什么问题,第二题50, 第三题20
2:
ListNode* mergeList(ListNode* a, ListNode* b) {
ListNode* dummy = new ListNode(-1);
ListNode* cura = a, *curb = b, *p = dummy;
while(cura && cura->val == curb->val) {
p->next = cura;
cura = cura->next;
curb = curb->next;
p = p->next;
}
ListNode* fast = cura, *slow = curb;
int n = 0, m = 0;
while(cura) {
++n;
cura = cura->next;
}
while(curb) {
++m;
curb = curb->next;
}
int len = n - m;
if(n < m) {
ListNode* tmp = fast;
fast = slow;
slow = tmp;
len = m - n;
}
while(len-- &amp;&amp; fast) {
fast = fast->next;
}
while(fast &amp;&amp; fast->val != slow->val) {
fast = fast->next;
slow = slow->next;
}
p->next = fast;
return dummy->next;
}
3:
int matrixCount(vector&amp; a, int val) {
int n = a.size();
vector> r(n + 1, vector(n + 1, 0));
vector> e(n + 1, vector(n + 1, 0));
vector> d(n + 1, vector(n + 1, 0));
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(a[i - 1][j - 1] == 'r') {
r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1] + 1;
e[i][j] = e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1];
d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
}
else if(a[i - 1][j - 1] == 'e') {
r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1];
e[i][j] = e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1] + 1;
d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
} else {
r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1];
e[i][j] = e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1];
d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + 1;
}
}
}
long long res = 0;
for(int k = 1; k <= n; k++) {
for(int i = 1; i + k - 1 <= n; ++i) {
for(int j = 1; j + k - 1<= n; ++j) {
int x = i + k - 1, y = j + k - 1;
long long numa = 0, nume = 0, numd = 0;
numa = r[x][y] - r[x - k][y] - r[x][y - k] + r[x-k][y-k];
nume = e[x][y] - e[x - k][y] - e[x][y - k] + e[x-k][y-k];
numd = d[x][y] - d[x - k][y] - d[x][y - k] + d[x-k][y-k];
long long sum = min(numa, min(numd, nume));
if(sum >= val) ++res;
}
}
}
return res;
}
2:
ListNode* mergeList(ListNode* a, ListNode* b) {
ListNode* dummy = new ListNode(-1);
ListNode* cura = a, *curb = b, *p = dummy;
while(cura &amp;&amp; cura->val == curb->val) {
p->next = cura;
cura = cura->next;
curb = curb->next;
p = p->next;
}
ListNode* fast = cura, *slow = curb;
int n = 0, m = 0;
while(cura) {
++n;
cura = cura->next;
}
while(curb) {
++m;
curb = curb->next;
}
int len = n - m;
if(n < m) {
ListNode* tmp = fast;
fast = slow;
slow = tmp;
len = m - n;
}
while(len-- &amp;&amp; fast) {
fast = fast->next;
}
while(fast &amp;&amp; fast->val != slow->val) {
fast = fast->next;
slow = slow->next;
}
p->next = fast;
return dummy->next;
}
3:
int matrixCount(vector
int n = a.size();
vector
vector
vector
for(int i = 1; i <= n; ++i) {
for(int j = 1; j <= n; ++j) {
if(a[i - 1][j - 1] == 'r') {
r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1] + 1;
e[i][j] = e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1];
d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
}
else if(a[i - 1][j - 1] == 'e') {
r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1];
e[i][j] = e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1] + 1;
d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1];
} else {
r[i][j] = r[i - 1][j] + r[i][j - 1] - r[i - 1][j - 1];
e[i][j] = e[i - 1][j] + e[i][j - 1] - e[i - 1][j - 1];
d[i][j] = d[i - 1][j] + d[i][j - 1] - d[i - 1][j - 1] + 1;
}
}
}
long long res = 0;
for(int k = 1; k <= n; k++) {
for(int i = 1; i + k - 1 <= n; ++i) {
for(int j = 1; j + k - 1<= n; ++j) {
int x = i + k - 1, y = j + k - 1;
long long numa = 0, nume = 0, numd = 0;
numa = r[x][y] - r[x - k][y] - r[x][y - k] + r[x-k][y-k];
nume = e[x][y] - e[x - k][y] - e[x][y - k] + e[x-k][y-k];
numd = d[x][y] - d[x - k][y] - d[x][y - k] + d[x-k][y-k];
long long sum = min(numa, min(numd, nume));
if(sum >= val) ++res;
}
}
}
return res;
}
全部评论
t2是题目***,那个链表实际不相交,题目的想等是指listnode.val想等,所以你放两个数组,然后整前缀后缀,最后后缀反转,串起来就ac了
t3我也就过了90,那个第一层循环可以从sqrt(3*val)开始枚举
楼主你写的t2比较的是val,为什么也不行呢?感觉没什么问题啊
不小于,是不是要改成大于if(sum > val) ++res;
第三题加个二分优化
相关推荐
点赞 评论 收藏
分享