#腾讯笔试10.26 ak代码#

#腾讯笔试#

五题难度分析,四道medium,一道hard

第一题

    ListNode* xorList(ListNode* a, ListNode* b) {
        stack<ListNode*>st;
        ListNode* ca=a;
        ListNode* cb=b;
        while(ca!=nullptr){
            st.push(ca);
            ca=ca->next;
        }
        ListNode* head=new ListNode(0);
        while(!st.empty()){
            ListNode* t=new ListNode(st.top()->val^cb->val);
            t->next=head->next;
            head->next=t;
            cb=cb->next;
            st.pop();
            if(cb==nullptr){
                while(!st.empty()){
                    ListNode* t=st.top();
                    t->next=head->next;
                    head->next=t;
                    st.pop();
                }
            }
        }
        if(cb!=nullptr){
            while (cb!=nullptr) {
                ListNode* t=new ListNode(cb->val);
                t->next=head->next;
                head->next=t;
                cb=cb->next;
            }
        }
        ListNode* h=head->next;
        while(h!=nullptr&&h->val==0){
            h=h->next;
        }
        if(h==nullptr) h=new ListNode(0);
        return h;
    }

第二题

#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<stack>
#include<algorithm>
#include<bitset>
#include<queue>
#include<iomanip>
#include<cstring>
using namespace std;
typedef pair<int,int> pi;

int count(int n){
    int res=0;
    while(n!=0){
        n=n&(n-1);
        res++;
    }
    return res;
}
int main(){
    int n,k;
    cin>>n>>k;
    vector<int>v(n);
    priority_queue<pi>q;
    long long res=0;
    for(int i=0;i<n;i++) {
        cin>>v[i];
        res+=(long long)v[i];
        q.push(pi(v[i]-count(v[i]),i));
    }
    while(k--){
        auto [a,i]=q.top();
        q.pop();
        int x=v[i]-a;
        res=res-a+(long long)x;
        q.push(pi(x,i));
    }
    cout<<res<<endl;
}

第三题

#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<stack>
#include<algorithm>
#include<bitset>
#include<queue>
#include<iomanip>
#include<cstring>
using namespace std;
int main(){
    int n;
    cin>>n;
    deque<int>q;
    int a;
    for(int i=0;i<n;i++){
        cin>>a;
        q.push_back(a);
    }
    vector<int>v(n);
    for(int i=1;i<=n;i++){
        if(i&1==1){
            v[i-1]=max(q.front(),q.back());
            if(q.front()>=q.back()){
                q.pop_front();
            }else q.pop_back();
        }else{
            v[i-1]=min(q.front(),q.back());
            if(q.front()<=q.back()){
                q.pop_front();
            }else q.pop_back();
        }
    }
    for(auto i:v) cout<<i<<" ";
    cout<<endl;
}

第四题

#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<stack>
#include<algorithm>
#include<bitset>
#include<queue>
#include<iomanip>
#include<cstring>
using namespace std;

long long f(long long n,long long y){
    if(n==1) return cal(y);
    long long x=1;
    while(x<n) x=x<<1;
    if(x==n) return x/2;
    return x/4+f(n-x/2,y);
}
bool cal(long long n){
    if(n==1) return 1;
    long long x=1;
    while(x<n) x=x<<1;
    return !cal(n-x/2);
}
int main(){
    long long l,r;
    cin>>l>>r;
    long long x=f(r,r)-f(l-1,l-1);
    cout<<x<<endl;
}

第五题

第五题多说一嘴哈,其实这个题可以分解为,将一个数组分为两部分,使得两部分的和的差值最小,保存怎么分的就行

#include<iostream>
#include<vector>
#include<cmath>
#include<map>
#include<unordered_map>
#include<set>
#include<string>
#include<stack>
#include<algorithm>
#include<bitset>
#include<queue>
#include<iomanip>
#include<cstring>
using namespace std;

vector<int>st;
int mx=0;
unordered_map<long,int>p;
int jud(vector<int>&a,int x,int i,vector<int>&v,int cur){
    if(i==a.size()){
        if(cur>mx){
            st=v;
            mx=cur;
        }
        return cur;
    }
    long ss=cur*101+i;
    int res=0;
    if(p.count(ss)) return p[ss];
    if(a[i]+cur>x){
        res=jud(a,x,i+1,v,cur);
    }else{
        v[i]=1;
        int l=jud(a,x,i+1,v,cur+a[i]);
        v[i]=0;
        int r=jud(a,x,i+1,v,cur);
        res=max(l,r);
    }
    p[ss]=res;
    return res;
}

