美文网首页程序员
面试题 17.23. 最大黑方阵

面试题 17.23. 最大黑方阵

作者: xy啊_46b8 | 来源:发表于2020-04-22 16:58 被阅读0次

给定一个方阵,其中每个单元(像素)非黑即白。设计一个算法,找出 4 条边皆为黑色像素的最大子方阵。

返回一个数组 [r, c, size] ,其中 r, c 分别代表子方阵左上角的行号和列号,size 是子方阵的边长。若有多个满足条件的子方阵,返回 r 最小的,若 r 相同,返回 c 最小的子方阵。若无满足条件的子方阵,返回空数组。

示例 1

解释: 输入中 0 代表黑色,1 代表白色,标粗的元素即为满足条件的最大子方阵

示例2

关键词:leetcode,异或运算,矩阵,数组

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class ostt {

    private int global_row, global_col;

        private int[][] global_matrix;

        private static int[]res =new int[3];

        public int[] findSquare(int[][] matrix) {

            global_matrix = matrix;

            if ((global_row = matrix.length) <1) return new int[0];

            global_col = matrix[0].length;

            for (int i =0; i < global_row -res[2]; ++i)

                and_operate(i);

            return res[2] >0 ?res :new int[0];

        }

        private void and_operate(int cur_idx) {

        int[] first_row =global_matrix[cur_idx], base = first_row.clone();

        if (res[2] <1)

            for (int i =0; i < global_col; ++i)

                if (first_row[i] <1) {

                    res[2] =1;

                    res[0] = cur_idx;

                    res[1] = i;

                    break;

                }

            int offset =0;

            for (int i = cur_idx +1; i < global_row; ++i) {

                ++offset;

                int[] last_row =global_matrix[i];

                for (int j =0; j < global_col; ++j)

                    base[j] |=global_matrix[i][j];

                List idx_list =new ArrayList<>();

                int count =0, temp_idx =0;

                for (; temp_idx < global_col - offset; ++temp_idx) {

                    if (base[temp_idx] ==0) {

                        ++count;

                        if (base[temp_idx + offset] ==0)

                            idx_list.add(temp_idx);

                    }

                }

                if (count <2) {

                    for (; temp_idx < global_col; ++temp_idx)

                        if (base[temp_idx] ==0 && ++count ==2)

                            break;

                    if (count <2)

                        return;

                }

                for (int begin_idx : idx_list) {

                    int end_idx = begin_idx + offset;

                    for (int l = begin_idx +1; l < end_idx; ++l)

                        if (first_row[l] >0 || last_row[l] >0)

                            continue;

                    if (res[2] < offset +1) {

                        res[2] = offset +1;

                        res[0] = cur_idx;

                        res[1] = begin_idx;

                    }

                }

            }

        }

        public static void main(String[] args){

            int matrix[][]={{0,1,1},{1,0,1},{1,1,0}};

            ostt osfs =new ostt();

            osfs.findSquare(matrix);

            System.out.println(Arrays.toString(res));

        }

}

相关文章

  • 面试题 17.23. 最大黑方阵

    给定一个方阵,其中每个单元(像素)非黑即白。设计一个算法,找出 4 条边皆为黑色像素的最大子方阵。 返回一个数组 ...

  • 面试题 17.23. 最大黑方阵

    题目地址 https://leetcode-cn.com/problems/max-black-square-lc...

  • 寻找最大子方阵

    题目描述 给定一个元素为0或1的方阵,编写一个程序,找出其中最大的子方阵,使得该子方阵的元素都是1。程序先提示用户...

  • 构建数组

    网上的tx面试题:输入n,输出方阵:n=5时如下: n=3时如下: 代码: output:

  • Python3 欧拉计划 问题11-15

    11、数字方阵中的最大乘积   在下面20×20的数字方阵中,以第7行第9列的数字26[加粗]开始,右下对角线方向...

  • 《方阵》

    文/陈雄辉 我踟蹰在这方阵里 承受着钢筋水泥的凝固和冰冷 没有一种思路像炊烟一样柔软 没有一种表达像乡愁一样自然 ...

  • 方阵

    白云、丛兰、诗香、菩提、碧漪…… 收获、小窗、眉长、汤河、细雨 甲虫、她她、寒秋、安然、旖旎 枫红、南飞、月明、频...

  • 运动会(原文)

    11月10日星期五,我们学校举行了运动会。 先是走方阵,鲜花方阵,红旗方阵,然后是几个班级的方阵。我在走方阵时忘了...

  • 2019-07-23 MPPT

    mppt是什么? Maximum Power Point Tracking,最大功率点跟踪,指对光伏方阵表面温度变...

  • 3-16方阵问题

    从植树问题,我们扩展到了方阵的问题。 方阵问题是很重要的数形结合应用。 方阵有实心和空心两种,实心方阵暂时放下,那...

网友评论

    本文标题:面试题 17.23. 最大黑方阵

    本文链接:https://www.haomeiwen.com/subject/sbfwihtx.html