腾讯笔试简单题解

第一题,通过找到每个点的连通是否都红,直接与运算

n,m = (int(i) for i in input().split(" "))
dic = {}
for i in range(m):
    f,t,c = (i for i in input().split(" "))
    f = int(f)
    t = int(t)
    if dic.get(f) is None: dic[f] = c=="R"
    else: dic[f] = dic[f] and c=="R"
    if dic.get(t) is None: dic[t] = c=="R"
    else: dic[t] = dic[t] and c=="R"


res = 0
for i in range(1,n+1):
    if dic.get(i) is None or dic.get(i):
        res+=1
print(res)

第二题,找有几个降序节点,超过两个返回false,不知道为什么只过了90???

public boolean[] canSorted (ListNode[] lists) {
	boolean[] res = new boolean[lists.length];
	Arrays.fill(res,true);
	for(int i= 0;i<lists.length;i++){
	  ListNode n = lists[i];
	  int pre=0;
	  while(n.next!=null){
		if(n.next.val<n.val){
		  pre++;
		  if(pre>=2){
			res[i] = false;
			break;
		  }
		}
		n = n.next;
	  }
	}
	return res;
}

第三题:并查集变形,记录顶级父节点拥有子节点数

class Main {
    public static void main(String[] args) {
        class Tu{
            int[][] fa;
            int cnt;
            public Tu(int n){
                this.fa = new int[n+1][2];
                for(int i=1;i<=n;i++){
                    this.fa[i][0] = i;
                    this.fa[i][1] = 1;
                }
                this.cnt = n;
            }

            public boolean isc(int f,int t){
                return this.find(f)==this.find(t);
            }

            public int find(int f){
                if(this.fa[f][0]==f){
                    return f;
                }
                int ff = find(this.fa[f][0]);
                this.fa[f][0] = ff;
                return ff;
            }
            public void ins(int f,int t){
                int ff = this.find(f);
                this.fa[t][0] = ff;
                this.fa[ff][1]++;
                this.fa[t][1] = 0;
                this.cnt--;
            }
        }

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m= in.nextInt();
        Tu tu = new Tu(n);
        for(int i=0;i<m;i++){
            int f= in.nextInt(),t= in.nextInt();
            if(!tu.isc(f,t)){
                tu.ins(f,t);
            }
        }
        int res = 1;
        if(tu.cnt>2){
            System.out.println(0);
        }else{
            for(int[] t: tu.fa){
                res*=t[1]>0?t[1]:1;
            }
            System.out.println(res);
        }
    }
}

第四题:爆搜直接超时,就不贴了

第五题:不带回溯的爆搜

class Main {
    public static char[] tt = "tencent".toCharArray();
    public static int res;
    public static int[][] mv = new int[][]{{1,0},{-1,0},{0,1},{0,-1}};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        res = 0;
        int n = in.nextInt();
        int m = in.nextInt();
        char[][] ch = new char[n][m];
        in.nextLine();
        List<int[]> lst = new ArrayList<>();
        for(int i=0;i<n;i++){
            ch[i] = in.nextLine().toCharArray();
            for(int j = 0;j<m;j++){
                if(ch[i][j]=='t'){
                    lst.add(new int[]{i,j});
                }
            }
        }
        for(int[] start: lst){
            dfs(ch,start[0],start[1],0);
        }
        System.out.println(res);
    }

    public static void dfs(char[][] ch,int x,int y,int ind){
        if(ind==6){
            if(tt[ind]==ch[x][y]){
                res++;
            }
            return;
        }
        if(ch[x][y]!=tt[ind]){
            return;
        }
        for(int[] m:mv){
            int xx = x+m[0],yy = y+m[1];
            if(xx>=0 && xx<ch.length && yy>=0
                    && yy<ch[0].length){
                dfs(ch,xx,yy,ind+1);
            }
        }
    }
}

#腾讯笔试##腾讯##笔试#
全部评论
有佬解答一下第二题吗
2
送花
回复
分享
发布于 03-31 22:06 上海
大佬太强了
2
送花
回复
分享
发布于 04-01 17:12 广东
网易互娱
校招火热招聘中
官网直投
有第4题思路吗
点赞
送花
回复
分享
发布于 03-31 22:06 新加坡
为什么你们第五题暴搜不超时啊😩
点赞
送花
回复
分享
发布于 03-31 22:08 辽宁
第一题,我是用一个数组来存储边,如果输入R,则边值为1,如果是W,则边值为2,否则其他都是默认值0,然后取遍历数组,如果该点连接的所有边值中有2的话,则其不是好点,否则则该点是好点,这个思路有什么错误吗
点赞
送花
回复
分享
发布于 03-31 22:19 浙江
大佬,第三题答案是两个集合的节点数相乘吗,我并查集怎么才6%,破防了😭
点赞
送花
回复
分享
发布于 03-31 22:27 河北
用java写的,第五题交上去每次拿的分数都不一样,最高就是一直卡在96.67%。。。。
点赞
送花
回复
分享
发布于 03-31 23:14 江苏
大佬第五题是100%吗
点赞
送花
回复
分享
发布于 03-31 23:29 四川
我第二题使用list做的,前面一个一个删,加到后面,加一次就判断是不是升序的,直到第一个等于之前记录的最后一个,返回false,
点赞
送花
回复
分享
发布于 04-01 15:02 吉林

相关推荐

6 10 评论
分享
牛客网
牛客企业服务