int main(){
    int n,sum=0;
    cin>>n;
    vector<int>a(n);
    st.resize(n,0);
    for(int i=0;i<n;i++) {
        cin>>a[i];
        sum+=a[i];
    }
    int x=0,y=0;
    vector<int>v(n,0);
    x=jud(a,sum/2,0,v,0);
    y=x-sum;
    cout<<x<<" "<<y<<endl;
    for(int i=0;i<st.size();i++){
        if(st[i]==1){
            cout<<"Y";
        }else cout<<"X";
    }
    cout<<endl;
}

全部评论
大神就是牛
点赞 回复 分享
发布于 2022-10-18 14:05 河南

相关推荐

02-14 07:38
已编辑
门头沟学院 Java
2.4&nbsp;一面2.6&nbsp;二面2.9&nbsp;三面(hr面)2.13&nbsp;oc1.15号收到面试电话那会就开始准备,因为一开始没底所以选择推迟一段时间面试,之后开始准备八股,准备实习可能会问的东西,这期间hot100过了有六七遍,真的是做吐了快,八股也是背了忘,忘了背,面经也看了很多,虽然最后用上的只有几道题,可是谁知道会问什么呢自从大二上开始学java以来,一路走来真的太痛了,一开始做外卖,点评,学微服务,大二下五六月时,开始投简历,哎,投了一千份了无音讯,开始怀疑自己(虽然能力确实很一般),后来去到一家小小厂,但是并不能学到什么东西,而且很多东西都很不规范,没待多久便离开,大二暑假基本上摆烂很怀疑自己,大三上因为某些原因开始继续学,期间也受到一俩个中小厂的offer,不过学校不知道为啥又不允许中小厂实习只允许大厂加上待遇不太好所以也没去,感觉自己后端能力很一般,于是便打算转战测开,学习了一些比较简单的测试理论(没有很深入的学),然后十二月又开始继续投,java和测开都投,不过好像并没有几个面试,有点打击不过并没有放弃心里还是想争一口气,一月初因为学校事比较多加上考试便有几天没有继续投,10号放假后便继续,想着放假应该很多人辞职可能机会大一点,直到接到字节的面试,心里挺激动的,总算有大厂面试了,虽然很开心,但同时压力也很大,心里真的很想很想很想进,一面前几天晚上都睡不好觉,基本上都是二三点睡六七点醒了,好在幸运终于眷顾我一次了(可能是之前太痛了),一面三十几分钟结束,问的都不太难,而且面试官人挺好但是有些问题问的很刁钻问到了测试的一些思想并不是理论,我不太了解这方面,但是也会给我讲一讲他的理解,但是面完很伤心觉得自己要挂了。但是幸运的是一面过了(感谢面试官),两天后二面,问的同样不算难,手撕也比较简单,但也有一两个没答出来,面试官人很好并没有追问,因为是周五进行的二面,没有立即出结果,等到周一才通知到过了,很煎熬的两天,根本睡不好,好在下周一终于通知二面过了(感谢面试官),然后约第二天三面,听别的字节同学说hr面基本上是谈薪资了,但是我的并不是,hr还问了业务相关的问题,不过问的比较浅,hr还问我好像比较紧张,而且hr明确说了还要比较一下,我说我有几家的面试都拒了就在等字节的面试(当然紧张,紧张到爆了要),三面完后就开始等结果,这几天干啥都没什么劲,等的好煎熬,终于13号下午接到了电话通知oc了,正式邮件也同时发了,接到以后真的不敢信,很激动但更重要的是可以松一口气了,可以安心的休息一下了终于可以带着个好消息过年了,找实习也可以稍微告一段落了,虽然本人很菜,但是感谢字节收留,成为忠诚的节孝子了因为问的比较简单,面经就挑几个记得的写一下一面:1.实习项目的难点说一下2.针对抖音评论设计一下测试用例3.手撕:合并两个有序数组二面:1.为什么转测开2.线程进程区别,什么场景适合用哪个3.发送一个朋友圈,从发出到别人看到,从数据流转的角度说一下会经历哪些过程4.针对抖音刷到广告视频设计测试用例5.手撕:无重复字符的最长字串
查看8道真题和解析
点赞 评论 收藏
分享
头像
01-29 18:11
海南大学 Java
奔跑的suechil...:单从项目看这个简历不怕被问穿吗 带微服务的项目需要相当多的项目理解和经验诶
点赞 评论 收藏
分享
评论
2
2
分享

创作者周榜

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