奇安信笔试2021-8-7

算法:1  给定数组,返回可能存在的数量  
    i<j<k,   a[i]<a[j]<a[k] 或者 a[i]>a[j]>a[k]
public static int TeamNums (int[] height) {
        // write code here
        int count  = 0;
        for (int i = 0; i < height.length; i++) {
            for (int j = i+1; j>i&&j < height.length; j++) {
                for (int k = j+1; k>j&&k < height.length; k++) {
                    int a = height[i];
                    int b = height[j];
                    int c = height[k];
                    if (a<b&&b<c)count++;
                    if (a>b&&b>c)count++;
                }
            }
        }
        return count;
    }
算法第二题: 二维数组找到最大路径值
public class 奇安信 {
    public static void main(String[] args) {
        System.out.println(getMaximumResource(new int[][]{{1, 0, 7}, {2, 0, 6}, {3, 4, 5}, {0, 3, 0}, {9, 0, 20}}));
    }
    public static int getMaximumResource (int[][] grid) {
        // write code here
        int max = 0;
        Stack<Start> starts = new Stack<>();
        for (int i = 0; i < grid.length; i++) {  // 遍历所有的可能起始点
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j]!=0){
                    starts.push(new Start(i,j));
                }
            }
        }
        while (!starts.isEmpty()){
            int[][] arr = ArrayCopy(grid);
            Start start = starts.pop();
            max = Math.max(max,find(arr,start.i,start.j));
        }
        return max;
    }
    private static int find(int[][] temp, int i, int j) {
        int sum = temp[i][j];
        int top =0,left=0,right=0,bottom=0;
        temp[i][j] = 0;
        if (i-1>=0&&i-1<temp.length&&temp[i-1][j]!=0){
            int[][] arr = ArrayCopy(temp);
            top = find(arr,i-1,j);
        }
        if (i+1>=0&&i+1<temp.length&&temp[i+1][j]!=0){
            int[][] arr = ArrayCopy(temp);
            bottom = find(arr,i+1,j);
        }
        if (j-1>=0&&j-1<temp[0].length&&temp[i][j-1]!=0){
            int[][] arr = ArrayCopy(temp);
            left = find(arr,i,j-1);
        }
        if (j+1>=0&&j+1<temp[0].length&&temp[i][j+1]!=0){
            int[][] arr = ArrayCopy(temp);
            right = find(arr,i,j+1);
        }
        return sum+Math.max(Math.max(top,bottom),Math.max(left,right));
    }
    private static int[][] ArrayCopy(int[][]temp){  // 数组拷贝
        int[][] arr = new int[temp.length][temp[0].length];
        for (int n = 0; n < temp.length; n++) {  
            for (int m= 0; m < temp[0].length; m++) {
                arr[n][m] =temp[n][m];
            }
        }
        return arr;
    }
}
class Start{
    int i;
    int j;

    public Start(int i, int j) {
        this.i = i;
        this.j = j;
    }
}


#奇安信笔试##奇安信##笔经#
全部评论
第一题暴力能全A吗
1 回复
分享
发布于 2021-08-07 23:46
第二题深度优先搜索20多行就行了
点赞 回复
分享
发布于 2021-08-08 00:42
联想
校招火热招聘中
官网直投
第二题没解出来能面试吗😅
点赞 回复
分享
发布于 2021-08-08 21:09
有收到面试通知吗?我下午睡过头了,迟到50分钟做题,前面选择题蒙了很多,A了一道,还有一道题写完了,没时间交
点赞 回复
分享
发布于 2021-08-09 15:57

相关推荐

头像
不愿透露姓名的神秘牛友
04-17 14:34
点赞 评论 收藏
转发
1 8 评论
分享
牛客网
牛客企业服务