20220925用友Java笔试

1. 用栈模拟,或者用链表

import java.util.Stack;

public class Main{
    public static void main(String[] args) {
        int[] nums = {9,8,7,6,5,4,3,2,1};
        System.out.println(fun1(nums));
    }

    public static int fun1(int[] circle){
        if (circle.length==1) return circle[0];
        int len = circle.length;
        Stack<Integer> stack = new Stack<>();
        for (int i = len-1; i >=0 ; i--) {
            if (circle[i]!=1) stack.push(circle[i]);
        }
        while (stack.size()>1){
            if (stack.size()>1){
                stack = funStackpop(stack,2);
            }
            if (stack.size()>1){
                stack = funStackpop(stack,3);
            }
            if (stack.size()>1){
                stack = funStackpop(stack,0);
            }
            if (stack.size()>1){
                stack = funStackpop(stack,1);
            }
        }
        return stack.pop();
    }

    public static Stack<Integer> funStackpop(Stack<Integer> stack,int k){
        Stack<Integer> stackHelp = new Stack<>();
        int sizeOri = stack.size();
        if (sizeOri==2){
            stack.pop();
            return stack;
        }

        for (int i = 1; i <= sizeOri ; i++) {
            int temp = stack.pop();
            if (temp%4!=k){
                stackHelp.push(temp);
            }
        }
        int sizeAfter = stackHelp.size();
        for (int i = 0; i <sizeAfter ; i++) {
            stack.push(stackHelp.pop());
        }
        return stack;
    }
}

2. 网格问题-力扣62加强版(动规)

/**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * @param x int整型 目标点的X坐标
     * @param y int整型 目标点的Y坐标
     * @param z int整型 目标点的Z坐标
     * @return long长整型
     */
    public long countPaths (int x, int y, int z) {
        long[][][] memo = new long[x + 1][y + 1][z + 1];
        //base case
        //从起点到起点只有一种路径,那就是站着不动,这就是唯一的base case
        memo[0][0][0] = 1;
        return dp(memo, x, y, z);
    }
    //采用记忆化搜索的办法
    private long dp(long[][][] memo, int x, int y, int z){
        //dp含义,从0,0,0到x,y,z的路径数量
        //状态转移
        //依据题意,每次都只能沿着三条坐标轴之一移动一个方向,所以到达x,y,z这个点的,只可能有这三种情况
        //从x-1,y,z过来,从x,y-1,z过来,从x,y,z-1过来
        //因此状态转移方程为
        //dp[x][y][z] = dp[x-1][y][z] + dp[x][y-1][z] + dp[x][y][z-1]
        if(x == -1 || y == -1 || z == -1){
            return 0;
        }
        if(memo[x][y][z] != 0){
            return memo[x][y][z];
        }
        //只能从三个方向而来
        long res = dp(memo, x - 1, y, z) + dp(memo, x, y - 1, z) + dp(memo, x, y, z - 1);
        memo[x][y][z] = res;
        return memo[x][y][z];
    }

 

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值