算法设计:如何查找矩阵中的最大元素?详细实现

2021年3月20日14:27:55 发表评论 704 次浏览

本文概述

给定一个NxM矩阵。任务是在此矩阵中找到最大元素。

例子:

Input: mat[4][4] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
Output: 25

Input: mat[3][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}, {1, 0, 12, 45}};
Output: 45

推荐:请尝试以下方法{IDE}首先, 在继续解决方案之前。

方法:这个想法是使用两个嵌套循环遍历矩阵, 一个嵌套行, 一个嵌套列, 并找到最大元素。使用最小值初始化变量maxElement并遍历矩阵, 并在每次比较当前元素大于maxElement时进行比较。如果是, 则使用当前元素更新maxElement。

下面是上述方法的实现:

C ++

// CPP code to find max element in a matrix
#include <bits/stdc++.h>
using namespace std;
  
#define N 4
#define M 4
  
// Function to find max element
// mat[][] : 2D array to find max element
int findMax( int mat[N][M])
{
  
     // Initializing max element as INT_MIN
     int maxElement = INT_MIN;
  
     // checking each element of matrix
     // if it is greater than maxElement, // update maxElement
     for ( int i = 0; i < N; i++) {
         for ( int j = 0; j < M; j++) {
             if (mat[i][j] > maxElement) {
                 maxElement = mat[i][j];
             }
         }
     }
  
     // finally return maxElement
     return maxElement;
}
  
// Driver code
int main()
{
  
     // matrix
     int mat[N][M] = { { 1, 2, 3, 4 }, { 25, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
  
     cout << findMax(mat) << endl;
  
     return 0;
}

Java

// Java code to find max element in a matrix
  
public class GFG {
      
     final static int N = 4 ;
     final static int  M = 4 ;
  
     // Function to find max element
     // mat[][] : 2D array to find max element
     static int findMax( int mat[][])
     {
  
         // Initializing max element as INT_MIN
         int maxElement = Integer.MIN_VALUE;
  
         // checking each element of matrix
         // if it is greater than maxElement, // update maxElement
         for ( int i = 0 ; i < N; i++) {
             for ( int j = 0 ; j < M; j++) {
                 if (mat[i][j] > maxElement) {
                     maxElement = mat[i][j];
                 }
             }
         }
  
         // finally return maxElement
         return maxElement;
     }
  
      
     // Driver code
     public static void main(String args[])
     {
            // matrix
         int mat[][] = { { 1 , 2 , 3 , 4 }, { 25 , 6 , 7 , 8 }, { 9 , 10 , 11 , 12 }, { 13 , 14 , 15 , 16 } };
  
         System.out.println(findMax(mat)) ;
    
     }
     // This Code is contributed by ANKITRAI1
}

Python3

# Python 3 code to find max element 
# in a matrix
import sys
N = 4
M = 4
  
# Function to find max element
# mat[][] : 2D array to find max element
def findMax(mat):
      
     # Initializing max element as INT_MIN
     maxElement = - sys.maxsize - 1
  
     # checking each element of matrix
     # if it is greater than maxElement, # update maxElement
     for i in range (N):
         for j in range (M):
             if (mat[i][j] > maxElement):
                 maxElement = mat[i][j]
          
     # finally return maxElement
     return maxElement
  
# Driver code
if __name__ = = '__main__' :
      
     # matrix
     mat = [[ 1 , 2 , 3 , 4 ], [ 25 , 6 , 7 , 8 ], [ 9 , 10 , 11 , 12 ], [ 13 , 14 , 15 , 16 ]]
     print (findMax(mat))
  
# This code is contributed by
# Surendra_Gangwar

C#

// C# code to find max element in a matrix
using System;
  
class GFG {
      
     static int N = 4;
     static int M = 4 ;
  
     // Function to find max element
     // mat[, ] : 2D array to find max element
     static int findMax( int [, ] mat)
     {
  
         // Initializing max element as INT_MIN
         int maxElement = int .MinValue;
  
         // checking each element of matrix
         // if it is greater than maxElement, // update maxElement
         for ( int i = 0; i < N; i++) {
              
             for ( int j = 0; j < M; j++) {
                  
                 if (mat[i, j] > maxElement) {
                      
                     maxElement = mat[i, j];
                 }
             }
         }
  
         // finally return maxElement
         return maxElement;
     }
  
      
     // Driver code
     public static void Main()
     {
          
         // matrix
         int [, ]mat = {{ 1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
  
         Console.Write(findMax(mat)) ;
     }
      
}
  
// This code is contributed by ChitraNayal

的PHP

<?php
// PHP code to find max element in a matrix
  
// Function to find max element
// mat[][] : 2D array to find max element
function findMax( $mat )
{
  
     // Initializing max element as INT_MIN
     $maxElement = PHP_INT_MIN;
  
     // checking each element of matrix
     // if it is greater than maxElement, // update maxElement
     for ( $i = 0; $i < 4; $i ++) 
     {
         for ( $j = 0; $j < 4; $j ++)
         {
             if ( $mat [ $i ][ $j ] > $maxElement )
             {
                 $maxElement = $mat [ $i ][ $j ];
             }
         }
     }
  
     // finally return maxElement
     return $maxElement ;
}
  
// Driver code
$mat = array ( array (1, 2, 3, 4), array (25, 6, 7, 8), array (9, 10, 11, 12), array (13, 14, 15, 16));
  
echo findMax( $mat ) . "\n" ;
  
// This code is contributed 
// by Akanksha Rai
?>

输出如下:

25

时间复杂度:O(N * M)


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: