首页 > 其他分享 >leetcode-1380-easy

leetcode-1380-easy

时间:2022-11-20 10:02:48浏览次数:47  
标签:matrix int max lucky leetcode length easy 1380 its

Lucky Numbers in a Matrix

Given an m x n matrix of distinct numbers, return all lucky numbers in the matrix in any order.

A lucky number is an element of the matrix such that it is the minimum element in its row and maximum in its column.

Example 1:

Input: matrix = [[3,7,8],[9,11,13],[15,16,17]]
Output: [15]
Explanation: 15 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example 2:

Input: matrix = [[1,10,4,2],[9,3,8,7],[15,16,17,12]]
Output: [12]
Explanation: 12 is the only lucky number since it is the minimum in its row and the maximum in its column.
Example 3:

Input: matrix = [[7,8],[1,2]]
Output: [7]
Explanation: 7 is the only lucky number since it is the minimum in its row and the maximum in its column.
Constraints:

m == mat.length
n == mat[i].length
1 <= n, m <= 50
1 <= matrix[i][j] <= 105.
All elements in the matrix are distinct.

思路一:m*n 的二维数组,先求出每个 m 的最小值,然后求每个 n 的最大值,如果最大值和最小值相等,就是所求解

public List<Integer> luckyNumbers (int[][] matrix) {
    int m = matrix.length;
    int n = matrix[0].length;

    int[] minVal = new int[m];
    for (int i = 0; i < matrix.length; i++) {

        int min = matrix[i][0];
        for (int j = 1; j < matrix[i].length; j++) {
            min = Math.min(min, matrix[i][j]);
        }
        minVal[i] = min;
    }

    int[] maxVal = new int[n];
    for (int i = 0; i < n; i++) {
        int max = Integer.MIN_VALUE;
        for (int j = 0; j < m; j++) {
            max = Math.max(max, matrix[j][i]);
        }
        maxVal[i] = max;
    }

    for (int k : minVal) {
        for (int i : maxVal) {
            if (k == i) {
                return new ArrayList<>(Arrays.asList(k));
            }
        }
    }

    return new ArrayList<>();
}

标签:matrix,int,max,lucky,leetcode,length,easy,1380,its
From: https://www.cnblogs.com/iyiluo/p/16907917.html

相关文章

  • leetcode-507-easy
    PerfectNumberAperfectnumberisapositiveintegerthatisequaltothesumofitspositivedivisors,excludingthenumberitself.Adivisorofanintegerx......
  • leetcode-1403-easy
    MinimumSubsequenceinNo-IncreasingOrderGiventhearraynums,obtainasubsequenceofthearraywhosesumofelementsisstrictlygreaterthanthesumofth......
  • leetcode-506-easy
    RelativeRanksYouaregivenanintegerarrayscoreofsizen,wherescore[i]isthescoreoftheithathleteinacompetition.Allthescoresareguaranteedt......
  • 代码随想录day4---LeetCode24. 两两交换链表中的节点&19.删除链表的倒数第N个节点&面
    LeetCode24.两两交换链表中的节点题目链接给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节......
  • EasyX之鼠标
    一、头文件#include<graphics.h>二、鼠标功能以下截取自graphics.h//Oldmouserelatedfunctions旧鼠标相关功能//Mousemessage//WM_MOUSEMOVE......
  • #yyds干货盘点# LeetCode 腾讯精选练习 50 题:2 的幂
    题目:给你一个整数n,请你判断该整数是否是2的幂次方。如果是,返回true;否则,返回false。如果存在一个整数x使得 n==2x,则认为n是2的幂次方。 示例1:输入:n=1输......
  • leetcode_Day1_14最长公共前缀
    1.题目  2.解一  主要思路:横向比较,字符串数组的公共前缀等于前两个字符串的公共前缀与第三个字符串比较,再与第四个比较。即依次遍历字符串数组中的每个字符串,对......
  • [LeetCode] 1099. Two Sum Less Than K
    Givenanarraynumsofintegersand integerk,returnthemaximumsumsuchthatthereexistsi<jwithnums[i]+nums[j]=sumandsum<k.Ifnoi,jexis......
  • [LeetCode] 891. Sum of Subsequence Widths
    Thewidthofasequenceisthedifferencebetweenthemaximumandminimumelementsinthesequence.Givenanarrayofintegersnums,returnthesumofthewi......
  • [oeasy]python0017_解码_decode_字节序列_bytes_字符串_str
    ​ 解码decode回忆上次内容code就是码最早也指电报码后来有各种编码、密码、砝码、条码都指的是把各种事物编个号encode就是编码编码就是给事物编个号......