腾讯把我捞起来后在今天虐了我一把

面试:
1.怼项目
2.找出树的最近公共父节点
3.手撕算法

第一题:


三个数组A[], B[], C[]

A3大的数 + B4小的数 + C5大的数结果, 不存在则为0

long long GetResult(vector<int> A, vector<int> B, vector<int> C)

{

}


第二题,

数组A[n]

设数组A的子集为连续m个数,即A[0:m-1] , A[1:m] A[n-m+1:n]

子集的和为这m个数相加,求和最大的子集(有多个则输出多个)

vector<vector<int>> GetResult(vector<int> A, int n, int m)

{

}

第三题

n * (n+10086) * (n-10086)

string GetResult( long long n)

{

}

请问各位大佬后面两道题怎么做?

#腾讯##笔试题目#
全部评论
第一题排序或堆 第二题尺取 第三题大整数乘法,模拟或者直接BigInteger。
点赞 回复 分享
发布于 2019-04-01 14:58
哎,这些思路在考试时都有了,可是当时就是写不出来。本菜鸟答案仅供参考。 第一题: 三个数组A[], B[], C[] 求A第3大的数 + B第4小的数 + C第5大的数结果, 不存在则为0 long long GetResult(vector<int> A, vector<int> B, vector<int> C) { } import java.util.Arrays; public class GetResult {     public static void main(String[] arg)     {         int[] A = new int[]{8,2,4,9,3,6};         int[] B = new int[]{8,2,4,9,3,6};         int[] C = new int[]{8,2,4,9,3,6};         System.out.println(getResult(A,B,C));     }     public static int getResult(int[] A,int[] B,int[] C)     {         if(A==null||B==null||C==null||A.length==0||B.length==0||C.length==0)         {             return 0;         }         Arrays.sort(A);         Arrays.sort(B);         Arrays.sort(C);         int sum =0;         int n = A.length,m = B.length,k = C.length;         sum = A[n-3]+B[3]+C[n-5];         return sum;     } } 第二题, 数组A[n] 设数组A的子集为连续m个数,即A[0:m-1] , A[1:m] … A[n-m+1:n] 子集的和为这m个数相加,求和最大的子集(有多个则输出多个) vector<vector<int>> GetResult(vector<int> A, int n, int m) { } 第二题虫取法 尺取就像虫虫一样。往前爬。比如[1,m]到[2,m+1],求出[1,m]的和后,只要删除1,加上m+1,就是[2,m+1]的和。 import java.util.*; public class GetResult2 {     public static void main(String[] arg)     {         int[] A = new int[]{8,2,4,9,3,6};         System.out.println(getResult2(A,6,2));     }     public static ArrayList<ArrayList<Integer>>getResult2(int[] A, int n , int m) {         ArrayList<ArrayList<Integer>> ret = new ArrayList<ArrayList<Integer>>();         ArrayList<Integer> list1 = new ArrayList<Integer>();         ArrayList<Integer> list2 = new ArrayList<Integer>();         int i = 0, j = m - 1;         int sum = 0;         while (j <= n - 1) {             for (int z = i; z <= j; z++) {                 sum += A[z];             }             System.out.println(sum);             list1.add(sum);             sum = 0;//忘记将sum重新置为0了。             i++;             j++;         }         int z = 0, k = m - 1;//忘记重新更新目录         int max =Collections.max(list1);         System.out.println(max);         while (k <= n - 1) {             for (int h = z; h <= k; h++) {                 sum += A[h];             }             if(sum==max)             {                 for(int g =k-m+1;g<=k;g++)                 list2.add(A[g]);                 System.out.println(list2);                 int size=list2.size()/m;                 while (size>0)                 {                     ret.add(list2);                     size--;                 }             }             sum = 0;//忘记将sum重新置为0了。             z++;             k++;         }         return ret;     } } 第三题 求n * (n+10086) * (n-10086) string GetResult( long long n) { } 大数运算 package com.linbin.struts; import java.math.BigDecimal; public class GetResult3 {     public static void main(String[] arg)     {         System.out.println(GetResult(100000));     }     public static String GetResult( long n) {        BigDecimal b1 = new BigDecimal(10086);        BigDecimal b2 = new BigDecimal(n);        BigDecimal b3=b2.pow(2);        BigDecimal b4 = b1.pow(2);        BigDecimal b5 =b3.subtract(b4);        return  b2.multiply(b5).toString();     } }
点赞 回复 分享
发布于 2019-04-01 16:56
第1题直接排序这么凶相吗,有没有O(n)的
点赞 回复 分享
发布于 2019-04-02 10:59
老哥你今天还是提前批面试?
点赞 回复 分享
发布于 2019-04-01 16:06
第三题不是平方差公式?
点赞 回复 分享
发布于 2019-04-01 15:19
第二题  就固定一个  长度为m的队列 (队首负责判断和当前i的位置关系,如果i - que[st].pos > m 就pop, 尾部就负责添加元素.. 中间用一个sum维护..   复杂度o(n) 因为元素在队列里面只进入和出去一次,所以均摊到每个元素上是O(1)的.. 第三题. 化简一下?   然后大数乘法写一下?
点赞 回复 分享
发布于 2019-04-01 15:11

相关推荐

天降大厂offer:你是我见过最美的牛客女孩
点赞 评论 收藏
分享
评论
点赞
16
分享

创作者周榜

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