`

Matrix

 
阅读更多

1. (难点 注意第12题)

Q: Print a matrix spirally

A: <http://www.careercup.com/question?id=2991909>

void m1(int[][] input) {

int i, w, h, k;

int height = input.length;

int width = input[0].length;

for (i = 0, w = width - 1, h = height - 1; w > 0; i++, w--, h--) {

for (k = i; k < w; k++)

System.out.println(input[i][k]);

for (k = i; k < h; k++)

System.out.println(input[k][w]);

for (k = w; k > i; k--)

System.out.println(input[h][k]);

for (k = h; k > i; k--)

System.out.println(input[k][i]);

}

}

 

2.

Q: You have a MxN matrix of 0s and 1s and in each row maximum one 1 is present. You have to tell the algorithm you'll use to find efficiently the number of 1s present in matrix along with the positions they are present i..e Matrix[i][j]. He was interested in minimizing comparisons basically and not any complexity.

A: <http://discuss.techinterview.org/default.asp?interview.11.763599.12>

typdef void (*func)(int, int) FUNC;

void NOP(int m, int n) {

}

void PRINT(int m, int n) {

    printf("(%d,%d)", m,n);

    counter ++;

}

CheckArray(int **array, int M, int N) {

    FUNC[2];

    FUNC[0] = NOP;

    FUNC[1] = PRINT;

    int m, n;

  for (m = 0; m < M; m++)

        for(n = 0; n < N; n++)

            FUNC(array[m][n])(m,n);

}

 

3.

Q: Given a 4 x 4 square, fill in each cell an integer selected from {1, 2, 3, 4}. Make sure that each row, each column, and each 2 x 2 sub-square (excluding the central one) do not contain duplicate numbers. Design an algorithm. The following example is a valid solution.

1 3 2 4

2 4 1 3

3 1 4 2

4 2 3 1

A: <http://discuss.joelonsoftware.com/default.asp?interview.11.397435.11>

见工程SudokuLight

 

4. 

Q: Given a matrix with each row and column sorted in ascending order. Find the median in the matrix.

A: <http://www.careercup.com/question?id=4285682>

It is Young Tableaux. It can be shown that A[i,j]>=A[k,l], where k=0..i and l=0..j except i=k and j=l. And same for lower-right part of matrix but with <= sign. So, I think that median element can appear only on matrix diagonal. So you should take all median elements, sort and find median. An observation - the median will always lie on the MINOR diagonal, the elements of the quadrants that are not on the diagonal will either be less than or more than half of the elements in the matrix, the major diagonal is already sorted so it has to lie on it, it will be the exact centre of the matrix, only the minor diagonal does not preserve any order, so effectively array of n -> elements of the minor diagonal, find the median :)

 

5. (难题)

Q: A m*n matrix of integer, all rows and columns are sorted in ascending order. Find the most efficient way to print out all numbers in ascending order. 类似题 Given a matrix of integers where every row is sorted and every column is sorted. Print all elements in sorted order. Cannot use merging of arrays. Solution should be better than O(n2logn)

A: 见工程YoungTableauPrintAscendingly

 

6.

Q: Young Tableau: 一个NxM的matrix,每一行,每一列都是递增的,现找第k大个数,要求时间复杂度低于O(mn)

A: <http://www.ruhike.com/blogs/travel/archive/2010/04/19/2-sorting-selection-searching.aspx>

在young tableaux中找第K大元素可以参考在heap中找第k大元素,因为young tableaux和heap有些类似. youngify()与heapify()类似,通过shift元素来保持young tabuleaux特性,其实现可参考http://lyle.smu.edu/~saad/courses/cse3358/ps5/problemset5sol.pdf. for a table (m, n), youngify() 的复杂度:at most (m+n)

while (count <k ) { 

  e = table.remove_min(); 

  table.youngify(); //去掉最小元素后,调整结构以保持young tabuleaux特性, 

  count++; 

return e;

 

7. (马虎过)

Q: 100*100部分有序矩阵数组的排序 有100个有序数组(从小到大),每个里面有100个数。设计一个算法合并这个一百个有序数组,中间步骤只允许多申请一个大小为100个数的空间(也就是一个数组的大小)。

A: http://fayaa.com/tiku/view/102/ 见解法2

void Merge(int data[][N], int startLine, int endLine)

{

    if(startLine + 1 >= endLine)

        return;

    int mid = (startLine + endLine) / 2;

    Merge(data, startLine, mid);

    Merge(data, mid, endLine);

    int i, j, k;

    i = j = k = 0;

    int *first = (int*)&data[startLine][0];

    int *second = (int*)&data[mid][0];

    while(i < (mid - startLine) * N && j < (endLine - mid) * N)

    {

        if(first[i] < second[j])

            buffer[k++] = first[i], ++i;

        else

            buffer[k++] = second[j], ++j;

    }

    while(i < (mid - startLine) * N)

        buffer[k++] = first[i], ++i;

    while(j < (endLine - mid) * N)

        buffer[k++] = second[j], ++j;

    assert(is_sorted(buffer, buffer + k));

    for(i = 0; i < k; i++)

    {

        data[startLine + i / N][i % N] = buffer[i];

    }

}

 

8. (难题)

Q: Given a binary matrix, find out the maximum size square sub-matrix with all 1s.

A: <http://geeksforgeeks.org/?p=6257>

1) Construct a sum matrix S[R][C] for the given M[R][C].

     a) Copy first row and first columns as it is from M[][] to S[][]

     b) For other entries, use following expressions to construct S[][]

         If M[i][j] is 1 then

            S[i][j] = min(S[i][j-1], S[i-1][j], S[i-1][j-1]) + 1

         Else /*If M[i][j] is 0*/

            S[i][j] = 0

2) Find the maximum entry in S[R][C]

3) Using the value and coordinates of maximum entry in S[i], print

   sub-matrix of M[][]

 

9. (难题)

Q: Maximum subarray with all 1’s. Given A two-dimensional array b (M rows, N columns) of Boolean values ("0" a 

nd "1") Goal: Find the largest (most elements) rectangular subarray containing all ones. 

A: <http://www.seas.gwu.edu/~simhaweb/cs151/lectures/module6/module6.html> 或者 <http://www.drdobbs.com/184410529>

 

10.

Q: Write a program to multiply two matrices.

A: <http://www.thecareerplus.com/?page=resources&cat=10&subCat=90&qNo=38>

// Matrix A (m*n)

// Matrix B (n*k)

// Matrix C (m*k)

for(i=0; i<m; i++)

{

   for(j=0;j<k;j++)

   {

      c[i][j]=0;

      for(l=0;l<n;l++)

           c[i][j] += a[i][l] * b[l][j];

   }

}

 

 

 

// CareerCups

11.

Q: [2.14.1] Write an algorithm such that if an element in an MxN matrix is 0, its entire row and column is set to 0.

A: int[][] a = new int[m]n[];

int[] row = new int[m];

int[] column = new int[n];

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

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

        if (a[i][j] == 0) {

            row[i] == 1;

            column[j] == 1;

        }

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

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

        if (row[i] == 1 || column[j] == 1)

            a[i][j] = 0;

 

12. (代码易写错 注意第1题)

Q: [4.1.6] Given an image represented by an NxN matrix, where each pixel in the image is 4 bytes, write a method to rotate the image by 90 degrees Can you do this in place?

A: public static void rotate(int[][] matrix, int n) {

    for(int i = 0; i < n/2; i++) {

        int first = i;

        int last = matrix.length - first - 1;

        for(int j = first; j < last; j++) {

            int offset = j - first;

            int temp = matrix[first][j];

            matrix[first][j] = matrix[last-offset][first];

            matrix[last-offset][first] = matrix[last][last-offset];

            matrix[last][last-offset] = matrix[j][last];

            matrix[j][last] = temp;

        }

    }

}

 

13. (难点)

Q: [2.14.3] Given a matrix in which each row and each column is sorted, write a method to find an element in it.

A: boolean findElement(int[][] matrix, int key) {

    int m = matrix.length -1;

    int n = 0;

    while(m > 0 && n < matrix[0].length) {

        if (key == matrix[m][n])

            return true;

        else if (key < matrix[m][n])

            m -= 1;

        else

            n += 1; 

    }

    return false;

}

 

14. (难点)

Q: [4.20.11] Imagine you have a square matrix, where each cell is filled with either black or white. Design and algorithm to find the maximum sub-square such that all four borders are filled with black pixels.

A: Subsquare findSquare(int[][] matrix) {

    int N = matrix.length;

    int col = 0;

    int currentMax = 0;

    Subsquare square = null;

    while (N-col > currentMax) {

        for (int row = 0; row < matrix.length; row++) {

            int size = N - Math.max(row, col);

            while (size > currentMax) {

                if (isSquare(matrix, row, col, size)) {

                    currentMax = size;

                    square = new Subsquare(row, col, size);

                    break;

                }

                size--;

            }

        }

        col++;

    }

    return square;

}

 

boolean isSquare(int[][] matrix, int row, int col, int size) {

    for(int i = 0; i < size; i++) {

        if (matrix[row][col + i] == 0)

            return false;

        if (matrix[row + size - 1][col + i] == 0)

            return false;

    }

    for(int i = 0; i < size; i++) {

        if (matrix[row + i][col] == 0)

            return false;

        if (matrix[row + i][col + size - 1] == 0)

            return false;

    }

    return true;

}

 

class Subsquare {

    public int row, column, size;

    public Subsquare(int r, int c, int sz) {

        this.row = r;

        this.column = c;

        this.size = sz;

    }

}

 

15. (解决)

Q: [4.20.12] Given an NxN matrix of positive and negative integers, write code to find the sub-matrix with the largest possible sum.

A: int getMaxMatrix(int[][] original) {

    int sumMax = 0;

    int[][] preprost = preprocess(original);

    for (int row1 = 0; row1 < original.length; row1++)

        for(int row2 = row1; row2 < original.length; row2++)

            for(int col1 = 0; col1 < original[0].length; col1++)

                for(col2 = col1; col2 < original[0].length; col2++)

                    sumMax = Math.max(sumMax, compute(preprost, row1, col1, 

                        row2, col2));    

}

 

int[][] preprocess(int[][] original) {

    int[][] matrix = new int[original.length][original[0].length];

    for (int i = 0; i < original.length; i++)

        for (int j = 0; j < original[0].length; j++) {

            if (i == 0 && j == 0)

                matrix[i][j] = original[i][j];

            else if (i == 0)

                matrix[i][j] += original[i][j-1];

            else if (j == 0)

                matrix[i][j] += original[i-1][j];

            else

                matrix[i][j] = matrix[i][j] + matrix[i-1][j] + matrix[i][j-1]

                    - matrix[i-1][j-1];

        }

    return matrix;

}

 

int compute(int[][] matrix, int row1, int col1, int row2, int col2) {

    if (row1 == 0 && col1 == 0)

        return matrix[row2][col2];

    else if (row1 == 0)

        return matrix[row2][col2] - matrix[row2][col1-1];

    else if (col1 == 0)

        return matrix[row2][col2] - matrix[row1-1][col2];

    else 

        return matrix[row2][col2] - matrix[row2][col1-1] - matrix[row1-1][col2]

            + matrix[row1-1][col1-1];

}

 

16.

Q: [4.8.2] Imagine a robot sitting on the upper left hand corner of an NxN grid The robot can only move in two directions: right and down How many possible paths are there for the robot? Imagine certain squares are “off limits”, such that the robot can not step on them Design an algorithm to get all possible paths for the robot.

A: 1). (X-1 + Y-1)! / ((X-1)! * (Y-1)!)

2). boolean isFree(int x, int y) {

return (m[x][y] == 0)?true:false;

}

 

boolean findPath(int x, int y) {

boolean success = false;

BoardPoint p = new BoardPoint(x, y);

path.add(p);

m[x][y] = 3;

 

if (x == 9 && y == 9)

return true;

 

if (x < 10 && isFree(x+1, y))

success = findPath(x+1, y);

if (!success && y < 10 && isFree(x, y+1))

success = findPath(x, y+1);

 

if (!success) {

path.remove(p);

m[x][y] = 2;

}

 

return success;

}

 

17.

Q: Given a M*N matrix A in which all the elements in a row and all the elements in a column are strictly increasing. Find a path from the smallest element (ie A[0][0]) to the largest element (ie A[M-1][N-1]) such that the sum of the elements in the path is maximum. Time Complexity O(m+n). Use efficient space.

A: <http://www.mitbbssg.com/bbsann2/life.faq/JobHunting/17/D12842543542i0/M.1287178937_2.40/%CE%CA%C1%BD%B5%C0%CE%A2%C8%ED%CC%E2> <http://www.careercup.com/question?id=421669>

 

分享到:
评论

相关推荐

    ansys matrix27单元详解

    《ANSYS Matrix27单元详解——自定义单元深入解析》 在ANSYS这款强大的有限元分析软件中,Matrix27单元是一种特殊的自定义单元类型,主要用于处理非线性问题,尤其是那些涉及到大变形、接触非线性和材料非线性的...

    DataMatrix动态库源码

    DataMatrix动态库源码是一种用于生成DataMatrix二维条形码的软件开发资源,它由C++语言编写,方便开发者在自己的应用程序中集成DataMatrix码的生成功能。DataMatrix码是一种广泛应用在工业自动化、物流管理、电子...

    c++编写的 矩阵 matrix 类源码

    这里我们关注的是一份名为“matrix”的自定义C++类,它提供了对矩阵进行基本运算的功能,包括加减法、乘法以及矩阵的逆运算。下面我们将详细探讨这些知识点。 首先,`matrix.h`文件通常包含类的声明,定义了类`...

    LABEL MATRIX 8.60特别版

    《LABEL MATRIX 8.60特别版:深入解析与应用》 LABEL MATRIX是一款专业的标签设计与打印软件,专为各种工业、商业及个人用户设计,提供了丰富的图形编辑工具和广泛的条形码支持。本文将围绕“LABEL MATRIX 8.60特别...

    Label Matrix32简体中文

    Label Matrix32是一款专业的标签设计软件,主要用于创建各种类型的标签,包括条形码、二维码、图形、文字等,广泛应用于工业、物流、零售等领域。这款软件的简体中文版本为国内用户提供了更友好的操作界面,使其能更...

    datamatrix编码和解码的程序,没有原码

    数据矩阵(DataMatrix)是一种二维条形码标准,由国际自动识别制造商协会(AIM)制定,主要用于在小尺寸标签上存储大量数据。这种编码技术在制造业、物流、电子元器件等领域广泛应用,因为它能够以极小的面积容纳...

    DmDecoder(DLL).rar_DataMatrix.dll_DmDecoder_datamatrix C_dmdecod

    《DataMatrix解码库及其应用解析》 在IT领域中,数据编码与解码是至关重要的环节,尤其在自动化和物联网(IoT)系统中。DataMatrix编码是一种二维条形码标准,它能够高效地存储大量信息,常用于工业标识、物流追踪等...

    Android-使用Matrix对Bitmap进行处理

    而Matrix则是Android图形系统中的一个关键类,它允许我们对图像进行各种变换操作,如旋转、缩放、平移和倾斜等。这个教程将深入探讨如何在Android中利用Matrix对Bitmap进行处理。 首先,我们需要了解Bitmap的基本...

    Delphi 生成DataMatrix二維碼源碼

    本话题主要聚焦于使用Delphi编程语言生成DataMatrix二维条形码的源代码。DataMatrix是一种小型、紧凑的矩阵式二维条形码标准,常用于工业自动化、电子元器件标识等领域,因为它具有高密度和纠错能力。 DataMatrix由...

    aggs-matrix-stats-client-6.8.3-API文档-中文版.zip

    赠送jar包:aggs-matrix-stats-client-6.8.3.jar; 赠送原API文档:aggs-matrix-stats-client-6.8.3-javadoc.jar; 赠送源代码:aggs-matrix-stats-client-6.8.3-sources.jar; 赠送Maven依赖信息文件:aggs-matrix-...

    和矩阵相关的头文件matrix.h

    ### 和矩阵相关的头文件matrix.h知识点解析 #### 文件概述 `matrix.h` 是一个自定义的C++头文件,用于实现基本的矩阵操作。当在C++程序开发过程中遇到“找不到 matrix.h”这类错误时,可以通过创建并填充该头文件来...

    DataMatrix二维码解码不同种方式.zip

    DataMatrix二维码,作为一种二维条码技术,被广泛应用于工业自动化、物流管理、产品标识等领域,因其高密度存储信息和抗损性强的特点而受到青睐。在本压缩包"DataMatrix二维码解码不同种方式.zip"中,包含了三种开源...

    步入Matrix函数 步入Matrix函数 步入Matrix函数

    Matrix函数在Flash 8中是一个重要的工具,尤其对于2D图形的变换操作至关重要。它基于3x3矩阵,虽然在理论数学中3x3矩阵可以用于3D计算,但在Flash 8中,Matrix主要应用于2D空间内的x轴和y轴变换。这个3x3矩阵由6个...

    android 利用matrix实现图片的旋转与缩放

    Matrix类在Android图形处理中扮演着重要角色,它是一个3x3的矩阵,用于进行2D变换,如平移、旋转、缩放和倾斜等。本教程将深入讲解如何利用Matrix实现图片的旋转与缩放。 首先,我们需要了解Matrix的基本操作。在...

    BusMatrix生成脚本

    在嵌入式系统设计中,总线矩阵(BusMatrix)是一种关键的硬件组件,它用于管理和协调不同子系统之间的数据传输。"BusMatrix生成脚本"通常是指一个自动化工具,用于根据特定的需求配置和生成ARM-design_start架构中的...

    C# DataMatrix.net使用

    【标题】:“C# DataMatrix.net使用” 在C#编程环境中,DataMatrix是一种二维条形码格式,常用于工业自动化、物流追踪等领域,因为它能够存储大量数据并具有高纠错能力。`DataMatrix.net`是一个开源库,允许开发者...

    delphi调用label matrix

    在Delphi编程环境中,"delphi调用label matrix"这个主题涉及到如何在Delphi应用程序中使用Label控件来创建和管理类似矩阵的布局。Label Matrix通常是指一种将多个Label控件按照行列排列的方式,用于展示表格或者网格...

    Label Matrix 32条码编辑.打印软件

    Label Matrix 32是一款专业的条码编辑和打印软件,它为用户提供了强大的功能,以便创建、设计和打印各种类型的条形码。这款软件在工业、零售、物流等多个领域都有着广泛的应用,因为它支持多种条码标准,能满足不同...

    DataMatrix二维条码编码和解码源代码(C#)

    DataMatrix二维条码是一种广泛应用在工业自动化、物流追踪、电子护照等领域的高密度编码技术。它采用矩阵式结构,能够存储大量的数据,并且具有抗损性强、读取速度快等特点。在C#编程环境下,实现DataMatrix条码的...

    DataLogic Matrix300N 中文彩页

    DataLogic Matrix300N是一款超紧凑型影像式条码阅读器,专为高速和直接零件标记(DPM)应用而设计。它采用130万像素/60帧/秒的高分辨率成像器,能超快速进行图像采集,是Matrix系列中的新一代紧凑型影像式条码阅读器...

Global site tag (gtag.js) - Google Analytics