来也科技飞扬季笔试 2023 届秋招专场 java

本文解析了三道编程面试题:两数之和的算法实现、直方图中可捕获的水量计算以及组合子集的递归方法。通过实例展示了如何利用双指针和动态规划解决数组问题,以及如何利用条件判断和遍历来求解直方图问题。最后,介绍了使用递归策略生成所有可能的子集列表。
摘要由CSDN通过智能技术生成

第一题(签到题)

代码:

 public int[] twoSum(int[] nums, int target) {
        int[] out = new int[ 2 ];
        for ( int i = 0; i < nums.length; i ++ ) {
            for ( int j = 0; j < nums.length; j ++ ) {
                if ( j == i )
                    continue;
                if ( nums[ i ] + nums[ j ] ==target ) {
                    out[0] = i;
                    out[1] = j;
                    return out;
                }
            }
        }
        return out;
 }

第二题


思路:

总水量 = 所有的水池装的水之和 = 所有水池中的每个 i 位置的装水量相加之和

所有的水池是由 一跟柱子height[ i ] + 后面的第一根>=height[ i ] 的柱子组成的 

也就是说,水池的左边边界柱子(也是水池装水的最高高度),必须是后面还有高度不低于它的柱子才可以

 代码:

 public int trap( int[] height ) {
      if ( height.length == 1 || height.length == 0)
            return 0;
        int max = 0;
        int n = height.length;
        int h = height[ 0 ], hIndex = 0;
        // 遍历找出第一个樯的高度 != 0 的下标,因为只有在非0的墙后面才可能装水
        for( int k = 0; k < n; k ++ ) {
            if( height[ k ] != 0 ) {
                h = height[ k ];
                hIndex = k;
                for ( int i = k; i < n; i ++ ) {
                    System.out.print( i +"》");
                    boolean f = false;
                    if ( i == hIndex ) {
                        f = true;
                        h = height[ hIndex ];
                        int j = 0;
                        // 寻找后面比目前最高的墙还高的墙的下标
                        while ( h >= 0 ) {
                            boolean flag = false;
                            for (j = i + 1; j < n; j++) {
                                if (height[j] >= h) {
                                    hIndex = j;
                                    System.out.print("[" + hIndex + "]");
                                    flag = true;
                                    break;
                                }
                            }
                            if ( flag )
                                break;
                            // 后见没有比现在最高的高度h高的墙,就h--,再继续遍历后面的寻找
                            if (j == n) {
                                h --;
                                continue;
                            }
                        }
                    }

                    // 加上本i位置所能装的水量(最高的墙 - 本i位置的墙的高度)
                    if ( !f ) {
                        max += h - height[i];
                        System.out.print(h - height[i] + "  ");
                    }
                }
                break;  // 找到非0的墙后,break掉
            }
        }
        System.out.println();
        return max;
 }

 面试题 17.21. 直方图的水量

第三题

 思路:

        递归

代码(成功90%):

static List<List<Integer>> list = new ArrayList<>();
    String str = "";
    public List<List<Integer>> subsets(int[] nums) {

        for ( int j = 0; j < nums.length; j ++ ) {
            List<Integer> list1 = new ArrayList<>();
            list1.add( nums[ j ] );
            getResult( nums, list1, j + 1 );
        }
        getResult( nums, new ArrayList<>(), 0 );
        return list;

    }

    private void getResult(int[] nums, List<Integer> integerList, int i) {

        String s = "[";
        for( int j = 0; j < integerList.size(); j ++ ) {
            s += integerList.get( j );
        }
        s += "]";
        if( ! str.contains( s ) ) {
            str += s + "---";
            list.add(new ArrayList<>(integerList));
        }

        if ( i == nums.length )
            return;

        for ( int j = i; j < nums.length; j ++ ) {
            List<Integer> list2 = new ArrayList<>( integerList );
            list2.add( nums[ j ] );
            getResult( nums, list2, j + 1 );
        }
}

成功90%,最后一个用例显示解答错误,也不知道什么原因,也不应该是超时,因为我直接测试了最大范围的用例,运行时间27ms而已,哎不知道啥错误原因

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值