Leetcode 1380 矩阵中的幸运数

  C:

int getMinInRow(int **matrix, int rowSize, int rowPoint)
{
    int minPoint = 0, minValue = matrix[rowPoint][0];
    for (int i = 1; i < rowSize; i++)
    {
        if (matrix[rowPoint][i] < minValue)
        {
            minPoint = i;
            minValue = matrix[rowPoint][i];
        }
    }
    return minPoint;
}

int getMaxInCell(int **matrix, int cellSize, int cellPoint)
{
    int maxPoint = 0, maxValue = matrix[0][cellPoint];
    for (int i = 1; i < cellSize; i++)
    {
        if (matrix[i][cellPoint] > maxValue)
        {
            maxPoint = i;
            maxValue = matrix[i][cellPoint];
        }
    }
    return maxPoint;
}

/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int *luckyNumbers(int **matrix, int matrixSize, int *matrixColSize, int *returnSize)
{
    int *cellCache = (int *)malloc(sizeof(int) * matrixColSize[0]);
    int *ret = (int *)malloc(sizeof(int) * matrixSize * matrixColSize[0]);
    int reSize = 0;
    for (int i = 0; i <  *matrixColSize; i++)
        cellCache[i] = -1;
    for (int i = 0; i < matrixSize; i++)
    {
        int minPoint = getMinInRow(matrix, matrixColSize[0], i);
        if (cellCache[minPoint] == -1)
            cellCache[minPoint] = getMaxInCell(matrix, matrixSize, minPoint);
        if (cellCache[minPoint] == i)
        {
            ret[reSize] = matrix[i][minPoint];
            reSize++;
        }
    }
    *returnSize = reSize;
    free(cellCache);
    return ret;
}

  JAVA:

class Solution {
public List<Integer> luckyNumbers(int[][] matrix) {
        if (matrix == null || matrix.length == 0) return new ArrayList<Integer>(0);
        List<Integer> reList = new ArrayList<Integer>();
        int[] cellCache = new int[matrix[0].length];
        for (int i = 0; i < cellCache.length; i++) cellCache[i] = -1;
        for (int i = 0; i < matrix.length; i++) {
            int minPoint = this.getMinInRow(matrix, i);
            if (cellCache[minPoint] == -1) cellCache[minPoint] = this.getMaxInCell(matrix, minPoint);
            if (i == cellCache[minPoint]) reList.add(matrix[i][minPoint]);
        }
        return reList;
    }

    private final int getMinInRow(int[][] matrix, int rowPoint) {
        int[] row = matrix[rowPoint];
        int minPoint = 0, minValue = row[0];
        for (int i = 1; i < row.length; i++) {
            if (row[i] < minValue) {
                minPoint = i;
                minValue = row[i];
            }
        }
        return minPoint;
    }

    private final int getMaxInCell(int[][] matrix, int cellPoint) {
        int maxPoint = 0, maxValue = matrix[0][cellPoint];
        for (int i = 1; i < matrix.length; i++) {
            if (matrix[i][cellPoint] > maxValue) {
                maxPoint = i;
                maxValue = matrix[i][cellPoint];
            }
        }
        return maxPoint;
    }
}

  JS:

/**
 * @param {number[][]} matrix
 * @return {number[]}
 */
var luckyNumbers = function (matrix) {
    if (!matrix || matrix.length == 0) return [];
    let re = [];
    let cellCache = new Array(matrix.length);
    for (let i = 0; i < matrix.length; i++) {
        let minPoint = getMinInRow(matrix, i);
        if (!cellCache[minPoint]) cellCache[minPoint] = getMaxInCell(matrix, minPoint);
        if (cellCache[minPoint] == i) re.push(matrix[i][minPoint]);
    }
    return re;
};

var getMinInRow = function (matrix, rowPoint) {
    let row = matrix[rowPoint];
    let minPoint = 0, minValue = row[0];
    for (let i = 1; i < row.length; i++) {
        if (row[i] < minValue) {
            minPoint = i;
            minValue = row[i];
        }
    }
    return minPoint;
}

var getMaxInCell = function (matrix, cellPoint) {
    let maxPoint = 0, maxValue = matrix[0][cellPoint];
    for (let i = 1; i < matrix.length; i++) {
        if (matrix[i][cellPoint] > maxValue) {
            maxPoint = i;
            maxValue = matrix[i][cellPoint];
        }
    }
    return maxPoint;
}
posted @ 2022-02-15 17:44  牛有肉  阅读(43)  评论(0编辑  收藏  举报